-
Notifications
You must be signed in to change notification settings - Fork 19
/
template_yaml.go
467 lines (368 loc) · 9.43 KB
/
template_yaml.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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
package dockerfilegenerator
import (
"errors"
"fmt"
"gopkg.in/yaml.v3"
"io/ioutil"
"strings"
)
// DockerfileDataYaml is used to decode yaml data. It has a Stages map instead of a slice for that purpose.
type DockerfileDataYaml struct {
Stages map[string]Stage `yaml:"stages"`
}
type yamlMapStringInterface map[string]interface{}
type yamlMapInterfaceInterface map[interface{}]interface{}
func errorStringWithType(value interface{}) string {
return fmt.Sprintf("Yaml contains an expected data, caused by %[1]v, type: %[1]T\n", value)
}
func ensureMapInterfaceInterface(value interface{}) map[interface{}]interface{} {
v, ok := value.(map[interface{}]interface{})
if !ok {
panic(errorStringWithType(value))
}
return v
}
func ensureMapStringInterface(value interface{}) map[string]interface{} {
v, ok := value.(map[string]interface{})
if !ok {
panic(errorStringWithType(value))
}
return v
}
func ensureMapString(value interface{}) string {
v, ok := value.(string)
if !ok {
panic(errorStringWithType(value))
}
return v
}
func convertMapIIToMapSS(mapInterface map[interface{}]interface{}) map[string]string {
mapString := make(map[string]string)
for key, value := range mapInterface {
strKey := fmt.Sprintf("%v", key)
strValue := fmt.Sprintf("%v", value)
mapString[strKey] = strValue
}
return mapString
}
func convertMapSIToMapSS(mapInterface yamlMapStringInterface) map[string]string {
mapString := make(map[string]string)
for key, value := range mapInterface {
strKey := fmt.Sprintf("%v", key)
strValue := fmt.Sprintf("%v", value)
mapString[strKey] = strValue
}
return mapString
}
func convertSliceInterfaceToString(s interface{}) ([]string, error) {
slice, ok := s.([]interface{})
if !ok {
return nil, errors.New("Invalid type, can't cast interface{} to []interface{}")
}
res := make([]string, len(slice))
for i, value := range slice {
res[i] = fmt.Sprintf("%v", value)
}
return res, nil
}
func cleanUpFrom(value yamlMapStringInterface) From {
v := convertMapSIToMapSS(value)
var from From
if v["image"] != "" {
from.Image = v["image"]
}
if v["as"] != "" {
from.As = v["as"]
}
return from
}
func cleanUpArg(value yamlMapInterfaceInterface) Arg {
v := convertMapIIToMapSS(value)
var arg Arg
if v["name"] != "" {
arg.Name = v["name"]
}
if v["value"] != "" {
arg.Value = v["value"]
}
if v["test"] == "true" || v["test"] == "yes" {
arg.Test = true
}
if v["envVariable"] == "true" || v["envVariable"] == "yes" {
arg.EnvVariable = true
}
return arg
}
func cleanUpLabel(value yamlMapStringInterface) Label {
v := convertMapSIToMapSS(value)
var l Label
if v["name"] != "" {
l.Name = v["name"]
}
if v["value"] != "" {
l.Value = v["value"]
}
return l
}
func cleanUpVolume(value yamlMapStringInterface) Volume {
v := convertMapSIToMapSS(value)
var vlm Volume
if v["source"] != "" {
vlm.Source = v["source"]
}
if v["destination"] != "" {
vlm.Destination = v["destination"]
}
return vlm
}
func cleanUpRunCommand(value yamlMapInterfaceInterface) RunCommand {
var r RunCommand
v := convertMapIIToMapSS(value)
params, err := convertSliceInterfaceToString(value["params"])
if err != nil {
panic("Failed to parse run instruction params!")
}
r.Params = params
r.RunForm = RunCommandDefaultRunForm
if v["runForm"] == "exec" {
r.RunForm = ExecForm
} else if v["runForm"] == "shell" {
r.RunForm = ShellForm
}
return r
}
func cleanUpEnvVariable(value yamlMapStringInterface) EnvVariable {
v := convertMapSIToMapSS(value)
var e EnvVariable
if v["name"] != "" {
e.Name = v["name"]
}
if v["value"] != "" {
e.Value = v["value"]
}
return e
}
func cleanUpCopyCommand(value yamlMapInterfaceInterface) CopyCommand {
var c CopyCommand
v := convertMapIIToMapSS(value)
params, err := convertSliceInterfaceToString(value["sources"])
if err != nil {
panic("Failed to parse copy instruction sources!")
}
c.Sources = params
if v["destination"] != "" {
c.Destination = v["destination"]
}
if v["chown"] != "" {
c.Chown = v["chown"]
}
if v["from"] != "" {
c.From = v["from"]
}
return c
}
func cleanUpCmd(value yamlMapInterfaceInterface) Cmd {
var c Cmd
v := convertMapIIToMapSS(value)
params, err := convertSliceInterfaceToString(value["params"])
if err != nil {
panic("Failed to parse cmd instruction params!")
}
c.Params = params
c.RunForm = CmdDefaultRunForm
if v["runForm"] == "exec" {
c.RunForm = ExecForm
} else if v["runForm"] == "shell" {
c.RunForm = ShellForm
}
return c
}
func cleanUpEntrypoint(value yamlMapInterfaceInterface) Entrypoint {
var e Entrypoint
v := convertMapIIToMapSS(value)
params, err := convertSliceInterfaceToString(value["params"])
if err != nil {
panic("Failed to parse entrypoint instruction params!")
}
e.Params = params
e.RunForm = EntrypointDefaultRunForm
if v["runForm"] == "exec" {
e.RunForm = ExecForm
} else if v["runForm"] == "shell" {
e.RunForm = ShellForm
}
return e
}
func cleanUpOnbuild(value yamlMapInterfaceInterface) Onbuild {
var o Onbuild
params, err := convertSliceInterfaceToString(value["params"])
if err != nil {
panic("Failed to parse onBuild instruction params!")
}
o.Params = params
return o
}
func cleanUpHealthCheck(value yamlMapInterfaceInterface) HealthCheck {
var h HealthCheck
params, err := convertSliceInterfaceToString(value["params"])
if err != nil {
panic("Failed to parse healthCheck instruction params!")
}
h.Params = params
return h
}
func cleanUpShell(value yamlMapInterfaceInterface) Shell {
var s Shell
params, err := convertSliceInterfaceToString(value["params"])
if err != nil {
panic("Failed to parse shell instruction params!")
}
s.Params = params
return s
}
func cleanUpWorkdir(value yamlMapStringInterface) Workdir {
v := convertMapSIToMapSS(value)
var w Workdir
if v["dir"] != "" {
w.Dir = v["dir"]
}
return w
}
func cleanUpUserString(value string) User {
return User{User: value}
}
func cleanUpUserMap(value yamlMapStringInterface) User {
v := convertMapSIToMapSS(value)
var u User
if v["user"] != "" {
u.User = v["user"]
}
if v["group"] != "" {
u.Group = v["group"]
}
return u
}
func cleanUpInterfaceArray(in []interface{}) []Instruction {
result := make([]Instruction, len(in))
for i, v := range in {
result[i] = cleanUpMapValue(v)
}
return result
}
func cleanUpMapSI(in map[string]interface{}) Instruction {
for key, value := range in {
switch key {
case "user":
v := ensureMapString(value)
return cleanUpUserString(v)
}
}
panic("Unknown instruction in yaml!")
}
func cleanUpMapIISimpleInstructions(instructionName string, value interface{}) Instruction {
v := ensureMapStringInterface(value)
switch strings.ToLower(instructionName) {
case "from":
return cleanUpFrom(v)
case "label":
return cleanUpLabel(v)
case "volume":
return cleanUpVolume(v)
case "envvariable":
return cleanUpEnvVariable(v)
case "workdir":
return cleanUpWorkdir(v)
case "user":
return cleanUpUserMap(v)
}
panic(errorStringWithType(value))
}
func cleanUpMapIIComplexInstructions(instructionName string, value interface{}) Instruction {
v := ensureMapInterfaceInterface(value)
switch strings.ToLower(instructionName) {
case "healthcheck":
return cleanUpHealthCheck(v)
case "onbuild":
return cleanUpOnbuild(v)
case "entrypoint":
return cleanUpEntrypoint(v)
case "cmd":
return cleanUpCmd(v)
case "copy":
return cleanUpCopyCommand(v)
case "arg":
return cleanUpArg(v)
case "run":
return cleanUpRunCommand(v)
case "shell":
return cleanUpShell(v)
}
panic(errorStringWithType(value))
}
func cleanUpMapII(in map[interface{}]interface{}) Instruction {
for key, value := range in {
switch value.(type) {
case map[string]interface{}:
key, ok := key.(string)
if !ok {
panic(errorStringWithType(key))
}
return cleanUpMapIISimpleInstructions(key, value)
case map[interface{}]interface{}:
key, ok := key.(string)
if !ok {
panic(errorStringWithType(key))
}
return cleanUpMapIIComplexInstructions(key, value)
}
}
panic("Unknown instruction in yaml!")
}
func cleanUpMapValue(v interface{}) Instruction {
switch v := v.(type) {
case map[string]interface{}:
return cleanUpMapSI(v)
case map[interface{}]interface{}:
return cleanUpMapII(v)
default:
panic("Invalid instruction type in yaml!")
}
}
func unmarshallYamlFile(filename string, node *yaml.Node) error {
yamlFile, err := ioutil.ReadFile(filename)
if err != nil {
return fmt.Errorf("yamlFile.Get err #%v", err)
}
err = yaml.Unmarshal(yamlFile, node)
if err != nil {
return fmt.Errorf("Unmarshal: %v", err)
}
return nil
}
func getStagesOrderFromYamlNode(node *yaml.Node) ([]string, error) {
var stages []string
if node.Kind != yaml.MappingNode {
return nil, errors.New("Yaml should contain a map that contains 'stages' key!")
}
stagesKeyNode := node.Content[0]
if stagesKeyNode.Kind != yaml.ScalarNode {
return nil, errors.New("Yaml should contain a 'stages' key!")
}
stagesMapNode := node.Content[1]
if stagesMapNode.Kind != yaml.MappingNode {
return nil, errors.New("Yaml should contain a 'stages' map that has stage names as keys!")
}
for i, stage := range stagesMapNode.Content {
if i%2 == 0 {
if stage.Kind != yaml.ScalarNode {
return nil, errors.New("Yaml should contain stage keys in 'staging' map")
}
stages = append(stages, stage.Value)
} else {
if stage.Kind != yaml.SequenceNode {
return nil, errors.New("Yaml should contain stage sequences in 'staging' map")
}
}
}
return stages, nil
}