From 16f1d63c8de35626e52def15f67fab99b58719c0 Mon Sep 17 00:00:00 2001 From: Patrice Ferlet Date: Mon, 28 Mar 2022 14:47:21 +0200 Subject: [PATCH] Ensure that the upgraded version is really greater --- go.mod | 1 + go.sum | 2 ++ update/main.go | 13 +++++++++---- update/update_test.go | 16 ++++++++++++++++ 4 files changed, 28 insertions(+), 4 deletions(-) diff --git a/go.mod b/go.mod index 4613f65..d51d4f1 100644 --- a/go.mod +++ b/go.mod @@ -5,5 +5,6 @@ go 1.16 require ( github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 github.com/spf13/cobra v1.3.0 + golang.org/x/mod v0.5.1 gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b ) diff --git a/go.sum b/go.sum index 8539993..2083c7d 100644 --- a/go.sum +++ b/go.sum @@ -387,6 +387,8 @@ golang.org/x/mod v0.4.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.4.1/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.5.0/go.mod h1:5OXOZSfqPIIbmVBIIKWRFfZjPR0E5r58TLhUjH0a2Ro= +golang.org/x/mod v0.5.1 h1:OJxoQ/rynoF0dcCdI7cLPktw/hR2cueqYfjm43oqK38= +golang.org/x/mod v0.5.1/go.mod h1:5OXOZSfqPIIbmVBIIKWRFfZjPR0E5r58TLhUjH0a2Ro= golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20181023162649-9b4f9f5ad519/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= diff --git a/update/main.go b/update/main.go index 3aa693e..bce0820 100644 --- a/update/main.go +++ b/update/main.go @@ -9,6 +9,8 @@ import ( "os" "runtime" "time" + + "golang.org/x/mod/semver" ) var exe, _ = os.Executable() @@ -62,15 +64,17 @@ func CheckLatestVersion() (string, []Asset, error) { return "", nil, errors.New("No release found") } - // if the current version is the same as the latest version, don't update - if Version == release.TagName { - fmt.Println("You are using the latest version") - return "", nil, errors.New("You are using the latest version") + // compare the current version, if the current version is the same or lower than the latest version, don't update + versions := []string{Version, release.TagName} + semver.Sort(versions) + if versions[1] == Version { + return "", nil, errors.New("Current version is the latest version") } return release.TagName, release.Assets, nil } +// DownloadLatestVersion will download the latest version of katenary. func DownloadLatestVersion(assets []Asset) error { // Download the latest version fmt.Println("Downloading the latest version...") @@ -118,6 +122,7 @@ func DownloadLatestVersion(assets []Asset) error { return err } +// DownloadFile will download a url to a local file. It also ensure that the file is executable. func DownloadFile(url, exe string) error { // Download the url binary to exe path resp, err := http.Get(url) diff --git a/update/update_test.go b/update/update_test.go index ec97e8a..0627958 100644 --- a/update/update_test.go +++ b/update/update_test.go @@ -34,3 +34,19 @@ func TestDownloadLatestRelease(t *testing.T) { t.Errorf("Error: %s", err) } } + +func TestAlreadyUpToDate(t *testing.T) { + Version = "99999.999.99" + exe = "/tmp/test-katenary" + defer os.Remove(exe) + + // Call the version check + version, _, err := CheckLatestVersion() + + if err == nil { + t.Errorf("Error: %s", err) + } + + t.Log("Version is already the most recent", version) + +}