add update check
continuous-integration/drone/tag Build is passing Details
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Jake 2022-02-05 12:08:02 +00:00
parent 7194fefc5c
commit ead5522c68
6 changed files with 67 additions and 1 deletions

View File

@ -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

1
go.mod
View File

@ -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
)

2
go.sum
View File

@ -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=

View File

@ -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)

50
update/update.go Normal file
View File

@ -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 ""
}
}

5
version.go Normal file
View File

@ -0,0 +1,5 @@
package main
var (
BuildVersion string = "?"
)