From fca8155283712eeeb07e0a22686ca1a17bc91524 Mon Sep 17 00:00:00 2001 From: Jarek Kowalski Date: Fri, 31 Jul 2020 07:41:48 -0700 Subject: [PATCH] update_check: clean up version numbers to always include 'v' prefix --- cli/update_check.go | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/cli/update_check.go b/cli/update_check.go index 6e5674cad..08c62ba02 100644 --- a/cli/update_check.go +++ b/cli/update_check.go @@ -9,6 +9,7 @@ "os" "path/filepath" "strconv" + "strings" "time" "github.com/fatih/color" @@ -150,7 +151,7 @@ func verifyGitHubReleaseIsComplete(releaseName string) error { return nil } -func maybeCheckForUpdates() (string, error) { +func maybeCheckForUpdates(ctx context.Context) (string, error) { if v := os.Getenv(checkForUpdatesEnvar); v != "" { // see if environment variable is set to false. if b, err := strconv.ParseBool(v); err == nil && !b { @@ -164,6 +165,7 @@ func maybeCheckForUpdates() (string, error) { } if time.Now().After(us.NextCheckTime) { + log(ctx).Debugf("time for next update check has been reached") // before we check for update, write update state file again, so if this fails // we won't bother GitHub for a while us.NextCheckTime = time.Now().Add(*updateCheckInterval) @@ -176,6 +178,8 @@ func maybeCheckForUpdates() (string, error) { return "", errors.Wrap(err, "update to get latest release from GitHub") } + log(ctx).Debugf("latest version on github: %v previous %v", newAvailableVersion, us.AvailableVersion) + // we got updated version from GitHub, write it in a state file again if newAvailableVersion != us.AvailableVersion { if err = verifyGitHubReleaseIsComplete(newAvailableVersion); err != nil { @@ -190,7 +194,9 @@ func maybeCheckForUpdates() (string, error) { } } - if us.AvailableVersion == "" || semver.Compare(repo.BuildVersion, us.AvailableVersion) >= 0 { + log(ctx).Debugf("build version %v, available %v", ensureVPrefix(repo.BuildVersion), ensureVPrefix(us.AvailableVersion)) + + if us.AvailableVersion == "" || semver.Compare(ensureVPrefix(repo.BuildVersion), ensureVPrefix(us.AvailableVersion)) >= 0 { // no new version available return "", nil } @@ -210,7 +216,7 @@ func maybeCheckForUpdates() (string, error) { // maybePrintUpdateNotification prints notification about available version. func maybePrintUpdateNotification(ctx context.Context) { - updatedVersion, err := maybeCheckForUpdates() + updatedVersion, err := maybeCheckForUpdates(ctx) if err != nil { log(ctx).Debugf("unable to check for updates: %v", err) return @@ -221,5 +227,13 @@ func maybePrintUpdateNotification(ctx context.Context) { return } - noticeColor.Fprintf(os.Stderr, updateAvailableNotice, repo.BuildVersion, updatedVersion) //nolint:errcheck + noticeColor.Fprintf(os.Stderr, updateAvailableNotice, ensureVPrefix(repo.BuildVersion), ensureVPrefix(updatedVersion)) //nolint:errcheck +} + +func ensureVPrefix(s string) string { + if strings.HasPrefix(s, "v") { + return s + } + + return "v" + s }