From 4ed4f472a4cd1b303272932acc004a59527ff5a8 Mon Sep 17 00:00:00 2001 From: NHAS Date: Mon, 1 May 2023 14:19:12 +1200 Subject: [PATCH] Add check updates functionality to fix #35 --- internal/config/config.go | 2 +- ui/check_updates.go | 63 +++++++++++++++++++++++++++++++++++++++ ui/structs.go | 1 + ui/templates/menus.html | 14 +++++++-- ui/ui_webserver.go | 4 +++ 5 files changed, 81 insertions(+), 3 deletions(-) create mode 100644 ui/check_updates.go diff --git a/internal/config/config.go b/internal/config/config.go index 5bbddf42..bb495735 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -71,7 +71,7 @@ type Config struct { path string Socket string `json:",omitempty"` - CheckVersion bool `json:",omitempty"` + CheckUpdates bool `json:",omitempty"` Proxied bool ExposePorts []string `json:",omitempty"` NAT *bool diff --git a/ui/check_updates.go b/ui/check_updates.go new file mode 100644 index 00000000..0a958d36 --- /dev/null +++ b/ui/check_updates.go @@ -0,0 +1,63 @@ +package ui + +import ( + "encoding/json" + "net/http" + "strings" + "time" + + "github.com/NHAS/wag/internal/config" +) + +type githubResponse struct { + Body string + Prerelease bool `json:"prerelease"` + TagName string `json:"tag_name"` + Published string `json:"published_at"` + Url string `json:"html_url"` +} + +type Update struct { + New bool + UpdateVersion string + Message []string + Url string + Released string +} + +var ( + mostRecentUpdate *Update + lastChecked time.Time +) + +func getUpdate() Update { + + if !config.Values().CheckUpdates { + return Update{} + } + + if time.Now().After(lastChecked.Add(15*time.Minute)) || mostRecentUpdate == nil { + resp, err := http.Get("https://api.github.com/repos/NHAS/wag/releases/latest") + if err != nil { + return Update{} + } + defer resp.Body.Close() + + var gr githubResponse + err = json.NewDecoder(resp.Body).Decode(&gr) + if err != nil { + return Update{} + } + + mostRecentUpdate = &Update{ + UpdateVersion: gr.TagName, + Message: strings.Split(gr.Body, "\r\n"), + Url: gr.Url, + Released: gr.Published, + } + } + + mostRecentUpdate.New = strings.Split(config.Version, "-")[0] != mostRecentUpdate.UpdateVersion + + return *mostRecentUpdate +} diff --git a/ui/structs.go b/ui/structs.go index 874b1ff3..b4946e17 100644 --- a/ui/structs.go +++ b/ui/structs.go @@ -1,6 +1,7 @@ package ui type Page struct { + Update Description string Title string User string diff --git a/ui/templates/menus.html b/ui/templates/menus.html index 34f4c7d3..b977c067 100755 --- a/ui/templates/menus.html +++ b/ui/templates/menus.html @@ -170,24 +170,34 @@
Tools: