Skip to content

Commit 99704f6

Browse files
authored
Merge pull request #7 from lbernardo/feature/read-includes-at-serverless-yml
Includes at serverless yml
2 parents 38c1b6b + 892b898 commit 99704f6

File tree

1 file changed

+49
-2
lines changed

1 file changed

+49
-2
lines changed

controller/readyaml.go

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,68 @@
11
package controller
22

33
import (
4+
"encoding/json"
45
"io/ioutil"
6+
"regexp"
7+
"strings"
58

69
"github.com/ghodss/yaml"
710
)
811

912
func ReadYaml(file string) ([]byte, error) {
10-
content, err := ioutil.ReadFile(file)
13+
content, err := MapServerlessFileIncludes(file)
1114
if err != nil {
1215
return nil, err
1316
}
1417

15-
cj, err := yaml.YAMLToJSON(content)
18+
cj, err := json.Marshal(content)
1619
if err != nil {
1720
return nil, err
1821
}
1922

2023
return cj, nil
2124
}
25+
26+
func MapServerlessFileIncludes(file string) (map[string]interface{}, error) {
27+
content, err := ioutil.ReadFile(file)
28+
if err != nil {
29+
return nil, err
30+
}
31+
contentMap := make(map[string]interface{}, 0)
32+
err = yaml.Unmarshal(content, &contentMap)
33+
if err != nil {
34+
return nil, err
35+
}
36+
r, err := regexp.Compile(`\${file\((.+).yml\)}`)
37+
if err != nil {
38+
return nil, err
39+
}
40+
for i, v := range contentMap {
41+
s, ok := v.(string)
42+
if !ok {
43+
continue
44+
}
45+
result := r.FindAllString(s, -1)
46+
if len(result) <= 0 {
47+
contentMap[i] = result
48+
continue
49+
}
50+
contentMap[i], err = processFiles(result)
51+
if err != nil {
52+
return nil, err
53+
}
54+
}
55+
return contentMap, nil
56+
}
57+
58+
func processFiles(files []string) (content map[string]interface{}, err error) {
59+
for _, file := range files {
60+
file = strings.Replace(file, "${file(", "", -1)
61+
file = strings.Replace(file, ")}", "", -1)
62+
content, err = MapServerlessFileIncludes(file)
63+
if err != nil {
64+
return
65+
}
66+
}
67+
return
68+
}

0 commit comments

Comments
 (0)