Move software version handling to a new package

This commit is contained in:
Justin Hawkins 2021-10-04 12:20:16 +10:30
parent 1812486b19
commit 3a65a60fcb
5 changed files with 40 additions and 30 deletions

View File

@ -10,7 +10,6 @@ import (
daulog "github.com/tardisx/discord-auto-upload/log" daulog "github.com/tardisx/discord-auto-upload/log"
"github.com/mitchellh/go-homedir" "github.com/mitchellh/go-homedir"
"golang.org/x/mod/semver"
) )
// Config for the application // Config for the application
@ -23,8 +22,6 @@ var Config struct {
Exclude string Exclude string
} }
const CurrentVersion string = "v1.0.0"
// Load the current config or initialise with defaults // Load the current config or initialise with defaults
func LoadOrInit() { func LoadOrInit() {
configPath := configPath() configPath := configPath()
@ -77,22 +74,3 @@ func configPath() string {
homeDir := homeDir() homeDir := homeDir()
return homeDir + string(os.PathSeparator) + ".dau.json" 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?
}

10
dau.go
View File

@ -24,6 +24,7 @@ import (
"github.com/tardisx/discord-auto-upload/config" "github.com/tardisx/discord-auto-upload/config"
daulog "github.com/tardisx/discord-auto-upload/log" daulog "github.com/tardisx/discord-auto-upload/log"
"github.com/tardisx/discord-auto-upload/uploads" "github.com/tardisx/discord-auto-upload/uploads"
"github.com/tardisx/discord-auto-upload/version"
"github.com/tardisx/discord-auto-upload/web" "github.com/tardisx/discord-auto-upload/web"
) )
@ -99,8 +100,11 @@ func checkUpdates() {
log.Fatal("could not parse JSON: ", err) log.Fatal("could not parse JSON: ", err)
} }
if config.NewVersionAvailable(latest.TagName) { // pre v0.11.0 version (ie before semver) did a simple string comparison,
fmt.Printf("You are currently on version %s, but version %s is available\n", config.CurrentVersion, latest.TagName) // 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("----------- Release Info -----------")
fmt.Println(latest.Body) fmt.Println(latest.Body)
fmt.Println("------------------------------------") fmt.Println("------------------------------------")
@ -128,7 +132,7 @@ func parseOptions() {
if *versionFlag { if *versionFlag {
fmt.Println("dau - https://github.com/tardisx/discord-auto-upload") 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) os.Exit(0)
} }

29
version/version.go Normal file
View File

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

View File

@ -1,13 +1,11 @@
package config_test package version
import ( import (
"testing" "testing"
"github.com/tardisx/discord-auto-upload/config"
) )
func TestVersioning(t *testing.T) { 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") t.Error("should be a version newer than v0.1.0")
} }
} }

View File

@ -16,6 +16,7 @@ import (
"github.com/tardisx/discord-auto-upload/config" "github.com/tardisx/discord-auto-upload/config"
daulog "github.com/tardisx/discord-auto-upload/log" daulog "github.com/tardisx/discord-auto-upload/log"
"github.com/tardisx/discord-auto-upload/uploads" "github.com/tardisx/discord-auto-upload/uploads"
"github.com/tardisx/discord-auto-upload/version"
) )
// DAUWebServer - stuff for the web server // DAUWebServer - stuff for the web server
@ -67,7 +68,7 @@ func getStatic(w http.ResponseWriter, r *http.Request) {
} }
b.Body = string(data) b.Body = string(data)
b.Path = string(sanitized_path) b.Path = string(sanitized_path)
b.Version = config.CurrentVersion b.Version = version.CurrentVersion
t.Execute(w, b) t.Execute(w, b)
return return
} }