forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
manifest.go
352 lines (310 loc) · 9.3 KB
/
manifest.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
package manifest
import (
"fmt"
. "github.com/cloudfoundry/cli/cf/i18n"
"path/filepath"
"regexp"
"strconv"
"strings"
"github.com/cloudfoundry/cli/cf/errors"
"github.com/cloudfoundry/cli/cf/formatters"
"github.com/cloudfoundry/cli/cf/models"
"github.com/cloudfoundry/cli/generic"
"github.com/cloudfoundry/cli/words"
)
type Manifest struct {
Path string
Data generic.Map
}
func NewEmptyManifest() (m *Manifest) {
return &Manifest{Data: generic.NewMap()}
}
func (m Manifest) Applications() (apps []models.AppParams, err error) {
rawData, errs := expandProperties(m.Data, words.NewWordGenerator())
if len(errs) > 0 {
err = errors.NewWithSlice(errs)
return
}
data := generic.NewMap(rawData)
appMaps, errs := m.getAppMaps(data)
if len(errs) > 0 {
err = errors.NewWithSlice(errs)
return
}
for _, appMap := range appMaps {
app, errs := mapToAppParams(filepath.Dir(m.Path), appMap)
if len(errs) > 0 {
err = errors.NewWithSlice(errs)
continue
}
apps = append(apps, app)
}
return
}
func (m Manifest) getAppMaps(data generic.Map) (apps []generic.Map, errs []error) {
globalProperties := data.Except([]interface{}{"applications"})
if data.Has("applications") {
appMaps, ok := data.Get("applications").([]interface{})
if !ok {
errs = append(errs, errors.New(T("Expected applications to be a list")))
return
}
for _, appData := range appMaps {
if !generic.IsMappable(appData) {
errs = append(errs, errors.NewWithFmt(T("Expected application to be a list of key/value pairs\nError occurred in manifest near:\n'{{.YmlSnippet}}'",
map[string]interface{}{"YmlSnippet": appData})))
continue
}
appMap := generic.DeepMerge(globalProperties, generic.NewMap(appData))
apps = append(apps, appMap)
}
} else {
apps = append(apps, globalProperties)
}
return
}
var propertyRegex = regexp.MustCompile(`\${[\w-]+}`)
func expandProperties(input interface{}, babbler words.WordGenerator) (output interface{}, errs []error) {
switch input := input.(type) {
case string:
match := propertyRegex.FindStringSubmatch(input)
if match != nil {
if match[0] == "${random-word}" {
output = strings.Replace(input, "${random-word}", strings.ToLower(babbler.Babble()), -1)
} else {
err := errors.NewWithFmt(T("Property '{{.PropertyName}}' found in manifest. This feature is no longer supported. Please remove it and try again.",
map[string]interface{}{"PropertyName": match[0]}))
errs = append(errs, err)
}
} else {
output = input
}
case []interface{}:
outputSlice := make([]interface{}, len(input))
for index, item := range input {
itemOutput, itemErrs := expandProperties(item, babbler)
outputSlice[index] = itemOutput
errs = append(errs, itemErrs...)
}
output = outputSlice
case map[interface{}]interface{}:
outputMap := make(map[interface{}]interface{})
for key, value := range input {
itemOutput, itemErrs := expandProperties(value, babbler)
outputMap[key] = itemOutput
errs = append(errs, itemErrs...)
}
output = outputMap
case generic.Map:
outputMap := generic.NewMap()
generic.Each(input, func(key, value interface{}) {
itemOutput, itemErrs := expandProperties(value, babbler)
outputMap.Set(key, itemOutput)
errs = append(errs, itemErrs...)
})
output = outputMap
default:
output = input
}
return
}
func mapToAppParams(basePath string, yamlMap generic.Map) (appParams models.AppParams, errs []error) {
errs = checkForNulls(yamlMap)
if len(errs) > 0 {
return
}
appParams.BuildpackUrl = stringValOrDefault(yamlMap, "buildpack", &errs)
appParams.DiskQuota = bytesVal(yamlMap, "disk_quota", &errs)
appParams.Domain = stringVal(yamlMap, "domain", &errs)
appParams.Host = stringVal(yamlMap, "host", &errs)
appParams.Name = stringVal(yamlMap, "name", &errs)
appParams.Path = stringVal(yamlMap, "path", &errs)
appParams.StackName = stringVal(yamlMap, "stack", &errs)
appParams.Command = stringValOrDefault(yamlMap, "command", &errs)
appParams.Memory = bytesVal(yamlMap, "memory", &errs)
appParams.InstanceCount = intVal(yamlMap, "instances", &errs)
appParams.HealthCheckTimeout = intVal(yamlMap, "timeout", &errs)
appParams.NoRoute = boolVal(yamlMap, "no-route", &errs)
appParams.UseRandomHostname = boolVal(yamlMap, "random-route", &errs)
appParams.ServicesToBind = sliceOrEmptyVal(yamlMap, "services", &errs)
appParams.EnvironmentVars = envVarOrEmptyMap(yamlMap, &errs)
if appParams.Path != nil {
path := *appParams.Path
if filepath.IsAbs(path) {
path = filepath.Clean(path)
} else {
path = filepath.Join(basePath, path)
}
appParams.Path = &path
}
return
}
func checkForNulls(yamlMap generic.Map) (errs []error) {
generic.Each(yamlMap, func(key interface{}, value interface{}) {
if key == "command" || key == "buildpack" {
return
}
if value == nil {
errs = append(errs, errors.NewWithFmt(T("{{.PropertyName}} should not be null", map[string]interface{}{"PropertyName": key})))
}
})
return
}
func stringVal(yamlMap generic.Map, key string, errs *[]error) *string {
val := yamlMap.Get(key)
if val == nil {
return nil
}
result, ok := val.(string)
if !ok {
*errs = append(*errs, errors.NewWithFmt(T("{{.PropertyName}} must be a string value", map[string]interface{}{"PropertyName": key})))
return nil
}
return &result
}
func stringValOrDefault(yamlMap generic.Map, key string, errs *[]error) *string {
if !yamlMap.Has(key) {
return nil
}
empty := ""
switch val := yamlMap.Get(key).(type) {
case string:
if val == "default" {
return &empty
} else {
return &val
}
case nil:
return &empty
default:
*errs = append(*errs, errors.NewWithFmt(T("{{.PropertyName}} must be a string or null value", map[string]interface{}{"PropertyName": key})))
return nil
}
}
func bytesVal(yamlMap generic.Map, key string, errs *[]error) *int64 {
yamlVal := yamlMap.Get(key)
if yamlVal == nil {
return nil
}
value, err := formatters.ToMegabytes(yamlVal.(string))
if err != nil {
*errs = append(*errs, errors.NewWithFmt(T("Unexpected value for {{.PropertyName}} :\n{{.Error}}",
map[string]interface{}{"PropertyName": key, "Error": err.Error()})))
return nil
}
return &value
}
func intVal(yamlMap generic.Map, key string, errs *[]error) *int {
var (
intVal int
err error
)
switch val := yamlMap.Get(key).(type) {
case string:
intVal, err = strconv.Atoi(val)
case int:
intVal = val
case int64:
intVal = int(val)
case nil:
return nil
default:
err = errors.NewWithFmt(T("Expected {{.PropertyName}} to be a number, but it was a {{.PropertyType}}.",
map[string]interface{}{"PropertyName": key, "PropertyType": val}))
}
if err != nil {
*errs = append(*errs, err)
return nil
}
return &intVal
}
func boolVal(yamlMap generic.Map, key string, errs *[]error) bool {
switch val := yamlMap.Get(key).(type) {
case nil:
return false
case bool:
return val
case string:
return val == "true"
default:
*errs = append(*errs, errors.NewWithFmt(T("Expected {{.PropertyName}} to be a boolean.", map[string]interface{}{"PropertyName": key})))
return false
}
}
func sliceOrEmptyVal(yamlMap generic.Map, key string, errs *[]error) *[]string {
if !yamlMap.Has(key) {
return new([]string)
}
var (
stringSlice []string
err error
)
sliceErr := errors.NewWithFmt(T("Expected {{.PropertyName}} to be a list of strings.",
map[string]interface{}{"PropertyName": key}))
switch input := yamlMap.Get(key).(type) {
case []interface{}:
for _, value := range input {
stringValue, ok := value.(string)
if !ok {
err = sliceErr
break
}
stringSlice = append(stringSlice, stringValue)
}
default:
err = sliceErr
}
if err != nil {
*errs = append(*errs, err)
return nil
}
return &stringSlice
}
func envVarOrEmptyMap(yamlMap generic.Map, errs *[]error) *map[string]string {
key := "env"
switch envVars := yamlMap.Get(key).(type) {
case nil:
aMap := make(map[string]string, 0)
return &aMap
case map[string]interface{}:
yamlMap.Set(key, generic.NewMap(yamlMap.Get(key)))
return envVarOrEmptyMap(yamlMap, errs)
case map[interface{}]interface{}:
yamlMap.Set(key, generic.NewMap(yamlMap.Get(key)))
return envVarOrEmptyMap(yamlMap, errs)
case generic.Map:
merrs := validateEnvVars(envVars)
if merrs != nil {
*errs = append(*errs, merrs...)
return nil
}
result := make(map[string]string, envVars.Count())
generic.Each(envVars, func(key, value interface{}) {
switch value.(type) {
case string:
result[key.(string)] = value.(string)
case int64, int, int32:
result[key.(string)] = fmt.Sprintf("%d", value)
case float32, float64:
result[key.(string)] = fmt.Sprintf("%f", value)
default:
*errs = append(*errs, errors.NewWithFmt(T("Expected environment variable {{.PropertyName}} to have a string value, but it was a {{.PropertyType}}.",
map[string]interface{}{"PropertyName": key, "PropertyType": value})))
}
})
return &result
default:
*errs = append(*errs, errors.NewWithFmt(T("Expected {{.Name}} to be a set of key => value, but it was a {{.Type}}.",
map[string]interface{}{"Name": key, "Type": envVars})))
return nil
}
}
func validateEnvVars(input generic.Map) (errs []error) {
generic.Each(input, func(key, value interface{}) {
if value == nil {
errs = append(errs, errors.New(fmt.Sprintf(T("env var '{{.PropertyName}}' should not be null",
map[string]interface{}{"PropertyName": key}))))
}
})
return
}