forked from hashicorp/otto
/
parse.go
348 lines (290 loc) · 7.73 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
package appfile
import (
"bytes"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"github.com/hashicorp/go-multierror"
"github.com/hashicorp/hcl"
hclobj "github.com/hashicorp/hcl/hcl"
"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
obj, err := hcl.Parse(buf.String())
if err != nil {
return nil, fmt.Errorf("error parsing: %s", err)
}
buf.Reset()
// Check for invalid keys
valid := []string{
"application",
"customization",
"import",
"infrastructure",
"project",
}
if err := checkHCLKeys(obj, valid); err != nil {
return nil, err
}
var result File
// Parse the imports
if o := obj.Get("import", false); o != nil {
if err := parseImport(&result, o); err != nil {
return nil, fmt.Errorf("error parsing 'import': %s", err)
}
}
// Parse the application
if o := obj.Get("application", false); o != nil {
if err := parseApplication(&result, o); err != nil {
return nil, fmt.Errorf("error parsing 'application': %s", err)
}
}
// Parse the project
if o := obj.Get("project", false); o != nil {
if err := parseProject(&result, o); err != nil {
return nil, fmt.Errorf("error parsing 'project': %s", err)
}
}
// Parse the infrastructure
if o := obj.Get("infrastructure", false); o != nil {
if err := parseInfra(&result, o); err != nil {
return nil, fmt.Errorf("error parsing 'infrastructure': %s", err)
}
}
// Parse the customizations
if o := obj.Get("customization", false); o != nil {
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, obj *hclobj.Object) error {
if obj.Len() > 1 {
return fmt.Errorf("only one 'application' block allowed")
}
// Check for invalid keys
valid := []string{"name", "type", "dependency"}
if err := checkHCLKeys(obj, valid); err != nil {
return multierror.Prefix(err, "application:")
}
var m map[string]interface{}
if err := hcl.DecodeObject(&m, obj); err != nil {
return err
}
var app Application
result.Application = &app
return mapstructure.WeakDecode(m, &app)
}
func parseCustomizations(result *File, obj *hclobj.Object) error {
// Get all the maps of keys to the actual object
objects := make(map[string]*hclobj.Object)
for _, o1 := range obj.Elem(false) {
for _, o2 := range o1.Elem(true) {
if _, ok := objects[o2.Key]; ok {
return fmt.Errorf(
"customization '%s' defined more than once",
o2.Key)
}
objects[o2.Key] = o2
}
}
if len(objects) == 0 {
return nil
}
// Go through each object and turn it into an actual result.
collection := make([]*Customization, 0, len(objects))
for n, o := range objects {
var m map[string]interface{}
if err := hcl.DecodeObject(&m, o); err != nil {
return err
}
var c Customization
c.Type = strings.ToLower(n)
c.Config = m
collection = append(collection, &c)
}
result.Customization = &CustomizationSet{Raw: collection}
return nil
}
func parseImport(result *File, obj *hclobj.Object) error {
// Get all the maps of keys to the actual object
objects := make([]*hclobj.Object, 0, 3)
set := make(map[string]struct{})
for _, o1 := range obj.Elem(false) {
for _, o2 := range o1.Elem(true) {
if _, ok := set[o2.Key]; ok {
return fmt.Errorf(
"imported '%s' more than once",
o2.Key)
}
objects = append(objects, o2)
set[o2.Key] = struct{}{}
}
}
if len(objects) == 0 {
return nil
}
// Go through each object and turn it into an actual result.
collection := make([]*Import, 0, len(objects))
for _, o := range objects {
// Check for invalid keys
if err := checkHCLKeys(o, nil); err != nil {
return multierror.Prefix(err, fmt.Sprintf(
"import '%s':", o.Key))
}
collection = append(collection, &Import{
Source: o.Key,
})
}
result.Imports = collection
return nil
}
func parseInfra(result *File, obj *hclobj.Object) error {
// Get all the maps of keys to the actual object
objects := make(map[string]*hclobj.Object)
for _, o1 := range obj.Elem(false) {
for _, o2 := range o1.Elem(true) {
if _, ok := objects[o2.Key]; ok {
return fmt.Errorf(
"infrastructure '%s' defined more than once",
o2.Key)
}
objects[o2.Key] = o2
}
}
if len(objects) == 0 {
return nil
}
// Go through each object and turn it into an actual result.
collection := make([]*Infrastructure, 0, len(objects))
for n, o := range objects {
// Check for invalid keys
valid := []string{"name", "type", "flavor", "foundation"}
if err := checkHCLKeys(o, valid); err != nil {
return multierror.Prefix(err, fmt.Sprintf(
"infrastructure '%s':", n))
}
var m map[string]interface{}
if err := hcl.DecodeObject(&m, o); 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 := o.Get("foundation", false); o != nil {
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, obj *hclobj.Object) error {
// Get all the maps of keys to the actual object
objects := make(map[string]*hclobj.Object)
for _, o1 := range obj.Elem(false) {
for _, o2 := range o1.Elem(true) {
if _, ok := objects[o2.Key]; ok {
return fmt.Errorf(
"foundation '%s' defined more than once",
o2.Key)
}
objects[o2.Key] = o2
}
}
if len(objects) == 0 {
return nil
}
// Go through each object and turn it into an actual result.
collection := make([]*Foundation, 0, len(objects))
for n, o := range objects {
var m map[string]interface{}
if err := hcl.DecodeObject(&m, o); 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, obj *hclobj.Object) error {
if obj.Len() > 1 {
return fmt.Errorf("only one 'project' block allowed")
}
// Check for invalid keys
valid := []string{"name", "infrastructure"}
if err := checkHCLKeys(obj, valid); err != nil {
return multierror.Prefix(err, "project:")
}
var m map[string]interface{}
if err := hcl.DecodeObject(&m, obj); 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(obj *hclobj.Object, valid []string) error {
validMap := make(map[string]struct{}, len(valid))
for _, v := range valid {
validMap[v] = struct{}{}
}
var result error
for _, o := range obj.Elem(true) {
if _, ok := validMap[o.Key]; !ok {
result = multierror.Append(result, fmt.Errorf(
"invald key: %s", o.Key))
}
}
return result
}