forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cc_api_version.go
79 lines (65 loc) · 1.89 KB
/
cc_api_version.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package requirements
import (
"strconv"
"strings"
"github.com/cloudfoundry/cli/cf"
"github.com/cloudfoundry/cli/cf/configuration/core_config"
. "github.com/cloudfoundry/cli/cf/i18n"
"github.com/cloudfoundry/cli/cf/terminal"
)
type CCApiVersionRequirement struct {
ui terminal.UI
config core_config.Reader
commandName string
major int
minor int
patch int
}
func NewCCApiVersionRequirement(ui terminal.UI, config core_config.Reader, commandName string, major, minor, patch int) CCApiVersionRequirement {
return CCApiVersionRequirement{ui, config, commandName, major, minor, patch}
}
func (req CCApiVersionRequirement) Execute() bool {
versions := strings.Split(req.config.ApiVersion(), ".")
if len(versions) != 3 {
return true
}
majorStr := versions[0]
major, err := strconv.Atoi(majorStr)
if err != nil {
return true
}
minorStr := versions[1]
minor, err := strconv.Atoi(minorStr)
if err != nil {
return true
}
patchStr := versions[2]
patch, err := strconv.Atoi(patchStr)
if err != nil {
return true
}
if major > req.major {
return true
} else if major < req.major {
return false
}
if minor > req.minor {
return true
} else if minor < req.minor {
return false
}
if patch >= req.patch {
return true
}
req.ui.Say(terminal.FailureColor(T("FAILED")))
req.ui.Say(T("Current CF CLI version {{.Version}}", map[string]interface{}{"Version": cf.Version}))
req.ui.Say(T("Current CF API version {{.ApiVersion}}", map[string]interface{}{"ApiVersion": req.config.ApiVersion()}))
req.ui.Say(T("To use the {{.CommandName}} feature, you need to upgrade the CF API to at least {{.MinApiVersionMajor}}.{{.MinApiVersionMinor}}.{{.MinApiVersionPatch}}",
map[string]interface{}{
"CommandName": req.commandName,
"MinApiVersionMajor": req.major,
"MinApiVersionMinor": req.minor,
"MinApiVersionPatch": req.patch,
}))
return false
}