forked from awslabs/goformation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
goformation.go
84 lines (63 loc) · 2.41 KB
/
goformation.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
package goformation
import (
"encoding/json"
"io/ioutil"
"strings"
"github.com/awslabs/goformation/cloudformation"
"github.com/awslabs/goformation/intrinsics"
)
//go:generate generate/generate.sh
// Open and parse a AWS CloudFormation template from file.
// Works with either JSON or YAML formatted templates.
func Open(filename string) (*cloudformation.Template, error) {
return OpenWithOptions(filename, nil)
}
// OpenWithOptions opens and parse a AWS CloudFormation template from file.
// Works with either JSON or YAML formatted templates.
// Parsing can be tweaked via the specified options.
func OpenWithOptions(filename string, options *intrinsics.ProcessorOptions) (*cloudformation.Template, error) {
data, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
if strings.HasSuffix(filename, ".json") {
// This is definitely JSON
return ParseJSONWithOptions(data, options)
}
return ParseYAMLWithOptions(data, options)
}
// ParseYAML an AWS CloudFormation template (expects a []byte of valid YAML)
func ParseYAML(data []byte) (*cloudformation.Template, error) {
return ParseYAMLWithOptions(data, nil)
}
// ParseYAMLWithOptions an AWS CloudFormation template (expects a []byte of valid YAML)
// Parsing can be tweaked via the specified options.
func ParseYAMLWithOptions(data []byte, options *intrinsics.ProcessorOptions) (*cloudformation.Template, error) {
// Process all AWS CloudFormation intrinsic functions (e.g. Fn::Join)
intrinsified, err := intrinsics.ProcessYAML(data, options)
if err != nil {
return nil, err
}
return unmarshal(intrinsified)
}
// ParseJSON an AWS CloudFormation template (expects a []byte of valid JSON)
func ParseJSON(data []byte) (*cloudformation.Template, error) {
return ParseJSONWithOptions(data, nil)
}
// ParseJSONWithOptions an AWS CloudFormation template (expects a []byte of valid JSON)
// Parsing can be tweaked via the specified options.
func ParseJSONWithOptions(data []byte, options *intrinsics.ProcessorOptions) (*cloudformation.Template, error) {
// Process all AWS CloudFormation intrinsic functions (e.g. Fn::Join)
intrinsified, err := intrinsics.ProcessJSON(data, options)
if err != nil {
return nil, err
}
return unmarshal(intrinsified)
}
func unmarshal(data []byte) (*cloudformation.Template, error) {
template := &cloudformation.Template{}
if err := json.Unmarshal(data, template); err != nil {
return nil, err
}
return template, nil
}