-
Notifications
You must be signed in to change notification settings - Fork 118
Expand file tree
/
Copy pathmin_cli_version.go
More file actions
45 lines (37 loc) · 1.24 KB
/
min_cli_version.go
File metadata and controls
45 lines (37 loc) · 1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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
}