forked from leonlee/drone-exec
/
types_yaml.go
250 lines (212 loc) · 5.13 KB
/
types_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
package yaml
import (
"fmt"
"strings"
"github.com/flynn/go-shlex"
"gopkg.in/yaml.v2"
)
type Command struct {
parts []string
}
func (s *Command) UnmarshalYAML(unmarshal func(interface{}) error) error {
var stringType string
err := unmarshal(&stringType)
if err == nil {
s.parts, err = shlex.Split(stringType)
return err
}
var sliceType []string
err = unmarshal(&sliceType)
if err == nil {
s.parts = sliceType
return nil
}
return err
}
func (s *Command) Slice() []string {
return s.parts
}
type MapEqualSlice struct {
parts []string
}
func (s *MapEqualSlice) UnmarshalYAML(unmarshal func(interface{}) error) error {
err := unmarshal(&s.parts)
if err == nil {
return nil
}
var mapType map[string]string
err = unmarshal(&mapType)
if err != nil {
return err
}
for k, v := range mapType {
s.parts = append(s.parts, strings.Join([]string{k, v}, "="))
}
return nil
}
func (s *MapEqualSlice) Slice() []string {
return s.parts
}
// Stringorslice represents a string or an array of strings.
// TODO use docker/docker/pkg/stringutils.StrSlice once 1.9.x is released.
type Stringorslice struct {
parts []string
}
// MarshalYAML implements the Marshaller interface.
func (s Stringorslice) MarshalYAML() (interface{}, error) {
return s.parts, nil
}
// UnmarshalYAML implements the Unmarshaller interface.
func (s *Stringorslice) UnmarshalYAML(unmarshal func(interface{}) error) error {
var sliceType []string
err := unmarshal(&sliceType)
if err == nil {
s.parts = sliceType
return nil
}
var stringType string
err = unmarshal(&stringType)
if err == nil {
sliceType = make([]string, 0, 1)
s.parts = append(sliceType, string(stringType))
return nil
}
return err
}
// Len returns the number of parts of the Stringorslice.
func (s *Stringorslice) Len() int {
if s == nil {
return 0
}
return len(s.parts)
}
// Slice gets the parts of the StrSlice as a Slice of string.
func (s *Stringorslice) Slice() []string {
if s == nil {
return nil
}
return s.parts
}
// Pluginslice is a slice of Plugins with a custom Yaml
// unarmshal function to preserve ordering.
type Pluginslice struct {
parts []Plugin
}
func (s *Pluginslice) UnmarshalYAML(unmarshal func(interface{}) error) error {
// unmarshal the yaml into the generic
// mapSlice type to preserve ordering.
obj := yaml.MapSlice{}
err := unmarshal(&obj)
if err != nil {
return err
}
// unarmshals each item in the mapSlice,
// unmarshal and append to the slice.
err = unmarshalYaml(obj, func(key string, val []byte) error {
plugin := Plugin{}
err := yaml.Unmarshal(val, &plugin)
if err != nil {
return err
}
if len(plugin.Image) == 0 {
plugin.Image = key
}
s.parts = append(s.parts, plugin)
return nil
})
return err
}
func (s *Pluginslice) Slice() []Plugin {
return s.parts
}
// ContainerSlice is a slice of Containers with a custom
// Yaml unarmshal function to preserve ordering.
type Containerslice struct {
parts []Container
}
func (s *Containerslice) UnmarshalYAML(unmarshal func(interface{}) error) error {
// unmarshal the yaml into the generic
// mapSlice type to preserve ordering.
obj := yaml.MapSlice{}
err := unmarshal(&obj)
if err != nil {
return err
}
// unarmshals each item in the mapSlice,
// unmarshal and append to the slice.
return unmarshalYaml(obj, func(key string, val []byte) error {
ctr := Container{}
err := yaml.Unmarshal(val, &ctr)
if err != nil {
return err
}
if len(ctr.Image) == 0 {
ctr.Image = key
}
s.parts = append(s.parts, ctr)
return nil
})
}
func (s *Containerslice) Slice() []Container {
return s.parts
}
// BuildStep holds the build step configuration using a custom
// Yaml unarmshal function to preserve ordering.
type BuildStep struct {
parts []Build
}
func (s *BuildStep) UnmarshalYAML(unmarshal func(interface{}) error) error {
build := Build{}
err := unmarshal(&build)
if err != nil {
return err
}
if build.Image != "" {
s.parts = append(s.parts, build)
return nil
}
// unmarshal the yaml into the generic
// mapSlice type to preserve ordering.
obj := yaml.MapSlice{}
if err := unmarshal(&obj); err != nil {
return err
}
// unarmshals each item in the mapSlice,
// unmarshal and append to the slice.
return unmarshalYaml(obj, func(key string, val []byte) error {
build := Build{}
err := yaml.Unmarshal(val, &build)
if err != nil {
return err
}
s.parts = append(s.parts, build)
return nil
})
}
func (s *BuildStep) Slice() []Build {
return s.parts
}
// emitter defines the callback function used for
// generic yaml parsing. It emits back a raw byte
// slice for custom unmarshalling into a structure.
type unmarshal func(string, []byte) error
// unmarshalYaml is a helper function that removes
// some of the boilerplate from unmarshalling
// complex map slices.
func unmarshalYaml(v yaml.MapSlice, emit unmarshal) error {
for _, vv := range v {
// re-marshal the interface{} back to
// a raw yaml value
val, err := yaml.Marshal(&vv.Value)
if err != nil {
return err
}
key := fmt.Sprintf("%v", vv.Key)
// unmarshal the raw value using the
// callback function.
if err := emit(key, val); err != nil {
return err
}
}
return nil
}