From ead5522c6805798e001cee52b670c60edd101534 Mon Sep 17 00:00:00 2001 From: Jake Walker Date: Sat, 5 Feb 2022 12:08:02 +0000 Subject: [PATCH] add update check --- README.md | 2 +- go.mod | 1 + go.sum | 2 ++ main.go | 8 ++++++++ update/update.go | 50 ++++++++++++++++++++++++++++++++++++++++++++++++ version.go | 5 +++++ 6 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 update/update.go create mode 100644 version.go diff --git a/README.md b/README.md index 756e44d..c305ced 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ and [Lip Gloss](https://github.com/charmbracelet/lipgloss). - [x] Statistics are saved to `~/.wordle.json` for use in other programs - [x] Same word list as the official website - [ ] Leaderboard of people on current tilde -- [ ] Update checking +- [x] Update checking - [ ] Export to sharable message/emojis - [ ] Share to Mastodon - [ ] Play previous days diff --git a/go.mod b/go.mod index d9eae25..c785880 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module tildegit.org/jakew/wordle go 1.17 require ( + github.com/Masterminds/semver/v3 v3.1.1 github.com/charmbracelet/bubbletea v0.19.3 github.com/charmbracelet/lipgloss v0.4.0 ) diff --git a/go.sum b/go.sum index 0fcfae2..3f49bf8 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,5 @@ +github.com/Masterminds/semver/v3 v3.1.1 h1:hLg3sBzpNErnxhQtUy/mmLR2I9foDujNK030IGemrRc= +github.com/Masterminds/semver/v3 v3.1.1/go.mod h1:VPu/7SZ7ePZ3QOrcuXROw5FAcLl4a0cBrbBpGY/8hQs= github.com/charmbracelet/bubbletea v0.19.3 h1:OKeO/Y13rQQqt4snX+lePB0QrnW80UdrMNolnCcmoAw= github.com/charmbracelet/bubbletea v0.19.3/go.mod h1:VuXF2pToRxDUHcBUcPmCRUHRvFATM4Ckb/ql1rBl3KA= github.com/charmbracelet/lipgloss v0.4.0 h1:768h64EFkGUr8V5yAKV7/Ta0NiVceiPaV+PphaW1K9g= diff --git a/main.go b/main.go index 798eb3f..a5e0946 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,7 @@ package main import ( + "bufio" "fmt" tea "github.com/charmbracelet/bubbletea" "os" @@ -8,6 +9,7 @@ import ( "tildegit.org/jakew/wordle/game/score" "tildegit.org/jakew/wordle/game/util" "tildegit.org/jakew/wordle/game/words" + "tildegit.org/jakew/wordle/update" "unicode" ) @@ -165,6 +167,12 @@ func (m model) View() string { } func main() { + updateMsg := update.CheckVersion(update.GetGiteaRelease("tildegit.org", "jakew", "wordle"), BuildVersion) + if updateMsg != "" { + fmt.Println(fmt.Sprintf("%v. Press enter to continue...", updateMsg)) + bufio.NewReader(os.Stdin).ReadBytes('\n') + } + p := tea.NewProgram(initialModel()) if err := p.Start(); err != nil { fmt.Printf("Something has gone wrong: %v", err) diff --git a/update/update.go b/update/update.go new file mode 100644 index 0000000..66dc70a --- /dev/null +++ b/update/update.go @@ -0,0 +1,50 @@ +package update + +import ( + "encoding/json" + "fmt" + "github.com/Masterminds/semver/v3" + "io" + "net/http" +) + +func GetGiteaRelease(instance string, owner string, repo string) *semver.Version { + res, err := http.Get(fmt.Sprintf("https://%v/api/v1/repos/%v/%v/releases", instance, owner, repo)) + if err != nil { + return nil + } + defer res.Body.Close() + body, err := io.ReadAll(res.Body) + + var data []struct { + TagName string `json:"tag_name"` + } + err = json.Unmarshal(body, &data) + if err != nil || len(data) < 1 { + return nil + } + + v, err := semver.NewVersion(data[0].TagName) + if err != nil { + return nil + } + + return v +} + +func CheckVersion(latest *semver.Version, current string) string { + if latest == nil { + return "" + } + + current1, err := semver.NewVersion(current) + if err != nil { + return "" + } + + if latest.GreaterThan(current1) { + return fmt.Sprintf("A new version, %s, is available for download", latest) + } else { + return "" + } +} diff --git a/version.go b/version.go new file mode 100644 index 0000000..b4ce86b --- /dev/null +++ b/version.go @@ -0,0 +1,5 @@ +package main + +var ( + BuildVersion string = "?" +)