From 15cb2ba69cd68b54b68fe357fa62afd3e99abf50 Mon Sep 17 00:00:00 2001 From: Jordan Ribbink Date: Tue, 17 Sep 2024 12:34:25 -0700 Subject: [PATCH] Use semver comparison for new version check (#1749) --- go.mod | 2 +- go.sum | 1 + internal/command/command.go | 14 +++++++++++++- 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/go.mod b/go.mod index d50dbbe8e..75c2eef12 100644 --- a/go.mod +++ b/go.mod @@ -7,6 +7,7 @@ toolchain go1.22.3 require ( github.com/charmbracelet/bubbles v0.19.0 github.com/charmbracelet/bubbletea v0.27.1 + github.com/coreos/go-semver v0.3.0 github.com/dukex/mixpanel v1.0.1 github.com/getsentry/sentry-go v0.29.0 github.com/go-git/go-git/v5 v5.11.0 @@ -81,7 +82,6 @@ require ( github.com/cockroachdb/tokenbucket v0.0.0-20230807174530-cc333fc44b06 // indirect github.com/consensys/bavard v0.1.13 // indirect github.com/consensys/gnark-crypto v0.12.1 // indirect - github.com/coreos/go-semver v0.3.0 // indirect github.com/crate-crypto/go-ipa v0.0.0-20240223125850-b1e8a79f509c // indirect github.com/crate-crypto/go-kzg-4844 v1.0.0 // indirect github.com/cyphar/filepath-securejoin v0.2.4 // indirect diff --git a/go.sum b/go.sum index 39332ffa6..c89de506c 100644 --- a/go.sum +++ b/go.sum @@ -1432,6 +1432,7 @@ github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376/go.mod h1:an3vInlBmS github.com/go-git/go-billy/v5 v5.5.0 h1:yEY4yhzCDuMGSv83oGxiBotRzhwhNr8VZyphhiu+mTU= github.com/go-git/go-billy/v5 v5.5.0/go.mod h1:hmexnoNsr2SJU1Ju67OaNz5ASJY3+sHgFRpCtpDCKow= github.com/go-git/go-git-fixtures/v4 v4.3.2-0.20231010084843-55a94097c399 h1:eMje31YglSBqCdIqdhKBW8lokaMrL3uTkpGYlE2OOT4= +github.com/go-git/go-git-fixtures/v4 v4.3.2-0.20231010084843-55a94097c399/go.mod h1:1OCfN199q1Jm3HZlxleg+Dw/mwps2Wbk9frAWm+4FII= github.com/go-git/go-git/v5 v5.11.0 h1:XIZc1p+8YzypNr34itUfSvYJcv+eYdTnTvOZ2vD3cA4= github.com/go-git/go-git/v5 v5.11.0/go.mod h1:6GFcX2P3NM7FPBfpePbpLd21XxsgdAt+lKqXmCUiUCY= github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU= diff --git a/internal/command/command.go b/internal/command/command.go index 4bf32e438..64b1d9212 100644 --- a/internal/command/command.go +++ b/internal/command/command.go @@ -36,6 +36,7 @@ import ( "github.com/onflow/flow-cli/internal/prompt" + "github.com/coreos/go-semver/semver" "github.com/dukex/mixpanel" "github.com/getsentry/sentry-go" "github.com/spf13/afero" @@ -302,7 +303,18 @@ func checkVersion(logger output.Logger) { latestVersion := fmt.Sprintf("v%s", strings.TrimPrefix(latestVersionRaw, "v")) - if currentVersion != latestVersion { + // compare semver versions + currentSemver, err := semver.NewVersion(strings.TrimPrefix(currentVersion, "v")) + if err != nil { + return + } + + latestSemver, err := semver.NewVersion(strings.TrimPrefix(latestVersion, "v")) + if err != nil { + return + } + + if currentSemver.LessThan(*latestSemver) { logger.Info(fmt.Sprintf( "\n%s Version warning: a new version of Flow CLI is available (%s).\n"+ " Read the installation guide for upgrade instructions: https://docs.onflow.org/flow-cli/install\n",