From fbd267e68771ab926901513a325962e13e9729e6 Mon Sep 17 00:00:00 2001 From: Justin Hawkins Date: Sun, 3 Apr 2022 20:33:20 +0930 Subject: [PATCH] Show if a new version is available in the web interface --- CHANGELOG.md | 2 ++ dau.go | 60 +++++++---------------------------- version/version.go | 70 +++++++++++++++++++++++++++++++++++++---- version/version_test.go | 16 ++++++++-- web/data/wrapper.tmpl | 4 +++ web/server.go | 10 ++++-- 6 files changed, 101 insertions(+), 61 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2afbbff..1a9a933 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,8 @@ All notable changes to this project will be documented in this file. ## [Unreleased] +- Show if a new version is available in the web interface + ## [v0.12.0] - 2020-04-03 - Break upload page into pending/current/complete sections diff --git a/dau.go b/dau.go index 2536ad4..cc24c62 100644 --- a/dau.go +++ b/dau.go @@ -2,13 +2,10 @@ package main import ( "context" - "encoding/json" "flag" "fmt" "io/fs" - "io/ioutil" "log" - "net/http" "os" "path/filepath" "strings" @@ -54,7 +51,17 @@ func main() { web := web.WebService{Config: config, Uploader: up} web.StartWebServer() - go func() { checkUpdates() }() + go func() { + version.GetOnlineVersion() + if version.UpdateAvailable() { + fmt.Printf("You are currently on version %s, but version %s is available\n", version.CurrentVersion, version.LatestVersionInfo.TagName) + fmt.Println("----------- Release Info -----------") + fmt.Println(version.LatestVersionInfo.Body) + fmt.Println("------------------------------------") + fmt.Println("Upgrade at https://github.com/tardisx/discord-auto-upload/releases/latest") + daulog.SendLog(fmt.Sprintf("New version available: %s - download at https://github.com/tardisx/discord-auto-upload/releases/latest", version.LatestVersionInfo.TagName), daulog.LogTypeInfo) + } + }() // create the watchers, restart them if config changes // blocks forever @@ -169,51 +176,6 @@ func (w *watch) checkFile(path string, found *[]string, exclusions []string) err return nil } -func checkUpdates() { - - type GithubRelease struct { - HTMLURL string `json:"html_url"` - TagName string `json:"tag_name"` - Name string `json:"name"` - Body string `json:"body"` - } - - daulog.SendLog("checking for new version", daulog.LogTypeInfo) - - client := &http.Client{Timeout: time.Second * 5} - resp, err := client.Get("https://api.github.com/repos/tardisx/discord-auto-upload/releases/latest") - if err != nil { - daulog.SendLog(fmt.Sprintf("WARNING: Update check failed: %v", err), daulog.LogTypeError) - return - } - defer resp.Body.Close() - body, err := ioutil.ReadAll(resp.Body) - if err != nil { - log.Fatal("could not check read update response") - } - - var latest GithubRelease - err = json.Unmarshal(body, &latest) - - if err != nil { - log.Fatal("could not parse JSON: ", err) - } - - // pre v0.11.0 version (ie before semver) did a simple string comparison, - // but since "0.10.0" < "v0.11.0" they should still get prompted to upgrade - // ok - if version.NewVersionAvailable(latest.TagName) { - fmt.Printf("You are currently on version %s, but version %s is available\n", version.CurrentVersion, latest.TagName) - fmt.Println("----------- Release Info -----------") - fmt.Println(latest.Body) - fmt.Println("------------------------------------") - fmt.Println("Upgrade at https://github.com/tardisx/discord-auto-upload/releases/latest") - daulog.SendLog(fmt.Sprintf("New version available: %s - download at https://github.com/tardisx/discord-auto-upload/releases/latest", latest.TagName), daulog.LogTypeInfo) - } - - daulog.SendLog("already running latest version", daulog.LogTypeInfo) -} - func parseOptions() { var versionFlag bool flag.BoolVar(&versionFlag, "version", false, "show version") diff --git a/version/version.go b/version/version.go index ece417b..2e0d6aa 100644 --- a/version/version.go +++ b/version/version.go @@ -1,24 +1,47 @@ package version import ( + "encoding/json" "fmt" + "io/ioutil" "log" + "net/http" + "time" + + daulog "github.com/tardisx/discord-auto-upload/log" "golang.org/x/mod/semver" ) -const CurrentVersion string = "v0.12.0" +const CurrentVersion string = "v0.12.1" -func NewVersionAvailable(v string) bool { +type GithubRelease struct { + HTMLURL string `json:"html_url"` + TagName string `json:"tag_name"` + Name string `json:"name"` + Body string `json:"body"` +} + +var LatestVersion string +var LatestVersionInfo GithubRelease + +// UpdateAvailable returns true or false, depending on whether not a new version is available. +// It always returns false if the OnlineVersion has not yet been fetched. +func UpdateAvailable() bool { if !semver.IsValid(CurrentVersion) { panic(fmt.Sprintf("my current version '%s' is not valid", CurrentVersion)) } - if !semver.IsValid(v) { - // maybe this should just be a warning - log.Printf("passed in version '%s' is not valid - assuming no new version", v) + + if LatestVersion == "" { return false } - comp := semver.Compare(v, CurrentVersion) + + if !semver.IsValid(LatestVersion) { + // maybe this should just be a warning + log.Printf("online version '%s' is not valid - assuming no new version", LatestVersion) + return false + } + comp := semver.Compare(LatestVersion, CurrentVersion) if comp == 0 { return false } @@ -27,3 +50,38 @@ func NewVersionAvailable(v string) bool { } return false // they are using a newer one than exists? } + +func GetOnlineVersion() { + + daulog.SendLog("checking for new version", daulog.LogTypeInfo) + LatestVersion = "v0.12.0" + LatestVersionInfo = GithubRelease{ + HTMLURL: "https://github.com/tardisx/discord-auto-upload/releases/tag/v0.13.0", + TagName: "v0.13.0", + Name: "v0.13.0", + Body: "- cool things\n-wow things\n", + } + return + + client := &http.Client{Timeout: time.Second * 5} + resp, err := client.Get("https://api.github.com/repos/tardisx/discord-auto-upload/releases/latest") + if err != nil { + daulog.SendLog(fmt.Sprintf("WARNING: Update check failed: %v", err), daulog.LogTypeError) + return + } + defer resp.Body.Close() + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + log.Fatal("could not check read update response") + } + + var latest GithubRelease + err = json.Unmarshal(body, &latest) + + if err != nil { + log.Fatal("could not parse JSON: ", err) + } + + LatestVersion = latest.TagName + LatestVersionInfo = latest +} diff --git a/version/version_test.go b/version/version_test.go index 0f78b41..b66233a 100644 --- a/version/version_test.go +++ b/version/version_test.go @@ -4,8 +4,18 @@ import ( "testing" ) -func TestVersioning(t *testing.T) { - if !NewVersionAvailable("v1.0.0") { - t.Error("should be a version newer than v1.0.0") +func TestVersioningUpdate(t *testing.T) { + // pretend there is a new version + LatestVersion = "v0.13.0" + if !UpdateAvailable() { + t.Error("should be a version newer than " + CurrentVersion) + } +} + +func TestVersioningNoUpdate(t *testing.T) { + // pretend there is a new version + LatestVersion = "v0.12.1" + if UpdateAvailable() { + t.Error("should NOT be a version newer than " + CurrentVersion) } } diff --git a/web/data/wrapper.tmpl b/web/data/wrapper.tmpl index d720b9a..1f9b0cb 100644 --- a/web/data/wrapper.tmpl +++ b/web/data/wrapper.tmpl @@ -42,6 +42,10 @@ Config Uploads Logs + {{ if eq .NewVersionAvailable true }} + Ver {{ .NewVersionInfo.TagName }} available! + {{ end }} + diff --git a/web/server.go b/web/server.go index 41a8a18..30ed2bc 100644 --- a/web/server.go +++ b/web/server.go @@ -74,12 +74,16 @@ func (ws *WebService) getStatic(w http.ResponseWriter, r *http.Request) { } var b struct { - Body string - Path string - Version string + Body string + Path string + Version string + NewVersionAvailable bool + NewVersionInfo version.GithubRelease } b.Path = path b.Version = version.CurrentVersion + b.NewVersionAvailable = version.UpdateAvailable() + b.NewVersionInfo = version.LatestVersionInfo err = t.ExecuteTemplate(w, "layout", b) if err != nil {