forked from awslabs/goformation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
goformation.go
65 lines (46 loc) · 1.46 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
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) {
data, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
if strings.HasSuffix(filename, ".yaml") || strings.HasSuffix(filename, ".yml") {
return ParseYAML(data)
}
return ParseJSON(data)
}
// ParseYAML an AWS CloudFormation template (expects a []byte of valid YAML)
func ParseYAML(data []byte) (*cloudformation.Template, error) {
// Process all AWS CloudFormation intrinsic functions (e.g. Fn::Join)
intrinsified, err := intrinsics.ProcessYAML(data, nil)
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) {
// Process all AWS CloudFormation intrinsic functions (e.g. Fn::Join)
intrinsified, err := intrinsics.ProcessJSON(data, nil)
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
}