diff --git a/common/common.go b/common/common.go index e07fa035..155331a0 100644 --- a/common/common.go +++ b/common/common.go @@ -3,6 +3,7 @@ package common import ( "context" "fmt" + "strings" "github.com/google/go-github/v57/github" "github.com/savioxavier/termlink" @@ -45,15 +46,16 @@ func GithubClient() *github.Client { func CheckVersionUpdate() { ghClient := GithubClient() res, skip := VersionCheck(ghClient) - if skip { + if skip || res == nil || res.TagName == nil { return } - // Check if the version is different from the one in the binary - if res.TagName != nil && *res.TagName != fmt.Sprintf("v%s", VersionCli) { - if res.TagName != nil && *res.TagName != VersionCli { - fmt.Printf("A newer version (%s) is available, please upgrade with \"civo update\"\n", *res.TagName) - } + latest := *res.TagName + latestClean := strings.TrimPrefix(latest, "v") + currentClean := strings.TrimPrefix(VersionCli, "v") + + if latestClean != currentClean { + fmt.Printf("A newer version (%s) is available, please upgrade with \"civo update\"\n", latest) } } diff --git a/common/common_test.go b/common/common_test.go index e55725b3..0e75ad70 100644 --- a/common/common_test.go +++ b/common/common_test.go @@ -32,3 +32,19 @@ func TestVersionCheck(t *testing.T) { } }) } + +func TestCheckVersionUpdate(t *testing.T) { + t.Run("nil release or tag safety", func(t *testing.T) { + oldCli := VersionCli + defer func() { VersionCli = oldCli }() + + VersionCli = "1.0.0" + + // Ensure calling with nil handling doesn't panic + res, skip := VersionCheck(github.NewClient(nil)) + if skip && res == nil { + // Should return cleanly without panic + CheckVersionUpdate() + } + }) +}