Skip to content

Commit e888f39

Browse files
committed
Add new version check
1 parent c0f76bd commit e888f39

File tree

5 files changed

+105
-4
lines changed

5 files changed

+105
-4
lines changed

browser/OpenUrl.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package browser
2+
3+
import (
4+
"os/exec"
5+
"runtime"
6+
)
7+
8+
func OpenUrl(url string) error {
9+
var cmd string
10+
var args []string
11+
12+
switch runtime.GOOS {
13+
case "windows":
14+
cmd = "cmd"
15+
args = []string{"/c", "start"}
16+
break
17+
case "darwin":
18+
cmd = "open"
19+
break
20+
default:
21+
cmd = "xdg-open"
22+
break
23+
}
24+
25+
args = append(args, url)
26+
27+
return exec.Command(cmd, args...).Start()
28+
}

go.mod

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ module LargeCsvReader
22

33
go 1.22
44

5-
require fyne.io/fyne/v2 v2.5.1
5+
require (
6+
fyne.io/fyne/v2 v2.5.1
7+
github.com/hashicorp/go-version v1.7.0
8+
)
69

710
require (
811
fyne.io/systray v1.11.0 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,8 @@ github.com/hashicorp/go-sockaddr v1.0.0/go.mod h1:7Xibr9yA9JjQq1JpNB2Vw7kxv8xerX
186186
github.com/hashicorp/go-syslog v1.0.0/go.mod h1:qPfqrKkXGihmCqbJM2mZgkZGvKG1dFdvsLplgctolz4=
187187
github.com/hashicorp/go-uuid v1.0.0/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=
188188
github.com/hashicorp/go-uuid v1.0.1/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=
189+
github.com/hashicorp/go-version v1.7.0 h1:5tqGy27NaOTB8yJKUZELlFAS/LTKJkrmONwQKeRZfjY=
190+
github.com/hashicorp/go-version v1.7.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
189191
github.com/hashicorp/go.net v0.0.1/go.mod h1:hjKkEWcCURg++eb33jQU7oqQcI9XDCnUzHA0oac0k90=
190192
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
191193
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=

main.go

Lines changed: 68 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package main
22

33
import (
4+
"LargeCsvReader/browser"
45
"LargeCsvReader/widgets"
56
"embed"
67
"encoding/csv"
8+
"fmt"
79
"fyne.io/fyne/v2"
810
"fyne.io/fyne/v2/app"
911
"fyne.io/fyne/v2/container"
@@ -12,16 +14,78 @@ import (
1214
"fyne.io/fyne/v2/layout"
1315
"fyne.io/fyne/v2/storage"
1416
"fyne.io/fyne/v2/widget"
17+
"github.com/hashicorp/go-version"
1518
"io"
19+
"net/http"
1620
"os"
21+
"regexp"
1722
"strings"
1823
)
1924

25+
const repository = "https://github.com/RikudouSage/LargeCsvReader"
26+
2027
//go:embed translation
2128
var translations embed.FS
2229

2330
//go:embed assets/appversion
24-
var version string
31+
var appVersion string
32+
33+
func checkForNewVersion(window fyne.Window) {
34+
currentVersion, err := version.NewVersion(strings.TrimSpace(appVersion))
35+
if err != nil || currentVersion.String() == "dev" {
36+
fmt.Println(err)
37+
return
38+
}
39+
40+
const url = repository + "/releases/latest"
41+
httpClient := &http.Client{
42+
CheckRedirect: func(req *http.Request, via []*http.Request) error {
43+
return http.ErrUseLastResponse
44+
},
45+
}
46+
response, err := httpClient.Get(url)
47+
if err != nil {
48+
fmt.Println(err)
49+
return
50+
}
51+
response.Body.Close()
52+
53+
redirectUrl := response.Header.Get("Location")
54+
if redirectUrl == "" {
55+
return
56+
}
57+
58+
regex := regexp.MustCompile("https://.*/v([0-9.]+)")
59+
matches := regex.FindStringSubmatch(redirectUrl)
60+
if len(matches) < 2 {
61+
return
62+
}
63+
64+
newestVersion, err := version.NewVersion(matches[1])
65+
if err != nil {
66+
fmt.Println(err)
67+
return
68+
}
69+
70+
if newestVersion.GreaterThan(currentVersion) {
71+
dialog.ShowConfirm(
72+
lang.X("app.new_version.title", "New version found!"),
73+
lang.X("app.new_version.description", "Do you want to download the newest version?"),
74+
func(result bool) {
75+
if !result {
76+
return
77+
}
78+
79+
err = browser.OpenUrl(redirectUrl)
80+
if err != nil {
81+
fmt.Println(err)
82+
return
83+
}
84+
},
85+
window,
86+
)
87+
}
88+
}
2589

2690
func showPreviewWindow(filePath string, fyneApp fyne.App) {
2791
window := fyneApp.NewWindow(lang.X("app.preview", "Preview"))
@@ -173,7 +237,7 @@ func main() {
173237
panic(err)
174238
}
175239

176-
window := fyneApp.NewWindow(lang.X("app.title", "Large CSV Reader") + " (" + strings.TrimSpace(version) + ")")
240+
window := fyneApp.NewWindow(lang.X("app.title", "Large CSV Reader") + " (" + strings.TrimSpace(appVersion) + ")")
177241
window.Resize(fyne.NewSize(640, 480))
178242
window.SetMaster()
179243

@@ -196,6 +260,8 @@ func main() {
196260
),
197261
))
198262

263+
go checkForNewVersion(window)
264+
199265
window.Show()
200266
fyneApp.Run()
201267
}

translation/cs.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,7 @@
77
"app.load_more_button": "Načíst více",
88
"app.separator": "Oddělovač",
99
"app.choose_csv_button.description": "Pomocí tlačítka níže zvolte CSV soubor, který chcete načíst",
10-
"app.choose_csv_button": "Otevřít CSV"
10+
"app.choose_csv_button": "Otevřít CSV",
11+
"app.new_version.title": "Nalezena nová verze!",
12+
"app.new_version.description": "Chcete novější verzi stáhnout?"
1113
}

0 commit comments

Comments
 (0)