Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
"syscall"
"time"

"github.com/ava-labs/avalanche-cli/pkg/version"

"github.com/ava-labs/avalanche-cli/cmd/blockchaincmd"
"github.com/ava-labs/avalanche-cli/cmd/configcmd"
"github.com/ava-labs/avalanche-cli/cmd/contractcmd"
Expand Down Expand Up @@ -144,7 +146,9 @@ func createApp(cmd *cobra.Command, _ []string) error {
if err := checkForUpdates(cmd, app); err != nil {
return err
}

if err := version.CheckCLIVersionIsOverMin(app, Version); err != nil {
return err
}
return nil
}

Expand Down
1 change: 1 addition & 0 deletions pkg/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ const (
AvalancheGoCompatibilityURL = "https://raw.githubusercontent.com/ava-labs/avalanchego/master/version/compatibility.json"
SubnetEVMRPCCompatibilityURL = "https://raw.githubusercontent.com/ava-labs/subnet-evm/master/compatibility.json"
CLILatestDependencyURL = "https://raw.githubusercontent.com/ava-labs/avalanche-cli/main/versions/latest.json"
CLIMinVersionURL = "https://raw.githubusercontent.com/ava-labs/avalanche-cli/main/versions/min_cli_version.json"

YesLabel = "Yes"
NoLabel = "No"
Expand Down
45 changes: 45 additions & 0 deletions pkg/version/min_cli_version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright (C) 2022, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package version

import (
"encoding/json"
"fmt"
"strings"

"golang.org/x/mod/semver"

"github.com/ava-labs/avalanche-cli/pkg/application"
"github.com/ava-labs/avalanche-cli/pkg/constants"
)

type CLIMinVersionMap struct {
MinVersion string `json:"min-version"`
}

func CheckCLIVersionIsOverMin(app *application.Avalanche, version string) error {
minVersionBytes, err := app.Downloader.Download(constants.CLIMinVersionURL)
if err != nil {
return err
}

var parsedMinVersion CLIMinVersionMap
if err = json.Unmarshal(minVersionBytes, &parsedMinVersion); err != nil {
return err
}

minVersion := parsedMinVersion.MinVersion
// Add 'v' prefix if missing
if !strings.HasPrefix(minVersion, "v") {
minVersion = "v" + minVersion
}
if !strings.HasPrefix(version, "v") {
version = "v" + version
}

versionComparison := semver.Compare(version, minVersion)
if versionComparison == -1 {
return fmt.Errorf("CLI version is required to be at least %s, current CLI version is %s, please upgrade CLI by calling `curl -sSfL https://raw.githubusercontent.com/ava-labs/avalanche-cli/main/scripts/install.sh | sh -s`", minVersion, version)
}
return nil
}
73 changes: 73 additions & 0 deletions pkg/version/min_cli_version_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// Copyright (C) 2022, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

package version

import (
"testing"

"github.com/ava-labs/avalanche-cli/internal/mocks"
"github.com/ava-labs/avalanche-cli/pkg/application"
"github.com/ava-labs/avalanche-cli/pkg/constants"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
)

func TestCheckMinDependencyVersion(t *testing.T) {
tests := []struct {
name string
cliVersion string
expectedError bool
cliDependencyData []byte
}{
{
name: "cli version equal to minimum version",
cliVersion: "1.8.10",
cliDependencyData: []byte(`{"min-version":"1.8.10"}`),
expectedError: false,
},
{
name: "cli version higher than minimum version",
cliVersion: "1.8.11",
cliDependencyData: []byte(`{"min-version":"1.8.10"}`),
expectedError: false,
},
{
name: "cli version lower than minimum version",
cliVersion: "1.8.9",
cliDependencyData: []byte(`{"min-version":"1.8.10"}`),
expectedError: true,
},
{
name: "cli version much higher than minimum version",
cliVersion: "1.13.0",
cliDependencyData: []byte(`{"min-version":"1.8.10"}`),
expectedError: false,
},
{
name: "cli version much lower than minimum version",
cliVersion: "1.7.0",
cliDependencyData: []byte(`{"min-version":"1.8.10"}`),
expectedError: true,
},
}

for _, tt := range tests {
mockDownloader := &mocks.Downloader{}
mockDownloader.On("Download", mock.MatchedBy(func(url string) bool {
return url == constants.CLIMinVersionURL
})).Return(tt.cliDependencyData, nil)

app := application.New()
app.Downloader = mockDownloader

t.Run(tt.name, func(t *testing.T) {
err := CheckCLIVersionIsOverMin(app, tt.cliVersion)
if tt.expectedError {
require.Error(t, err)
} else {
require.NoError(t, err)
}
})
}
}
3 changes: 3 additions & 0 deletions versions/min_cli_version.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"min-version": "1.8.10"
}
Loading