This repository has been archived by the owner on Jun 16, 2021. It is now read-only.
/
build.go
116 lines (107 loc) · 2.76 KB
/
build.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
package yaml
import (
"errors"
"fmt"
"strconv"
"strings"
)
// Build represents a build element in compose file.
// It can take multiple form in the compose file, hence this special type
type Build struct {
Context string
Dockerfile string
Args map[string]string
}
// MarshalYAML implements the Marshaller interface.
func (b Build) MarshalYAML() (interface{}, error) {
m := map[string]interface{}{}
if b.Context != "" {
m["context"] = b.Context
}
if b.Dockerfile != "" {
m["dockerfile"] = b.Dockerfile
}
if len(b.Args) > 0 {
m["args"] = b.Args
}
return m, nil
}
// UnmarshalYAML implements the Unmarshaller interface.
func (b *Build) UnmarshalYAML(unmarshal func(interface{}) error) error {
var stringType string
if err := unmarshal(&stringType); err == nil {
b.Context = stringType
return nil
}
var mapType map[interface{}]interface{}
if err := unmarshal(&mapType); err == nil {
for mapKey, mapValue := range mapType {
switch mapKey {
case "context":
b.Context = mapValue.(string)
case "dockerfile":
b.Dockerfile = mapValue.(string)
case "args":
args, err := handleBuildArgs(mapValue)
if err != nil {
return err
}
b.Args = args
default:
// Ignore unknown keys
continue
}
}
return nil
}
return errors.New("Failed to unmarshal Build")
}
func handleBuildArgs(value interface{}) (map[string]string, error) {
var args map[string]string
switch v := value.(type) {
case map[interface{}]interface{}:
return handleBuildArgMap(v)
case []interface{}:
return handleBuildArgSlice(v)
default:
return args, fmt.Errorf("Failed to unmarshal Build args: %#v", value)
}
}
func handleBuildArgSlice(s []interface{}) (map[string]string, error) {
var args = map[string]string{}
for _, arg := range s {
// check if a value is provided
switch v := strings.SplitN(arg.(string), "=", 2); len(v) {
case 1:
// if we have not specified a a value for this build arg, we assign it an ascii null value and query the environment
// later when we build the service
args[v[0]] = "\x00"
case 2:
// if we do have a value provided, we use it
args[v[0]] = v[1]
}
}
return args, nil
}
func handleBuildArgMap(m map[interface{}]interface{}) (map[string]string, error) {
args := map[string]string{}
for mapKey, mapValue := range m {
var argValue string
name, ok := mapKey.(string)
if !ok {
return args, fmt.Errorf("Cannot unmarshal '%v' to type %T into a string value", name, name)
}
switch a := mapValue.(type) {
case string:
argValue = a
case int:
argValue = strconv.Itoa(a)
case int64:
argValue = strconv.Itoa(int(a))
default:
return args, fmt.Errorf("Cannot unmarshal '%v' to type %T into a string value", mapValue, mapValue)
}
args[name] = argValue
}
return args, nil
}