/
parse.go
executable file
·351 lines (290 loc) · 8.1 KB
/
parse.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
package appfile
import (
"bytes"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"github.com/hashicorp/go-multierror"
"github.com/hashicorp/hcl"
"github.com/hashicorp/hcl/hcl/ast"
"github.com/mitchellh/mapstructure"
)
// Parse parses the Appfile from the given io.Reader.
//
// Due to current internal limitations, the entire contents of the
// io.Reader will be copied into memory first before parsing.
func Parse(r io.Reader) (*File, error) {
// Copy the reader into an in-memory buffer first since HCL requires it.
var buf bytes.Buffer
if _, err := io.Copy(&buf, r); err != nil {
return nil, err
}
// Parse the buffer
root, err := hcl.Parse(buf.String())
if err != nil {
return nil, fmt.Errorf("error parsing: %s", err)
}
buf.Reset()
// Top-level item should be the object list
list, ok := root.Node.(*ast.ObjectList)
if !ok {
return nil, fmt.Errorf("error parsing: file doesn't contain a root object")
}
// Check for invalid keys
valid := []string{
"application",
"customization",
"import",
"infrastructure",
"project",
}
if err := checkHCLKeys(list, valid); err != nil {
return nil, err
}
var result File
// Parse the imports
if o := list.Filter("import"); len(o.Items) > 0 {
if err := parseImport(&result, o); err != nil {
return nil, fmt.Errorf("error parsing 'import': %s", err)
}
}
// Parse the application
if o := list.Filter("application"); len(o.Items) > 0 {
if err := parseApplication(&result, o); err != nil {
return nil, fmt.Errorf("error parsing 'application': %s", err)
}
}
// Parse the project
if o := list.Filter("project"); len(o.Items) > 0 {
if err := parseProject(&result, o); err != nil {
return nil, fmt.Errorf("error parsing 'project': %s", err)
}
}
// Parse the infrastructure
if o := list.Filter("infrastructure"); len(o.Items) > 0 {
if err := parseInfra(&result, o); err != nil {
return nil, fmt.Errorf("error parsing 'infrastructure': %s", err)
}
}
// Parse the customizations
if o := list.Filter("customization"); len(o.Items) > 0 {
if err := parseCustomizations(&result, o); err != nil {
return nil, fmt.Errorf("error parsing 'customization': %s", err)
}
}
return &result, nil
}
// ParseFile parses the given path as an Appfile.
func ParseFile(path string) (*File, error) {
path, err := filepath.Abs(path)
if err != nil {
return nil, err
}
f, err := os.Open(path)
if err != nil {
return nil, err
}
defer f.Close()
result, err := Parse(f)
if result != nil {
result.Path = path
if err := result.loadID(); err != nil {
return nil, err
}
}
return result, err
}
func parseApplication(result *File, list *ast.ObjectList) error {
if len(list.Items) > 1 {
return fmt.Errorf("only one 'application' block allowed")
}
// Get our one item
item := list.Items[0]
// Check for invalid keys
valid := []string{"name", "type", "detect", "dependency"}
if err := checkHCLKeys(item.Val, valid); err != nil {
return multierror.Prefix(err, "application:")
}
var m map[string]interface{}
if err := hcl.DecodeObject(&m, item.Val); err != nil {
return err
}
app := Application{Detect: true}
result.Application = &app
return mapstructure.WeakDecode(m, &app)
}
func parseCustomizations(result *File, list *ast.ObjectList) error {
// Go through each object and turn it into an actual result.
collection := make([]*Customization, 0, len(list.Items))
for _, item := range list.Items {
key := "app"
if len(item.Keys) > 0 {
key = item.Keys[0].Token.Value().(string)
}
var m map[string]interface{}
if err := hcl.DecodeObject(&m, item.Val); err != nil {
return err
}
var c Customization
c.Type = strings.ToLower(key)
c.Config = m
collection = append(collection, &c)
}
result.Customization = &CustomizationSet{Raw: collection}
return nil
}
func parseImport(result *File, list *ast.ObjectList) error {
list = list.Children()
if len(list.Items) == 0 {
return nil
}
// Go through each object and turn it into an actual result.
collection := make([]*Import, 0, len(list.Items))
seen := make(map[string]struct{})
for _, item := range list.Items {
key := item.Keys[0].Token.Value().(string)
// Make sure we haven't already found this import
if _, ok := seen[key]; ok {
return fmt.Errorf("import '%s' defined more than once", key)
}
seen[key] = struct{}{}
// Check for invalid keys
if err := checkHCLKeys(item.Val, nil); err != nil {
return multierror.Prefix(err, fmt.Sprintf(
"import '%s':", key))
}
collection = append(collection, &Import{
Source: key,
})
}
result.Imports = collection
return nil
}
func parseInfra(result *File, list *ast.ObjectList) error {
list = list.Children()
if len(list.Items) == 0 {
return nil
}
// Go through each object and turn it into an actual result.
collection := make([]*Infrastructure, 0, len(list.Items))
seen := make(map[string]struct{})
for _, item := range list.Items {
n := item.Keys[0].Token.Value().(string)
// Make sure we haven't already found this
if _, ok := seen[n]; ok {
return fmt.Errorf("infrastructure '%s' defined more than once", n)
}
seen[n] = struct{}{}
// Check for invalid keys
valid := []string{"name", "type", "flavor", "foundation"}
if err := checkHCLKeys(item.Val, valid); err != nil {
return multierror.Prefix(err, fmt.Sprintf(
"infrastructure '%s':", n))
}
var listVal *ast.ObjectList
if ot, ok := item.Val.(*ast.ObjectType); ok {
listVal = ot.List
} else {
return fmt.Errorf("infrastructure '%s': should be an object", n)
}
var m map[string]interface{}
if err := hcl.DecodeObject(&m, item.Val); err != nil {
return err
}
var infra Infrastructure
if err := mapstructure.WeakDecode(m, &infra); err != nil {
return fmt.Errorf(
"error parsing infrastructure '%s': %s", n, err)
}
infra.Name = n
if infra.Type == "" {
infra.Type = infra.Name
}
// Parse the foundations if we have any
if o2 := listVal.Filter("foundation"); len(o2.Items) > 0 {
if err := parseFoundations(&infra, o2); err != nil {
return fmt.Errorf("error parsing 'foundation': %s", err)
}
}
collection = append(collection, &infra)
}
result.Infrastructure = collection
return nil
}
func parseFoundations(result *Infrastructure, list *ast.ObjectList) error {
list = list.Children()
if len(list.Items) == 0 {
return nil
}
// Go through each object and turn it into an actual result.
collection := make([]*Foundation, 0, len(list.Items))
seen := make(map[string]struct{})
for _, item := range list.Items {
n := item.Keys[0].Token.Value().(string)
// Make sure we haven't already found this
if _, ok := seen[n]; ok {
return fmt.Errorf("foundation '%s' defined more than once", n)
}
seen[n] = struct{}{}
var m map[string]interface{}
if err := hcl.DecodeObject(&m, item.Val); err != nil {
return err
}
var f Foundation
f.Name = n
f.Config = m
collection = append(collection, &f)
}
// Set the results
result.Foundations = collection
return nil
}
func parseProject(result *File, list *ast.ObjectList) error {
if len(list.Items) > 1 {
return fmt.Errorf("only one 'project' block allowed")
}
// Get our one item
item := list.Items[0]
// Check for invalid keys
valid := []string{"name", "infrastructure"}
if err := checkHCLKeys(item.Val, valid); err != nil {
return multierror.Prefix(err, "project:")
}
var m map[string]interface{}
if err := hcl.DecodeObject(&m, item.Val); err != nil {
return err
}
// Parse the project
var proj Project
result.Project = &proj
if err := mapstructure.WeakDecode(m, &proj); err != nil {
return err
}
return nil
}
func checkHCLKeys(node ast.Node, valid []string) error {
var list *ast.ObjectList
switch n := node.(type) {
case *ast.ObjectList:
list = n
case *ast.ObjectType:
list = n.List
default:
return fmt.Errorf("cannot check HCL keys of type %T", n)
}
validMap := make(map[string]struct{}, len(valid))
for _, v := range valid {
validMap[v] = struct{}{}
}
var result error
for _, item := range list.Items {
key := item.Keys[0].Token.Value().(string)
if _, ok := validMap[key]; !ok {
result = multierror.Append(result, fmt.Errorf(
"invalid key: %s", key))
}
}
return result
}