From 3a65a60fcb03b85d7ea59b5b8c60d35a7a62d711 Mon Sep 17 00:00:00 2001 From: Justin Hawkins Date: Mon, 4 Oct 2021 12:20:16 +1030 Subject: [PATCH] Move software version handling to a new package --- config/config.go | 22 -------------- dau.go | 10 +++++-- version/version.go | 29 +++++++++++++++++++ .../config_test.go => version/version_test.go | 6 ++-- web/server.go | 3 +- 5 files changed, 40 insertions(+), 30 deletions(-) create mode 100644 version/version.go rename config/config_test.go => version/version_test.go (50%) diff --git a/config/config.go b/config/config.go index e555166..86dbbf9 100644 --- a/config/config.go +++ b/config/config.go @@ -10,7 +10,6 @@ import ( daulog "github.com/tardisx/discord-auto-upload/log" "github.com/mitchellh/go-homedir" - "golang.org/x/mod/semver" ) // Config for the application @@ -23,8 +22,6 @@ var Config struct { Exclude string } -const CurrentVersion string = "v1.0.0" - // Load the current config or initialise with defaults func LoadOrInit() { configPath := configPath() @@ -77,22 +74,3 @@ func configPath() string { homeDir := homeDir() return homeDir + string(os.PathSeparator) + ".dau.json" } - -func NewVersionAvailable(v string) 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) - return false - } - comp := semver.Compare(v, CurrentVersion) - if comp == 0 { - return false - } - if comp == -1 { - return true - } - return false // they are using a newer one than exists? -} diff --git a/dau.go b/dau.go index 3a0fe75..51c4b4c 100644 --- a/dau.go +++ b/dau.go @@ -24,6 +24,7 @@ import ( "github.com/tardisx/discord-auto-upload/config" daulog "github.com/tardisx/discord-auto-upload/log" "github.com/tardisx/discord-auto-upload/uploads" + "github.com/tardisx/discord-auto-upload/version" "github.com/tardisx/discord-auto-upload/web" ) @@ -99,8 +100,11 @@ func checkUpdates() { log.Fatal("could not parse JSON: ", err) } - if config.NewVersionAvailable(latest.TagName) { - fmt.Printf("You are currently on version %s, but version %s is available\n", config.CurrentVersion, latest.TagName) + // 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("------------------------------------") @@ -128,7 +132,7 @@ func parseOptions() { if *versionFlag { fmt.Println("dau - https://github.com/tardisx/discord-auto-upload") - fmt.Printf("Version: %s\n", config.CurrentVersion) + fmt.Printf("Version: %s\n", version.CurrentVersion) os.Exit(0) } diff --git a/version/version.go b/version/version.go new file mode 100644 index 0000000..359fc01 --- /dev/null +++ b/version/version.go @@ -0,0 +1,29 @@ +package version + +import ( + "fmt" + "log" + + "golang.org/x/mod/semver" +) + +const CurrentVersion string = "v0.11.0" + +func NewVersionAvailable(v string) 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) + return false + } + comp := semver.Compare(v, CurrentVersion) + if comp == 0 { + return false + } + if comp == -1 { + return true + } + return false // they are using a newer one than exists? +} diff --git a/config/config_test.go b/version/version_test.go similarity index 50% rename from config/config_test.go rename to version/version_test.go index d03a9b5..7a433d3 100644 --- a/config/config_test.go +++ b/version/version_test.go @@ -1,13 +1,11 @@ -package config_test +package version import ( "testing" - - "github.com/tardisx/discord-auto-upload/config" ) func TestVersioning(t *testing.T) { - if !config.NewVersionAvailable("v0.1.0") { + if !NewVersionAvailable("v0.1.0") { t.Error("should be a version newer than v0.1.0") } } diff --git a/web/server.go b/web/server.go index 0ddc158..9b75cb7 100644 --- a/web/server.go +++ b/web/server.go @@ -16,6 +16,7 @@ import ( "github.com/tardisx/discord-auto-upload/config" daulog "github.com/tardisx/discord-auto-upload/log" "github.com/tardisx/discord-auto-upload/uploads" + "github.com/tardisx/discord-auto-upload/version" ) // DAUWebServer - stuff for the web server @@ -67,7 +68,7 @@ func getStatic(w http.ResponseWriter, r *http.Request) { } b.Body = string(data) b.Path = string(sanitized_path) - b.Version = config.CurrentVersion + b.Version = version.CurrentVersion t.Execute(w, b) return }