-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathcli.go
179 lines (132 loc) · 4.05 KB
/
cli.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package cbcluster
import (
"fmt"
"log"
"os"
"strconv"
"strings"
)
func IsCommandEnabled(arguments map[string]interface{}, commandKey string) bool {
val, ok := arguments[commandKey]
if !ok {
return false
}
boolVal, ok := val.(bool)
if !ok {
return false
}
return boolVal
}
// convert from comma separated list to a string slice
func ExtractEtcdServerList(docOptParsed map[string]interface{}) []string {
rawServerList, found := docOptParsed["--etcd-servers"]
if !found {
log.Printf("ExtractEtcdServerList calling EtcdServerListFromEnv")
return EtcdServerListFromEnv(docOptParsed)
}
rawServerListStr, ok := rawServerList.(string)
if !ok {
log.Printf("ExtractEtcdServerList calling EtcdServerListFromEnv")
return EtcdServerListFromEnv(docOptParsed)
}
log.Printf("ExtractEtcdServerList returning: %v", rawServerListStr)
return strings.Split(rawServerListStr, ",")
}
func EtcdServerListFromEnv(docOptParsed map[string]interface{}) []string {
serviceName, found := docOptParsed["--k8s-service-name"]
if !found {
log.Printf("EtcdServerListFromEnv did not find --k8s-service-name")
return nil
}
hostNameEnvVar := fmt.Sprintf("%v_SERVICE_HOST", serviceName)
hostName := os.Getenv(hostNameEnvVar)
portEnvVar := fmt.Sprintf("%v_SERVICE_PORT", serviceName)
port := os.Getenv(portEnvVar)
log.Printf("Etcd hostname: %v port: %v", hostName, port)
if len(hostName) == 0 {
log.Printf("EtcdServerListFromEnv did not find ENV variable: %v", hostNameEnvVar)
return nil
}
if len(port) == 0 {
log.Printf("EtcdServerListFromEnv did not find ENV variable: %v", portEnvVar)
return nil
}
return []string{fmt.Sprintf("%v:%v", hostName, port)}
}
func ExtractUserPass(docOptParsed map[string]interface{}) (string, error) {
return ExtractStringArg(docOptParsed, "--userpass")
}
func ExtractDockerTagOrLatest(docOptParsed map[string]interface{}) string {
dockerTag, err := ExtractStringArg(docOptParsed, "--docker-tag")
if err != nil || dockerTag == "" {
return "latest"
}
return dockerTag
}
func ExtractCbVersion(docOptParsed map[string]interface{}) (string, error) {
rawVersion, err := ExtractStringArg(docOptParsed, "--version")
if err != nil {
return rawVersion, err
}
// if they passed in "latest", ignore the edition and just use the
// the "latest" docker tag, which will pull latest enterprise edition.
if rawVersion == "latest" {
return rawVersion, nil
}
rawEdition, _ := docOptParsed["--edition"]
if rawEdition == nil || rawEdition == "" {
rawEdition = "community"
}
switch rawEdition {
case "community":
rawVersion = fmt.Sprintf("community-%v", rawVersion)
case "enterprise":
rawVersion = fmt.Sprintf("enterprise-%v", rawVersion)
default:
return "", fmt.Errorf("Invalid value for edition: %v", rawEdition)
}
return rawVersion, nil
}
func ExtractIntArg(docOptParsed map[string]interface{}, argToExtract string) (int, error) {
rawVal, found := docOptParsed[argToExtract]
if !found {
return -1, fmt.Errorf("Did not find arg: %v", argToExtract)
}
stringVal, ok := rawVal.(string)
if !ok {
return -1, fmt.Errorf("Invalid type for %v", argToExtract)
}
intVal, err := strconv.ParseInt(stringVal, 10, 64)
if err != nil {
return -1, err
}
return int(intVal), nil
}
func ExtractStringArg(docOptParsed map[string]interface{}, argToExtract string) (string, error) {
rawVal, found := docOptParsed[argToExtract]
if !found {
return "", fmt.Errorf("Did not find arg: %v", argToExtract)
}
stringVal, ok := rawVal.(string)
if !ok {
return "", fmt.Errorf("Invalid type for %v", argToExtract)
}
return stringVal, nil
}
func ExtractBoolArg(docOptParsed map[string]interface{}, argToExtract string) bool {
rawVal, found := docOptParsed[argToExtract]
if !found {
return false
}
boolVal, ok := rawVal.(bool)
if !ok {
return false
}
return boolVal
}
func ExtractSkipCheckCleanState(docOptParsed map[string]interface{}) bool {
return ExtractBoolArg(docOptParsed, "--skip-clean-slate-check")
}
func ExtractNumNodes(docOptParsed map[string]interface{}) (int, error) {
return ExtractIntArg(docOptParsed, "--num-nodes")
}