forked from cloudfoundry/cli
/
api_version_warning.go
64 lines (51 loc) · 1.49 KB
/
api_version_warning.go
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package command
import (
"code.cloudfoundry.org/cli/command/v2/constant"
"code.cloudfoundry.org/cli/version"
"github.com/blang/semver"
)
func WarnCLIVersionCheck(config Config, ui UI) error {
minVer := config.MinCLIVersion()
currentVer := config.BinaryVersion()
isOutdated, err := checkVersionOutdated(currentVer, minVer)
if err != nil {
return err
}
if isOutdated {
ui.DisplayWarning("Cloud Foundry API version {{.APIVersion}} requires CLI version {{.MinCLIVersion}}. You are currently on version {{.BinaryVersion}}. To upgrade your CLI, please visit: https://github.com/cloudfoundry/cli#downloads",
map[string]interface{}{
"APIVersion": config.APIVersion(),
"MinCLIVersion": minVer,
"BinaryVersion": currentVer,
})
ui.DisplayNewline()
}
return nil
}
func WarnAPIVersionCheck(apiVersion string, ui UI) error {
isOutdated, err := checkVersionOutdated(apiVersion, constant.MinimumAPIVersion)
if err != nil {
return err
}
if isOutdated {
ui.DisplayWarning("Your API version is no longer supported. Upgrade to a newer version of the API.")
}
return nil
}
func checkVersionOutdated(current string, minimum string) (bool, error) {
if current == version.DefaultVersion || minimum == "" {
return false, nil
}
currentSemvar, err := semver.Make(current)
if err != nil {
return false, err
}
minimumSemvar, err := semver.Make(minimum)
if err != nil {
return false, err
}
if currentSemvar.Compare(minimumSemvar) == -1 {
return true, nil
}
return false, nil
}