From b112b12a3fac204152cc60be4000dc53fe3def92 Mon Sep 17 00:00:00 2001 From: Justin Hawkins Date: Sun, 26 Sep 2021 21:15:04 +0930 Subject: [PATCH] Add version package --- version/version.go | 79 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 version/version.go diff --git a/version/version.go b/version/version.go new file mode 100644 index 0000000..f2c5dd5 --- /dev/null +++ b/version/version.go @@ -0,0 +1,79 @@ +// Package version deals with versioning of the software +package version + +import ( + "encoding/json" + "errors" + "fmt" + "io" + "log" + "net/http" + + "golang.org/x/mod/semver" +) + +type Info struct { + CurrentVersion string `json:"current_version"` + GithubVersion string `json:"github_version"` + UpgradeAvailable bool `json:"upgrade_available"` + GithubVersionFetched bool `json:"-"` +} + +func (i *Info) UpdateGitHubVersion() error { + i.GithubVersionFetched = false + versionUrl := "https://api.github.com/repos/tardisx/gropple/releases" + resp, err := http.Get(versionUrl) + if err != nil { + log.Fatal("Error getting response. ", err) + } + defer resp.Body.Close() + + b, err := io.ReadAll(resp.Body) + if err != nil { + return fmt.Errorf("failed to read body: %v", err) + } + + type release struct { + HTMLUrl string `json:"html_url"` + TagName string `json:"tag_name"` + Name string `json:"name"` + } + + var releases []release + + err = json.Unmarshal(b, &releases) + if err != nil { + return fmt.Errorf("failed to read unmarshal: %v", err) + } + if len(releases) == 0 { + log.Printf("found no releases on github?") + return errors.New("no releases found") + } + + i.GithubVersion = releases[0].Name + + i.GithubVersionFetched = true + i.UpgradeAvailable = i.canUpgrade() + return nil +} + +func (i *Info) canUpgrade() bool { + if !i.GithubVersionFetched { + return false + } + + log.Printf("We are %s, github is %s", i.CurrentVersion, i.GithubVersion) + + if !semver.IsValid(i.CurrentVersion) { + log.Fatalf("current version %s is invalid", i.CurrentVersion) + } + + if !semver.IsValid(i.GithubVersion) { + log.Fatalf("github version %s is invalid", i.GithubVersion) + } + + if semver.Compare(i.CurrentVersion, i.GithubVersion) == -1 { + return true + } + return false +}