Move software version handling to a new package

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

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

11
version/version_test.go Normal file
View File

@@ -0,0 +1,11 @@
package version
import (
"testing"
)
func TestVersioning(t *testing.T) {
if !NewVersionAvailable("v0.1.0") {
t.Error("should be a version newer than v0.1.0")
}
}