Show if a new version is available in the web interface

This commit is contained in:
Justin Hawkins 2022-04-03 20:33:20 +09:30
parent 896946c751
commit fbd267e687
6 changed files with 101 additions and 61 deletions

View File

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

60
dau.go
View File

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

View File

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

View File

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

View File

@ -42,6 +42,10 @@
<a class="nav-link {{ if eq .Path "config.html"}} active {{ end }}" href="/config.html">Config</a>
<a class="nav-link {{ if eq .Path "uploads.html"}} active {{ end }}" href="/uploads.html">Uploads</a>
<a class="nav-link {{ if eq .Path "logs.html"}} active {{ end }}" href="/logs.html">Logs</a>
{{ if eq .NewVersionAvailable true }}
<a class="nav-link" href="{{ .NewVersionInfo.HTMLURL }}">Ver {{ .NewVersionInfo.TagName }} available!</a>
{{ end }}
</nav>
</div>
</header>

View File

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