-
Notifications
You must be signed in to change notification settings - Fork 117
Check that CLI is at least specified min version #2832
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
ebf89e8
cli min version
sukantoraymond c4a0a86
add test
sukantoraymond 2534917
we try with higher version
sukantoraymond a84c1e6
we try with same version
sukantoraymond 4f6f737
lint
sukantoraymond 8f79fad
Merge branch 'main' into cli-min-version
sukantoraymond 4aaa3fc
Merge branch 'main' into cli-min-version
sukantoraymond 8185c53
Merge branch 'main' into cli-min-version
sukantoraymond ef44404
address comments
sukantoraymond File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | ||
| } | ||
| }) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| { | ||
| "min-version": "1.8.10" | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you also add
avalanche updateto the upgrade instructions?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this as the first option, and the curl as second one
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
addreessed