Skip to content

Commit

Permalink
Add new version check
Browse files Browse the repository at this point in the history
  • Loading branch information
RikudouSage committed Sep 25, 2024
1 parent c0f76bd commit e888f39
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 4 deletions.
28 changes: 28 additions & 0 deletions browser/OpenUrl.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package browser

import (
"os/exec"
"runtime"
)

func OpenUrl(url string) error {
var cmd string
var args []string

switch runtime.GOOS {
case "windows":
cmd = "cmd"
args = []string{"/c", "start"}
break
case "darwin":
cmd = "open"
break
default:
cmd = "xdg-open"
break
}

args = append(args, url)

return exec.Command(cmd, args...).Start()
}
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ module LargeCsvReader

go 1.22

require fyne.io/fyne/v2 v2.5.1
require (
fyne.io/fyne/v2 v2.5.1
github.com/hashicorp/go-version v1.7.0
)

require (
fyne.io/systray v1.11.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,8 @@ github.com/hashicorp/go-sockaddr v1.0.0/go.mod h1:7Xibr9yA9JjQq1JpNB2Vw7kxv8xerX
github.com/hashicorp/go-syslog v1.0.0/go.mod h1:qPfqrKkXGihmCqbJM2mZgkZGvKG1dFdvsLplgctolz4=
github.com/hashicorp/go-uuid v1.0.0/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=
github.com/hashicorp/go-uuid v1.0.1/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=
github.com/hashicorp/go-version v1.7.0 h1:5tqGy27NaOTB8yJKUZELlFAS/LTKJkrmONwQKeRZfjY=
github.com/hashicorp/go-version v1.7.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
github.com/hashicorp/go.net v0.0.1/go.mod h1:hjKkEWcCURg++eb33jQU7oqQcI9XDCnUzHA0oac0k90=
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
Expand Down
70 changes: 68 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package main

import (
"LargeCsvReader/browser"
"LargeCsvReader/widgets"
"embed"
"encoding/csv"
"fmt"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/container"
Expand All @@ -12,16 +14,78 @@ import (
"fyne.io/fyne/v2/layout"
"fyne.io/fyne/v2/storage"
"fyne.io/fyne/v2/widget"
"github.com/hashicorp/go-version"
"io"
"net/http"
"os"
"regexp"
"strings"
)

const repository = "/~https://github.com/RikudouSage/LargeCsvReader"

//go:embed translation
var translations embed.FS

//go:embed assets/appversion
var version string
var appVersion string

func checkForNewVersion(window fyne.Window) {
currentVersion, err := version.NewVersion(strings.TrimSpace(appVersion))
if err != nil || currentVersion.String() == "dev" {
fmt.Println(err)
return
}

const url = repository + "/releases/latest"
httpClient := &http.Client{
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
},
}
response, err := httpClient.Get(url)
if err != nil {
fmt.Println(err)
return
}
response.Body.Close()

redirectUrl := response.Header.Get("Location")
if redirectUrl == "" {
return
}

regex := regexp.MustCompile("https://.*/v([0-9.]+)")
matches := regex.FindStringSubmatch(redirectUrl)
if len(matches) < 2 {
return
}

newestVersion, err := version.NewVersion(matches[1])
if err != nil {
fmt.Println(err)
return
}

if newestVersion.GreaterThan(currentVersion) {
dialog.ShowConfirm(
lang.X("app.new_version.title", "New version found!"),
lang.X("app.new_version.description", "Do you want to download the newest version?"),
func(result bool) {
if !result {
return
}

err = browser.OpenUrl(redirectUrl)
if err != nil {
fmt.Println(err)
return
}
},
window,
)
}
}

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

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

Expand All @@ -196,6 +260,8 @@ func main() {
),
))

go checkForNewVersion(window)

window.Show()
fyneApp.Run()
}
4 changes: 3 additions & 1 deletion translation/cs.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,7 @@
"app.load_more_button": "Načíst více",
"app.separator": "Oddělovač",
"app.choose_csv_button.description": "Pomocí tlačítka níže zvolte CSV soubor, který chcete načíst",
"app.choose_csv_button": "Otevřít CSV"
"app.choose_csv_button": "Otevřít CSV",
"app.new_version.title": "Nalezena nová verze!",
"app.new_version.description": "Chcete novější verzi stáhnout?"
}

0 comments on commit e888f39

Please sign in to comment.