forked from buildkite/agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pipeline_parser.go
272 lines (226 loc) · 7.26 KB
/
pipeline_parser.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
package agent
import (
"errors"
"fmt"
"os"
"reflect"
"strings"
"github.com/buildkite/agent/env"
"github.com/buildkite/agent/yamltojson"
"github.com/buildkite/interpolate"
// This is a fork of gopkg.in/yaml.v2 that fixes anchors with MapSlice
yaml "github.com/buildkite/yaml"
)
type PipelineParser struct {
Env *env.Environment
Filename string
Pipeline []byte
NoInterpolation bool
}
func (p PipelineParser) Parse() (*PipelineParserResult, error) {
if p.Env == nil {
p.Env = env.FromSlice(os.Environ())
}
var errPrefix string
if p.Filename == "" {
errPrefix = "Failed to parse pipeline"
} else {
errPrefix = fmt.Sprintf("Failed to parse %s", p.Filename)
}
var pipelineAsSlice []topLevelStep
var pipeline yaml.MapSlice
// We support top-level arrays of steps, so try that first
if err := yaml.Unmarshal(p.Pipeline, &pipelineAsSlice); err == nil {
var steps []interface{}
// Unwrap our custom topLevelStep types for marshaling later
for _, step := range pipelineAsSlice {
if step.MapSlice != nil {
steps = append(steps, step.MapSlice)
} else {
steps = append(steps, step.Body)
}
}
pipeline = yaml.MapSlice{
{Key: "steps", Value: steps},
}
} else if err := yaml.Unmarshal(p.Pipeline, &pipeline); err != nil {
return nil, fmt.Errorf("%s: %v", errPrefix, formatYAMLError(err))
}
if p.NoInterpolation {
return &PipelineParserResult{pipeline: pipeline}, nil
}
// Preprocess any env that are defined in the top level block and place them into env for
// later interpolation into env blocks
if item, ok := mapSliceItem("env", pipeline); ok {
if envMap, ok := item.Value.(yaml.MapSlice); ok {
if err := p.interpolateEnvBlock(envMap); err != nil {
return nil, err
}
} else {
return nil, fmt.Errorf("Expected pipeline top-level env block to be a map, got %T", item)
}
}
// Recursively go through the entire pipeline and perform environment
// variable interpolation on strings
interpolated, err := p.interpolate(pipeline)
if err != nil {
return nil, err
}
return &PipelineParserResult{pipeline: interpolated.(yaml.MapSlice)}, nil
}
func mapSliceItem(key string, s yaml.MapSlice) (yaml.MapItem, bool) {
for _, item := range s {
if k, ok := item.Key.(string); ok && k == key {
return item, true
}
}
return yaml.MapItem{}, false
}
func (p PipelineParser) interpolateEnvBlock(envMap yaml.MapSlice) error {
for _, item := range envMap {
k, ok := item.Key.(string)
if !ok {
return fmt.Errorf("Unexpected type of %T for env block key %v", item.Key, item.Key)
}
switch tv := item.Value.(type) {
case string:
interpolated, err := interpolate.Interpolate(p.Env, tv)
if err != nil {
return err
}
p.Env.Set(k, interpolated)
}
}
return nil
}
func formatYAMLError(err error) error {
return errors.New(strings.TrimPrefix(err.Error(), "yaml: "))
}
// interpolate function inspired from: https://gist.github.com/hvoecking/10772475
func (p PipelineParser) interpolate(obj interface{}) (interface{}, error) {
// Make sure there's something actually to interpolate
if obj == nil {
return nil, nil
}
// Wrap the original in a reflect.Value
original := reflect.ValueOf(obj)
// Make a copy that we'll add the new values to
copy := reflect.New(original.Type()).Elem()
err := p.interpolateRecursive(copy, original)
if err != nil {
return nil, err
}
// Remove the reflection wrapper
return copy.Interface(), nil
}
func (p PipelineParser) interpolateRecursive(copy, original reflect.Value) error {
switch original.Kind() {
// If it is a pointer we need to unwrap and call once again
case reflect.Ptr:
// To get the actual value of the original we have to call Elem()
// At the same time this unwraps the pointer so we don't end up in
// an infinite recursion
originalValue := original.Elem()
// Check if the pointer is nil
if !originalValue.IsValid() {
return nil
}
// Allocate a new object and set the pointer to it
copy.Set(reflect.New(originalValue.Type()))
// Unwrap the newly created pointer
err := p.interpolateRecursive(copy.Elem(), originalValue)
if err != nil {
return err
}
// If it is an interface (which is very similar to a pointer), do basically the
// same as for the pointer. Though a pointer is not the same as an interface so
// note that we have to call Elem() after creating a new object because otherwise
// we would end up with an actual pointer
case reflect.Interface:
// Get rid of the wrapping interface
originalValue := original.Elem()
// Check to make sure the interface isn't nil
if !originalValue.IsValid() {
return nil
}
// Create a new object. Now new gives us a pointer, but we want the value it
// points to, so we have to call Elem() to unwrap it
copyValue := reflect.New(originalValue.Type()).Elem()
err := p.interpolateRecursive(copyValue, originalValue)
if err != nil {
return err
}
copy.Set(copyValue)
// If it is a struct we interpolate each field
case reflect.Struct:
for i := 0; i < original.NumField(); i += 1 {
err := p.interpolateRecursive(copy.Field(i), original.Field(i))
if err != nil {
return err
}
}
// If it is a slice we create a new slice and interpolate each element
case reflect.Slice:
copy.Set(reflect.MakeSlice(original.Type(), original.Len(), original.Cap()))
for i := 0; i < original.Len(); i += 1 {
err := p.interpolateRecursive(copy.Index(i), original.Index(i))
if err != nil {
return err
}
}
// If it is a map we create a new map and interpolate each value
case reflect.Map:
copy.Set(reflect.MakeMap(original.Type()))
for _, key := range original.MapKeys() {
originalValue := original.MapIndex(key)
// New gives us a pointer, but again we want the value
copyValue := reflect.New(originalValue.Type()).Elem()
err := p.interpolateRecursive(copyValue, originalValue)
if err != nil {
return err
}
// Also interpolate the key if it's a string
if key.Kind() == reflect.String {
interpolatedKey, err := interpolate.Interpolate(p.Env, key.Interface().(string))
if err != nil {
return err
}
copy.SetMapIndex(reflect.ValueOf(interpolatedKey), copyValue)
} else {
copy.SetMapIndex(key, copyValue)
}
}
// If it is a string interpolate it (yay finally we're doing what we came for)
case reflect.String:
interpolated, err := interpolate.Interpolate(p.Env, original.Interface().(string))
if err != nil {
return err
}
copy.SetString(interpolated)
// And everything else will simply be taken from the original
default:
copy.Set(original)
}
return nil
}
// PipelineParserResult is the ordered parse tree of a Pipeline document
type PipelineParserResult struct {
pipeline yaml.MapSlice
}
func (p *PipelineParserResult) MarshalJSON() ([]byte, error) {
return yamltojson.MarshalMapSliceJSON(p.pipeline)
}
// topLevelStep is a custom type to support "step or string" which works around
// an issue where ordered parsing of yaml doesn't work with a top-level slice
type topLevelStep struct {
yaml.MapSlice
Body string
}
func (s *topLevelStep) UnmarshalYAML(unmarshal func(interface{}) error) error {
// Some steps are plain old strings like "wait". To avoid unmarshaling errors
// we check for plain old strings
if err := unmarshal(&s.Body); err == nil {
return nil
}
return unmarshal(&s.MapSlice)
}