Check if newer version exists in Github (#128)

* Check if newer version exists in Github
This commit is contained in:
Igor Gov
2021-07-21 16:36:47 +03:00
committed by GitHub
parent 30651c0f75
commit 7d5ed601df
4 changed files with 56 additions and 3 deletions

View File

@@ -1,15 +1,19 @@
package mizu
import (
"context"
"encoding/json"
"fmt"
"github.com/google/go-github/v37/github"
"github.com/romana/rlog"
"github.com/up9inc/mizu/shared"
"github.com/up9inc/mizu/shared/semver"
"io/ioutil"
"net/http"
"net/url"
"time"
)
func getApiVersion(port uint16) (string, error) {
versionUrl, _ := url.Parse(fmt.Sprintf("http://localhost:%d/mizu/metadata/version", port))
req := &http.Request{
@@ -44,3 +48,45 @@ func CheckVersionCompatibility(port uint16) (bool, error) {
fmt.Printf(Red, fmt.Sprintf("cli version (%s) is not compatible with api version (%s)\n", SemVer, apiSemVer))
return false, nil
}
func CheckNewerVersion() {
rlog.Debugf("Checking for newer version...")
start := time.Now()
client := github.NewClient(nil)
latestRelease, _, err := client.Repositories.GetLatestRelease(context.Background(), "up9inc", "mizu")
if err != nil {
rlog.Debugf("Failed to get latest release")
return
}
versionFileUrl := ""
for _, asset := range latestRelease.Assets {
if *asset.Name == "version.txt" {
versionFileUrl = *asset.BrowserDownloadURL
break
}
}
if versionFileUrl == "" {
rlog.Debugf("Version file not found in the latest release")
return
}
res, err := http.Get(versionFileUrl)
if err != nil {
rlog.Debugf("http.Get version asset -> %v", err)
return
}
data, err := ioutil.ReadAll(res.Body)
res.Body.Close()
if err != nil {
rlog.Debugf("ioutil.ReadAll -> %v", err)
return
}
gitHubVersion := string(data)
gitHubVersion = gitHubVersion[:len(gitHubVersion)-1]
rlog.Debugf("Finished version validation, took %v", time.Since(start))
if SemVer < gitHubVersion {
fmt.Printf(Yellow, fmt.Sprintf("Update available! %v -> %v (%v)\n", SemVer, gitHubVersion, *latestRelease.HTMLURL))
}
}