-
Notifications
You must be signed in to change notification settings - Fork 0
/
yaml_loader.go
134 lines (103 loc) · 2.67 KB
/
yaml_loader.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
package loader
import (
"bytes"
"fmt"
"strings"
"text/template"
"github.com/pkg/errors"
"github.com/stretchy/stretchy/pkg/configuration"
"gopkg.in/yaml.v3"
"github.com/Masterminds/sprig/v3"
)
type YAMLLoader struct {
basePath string
}
func NewYAMLLoader(basePath string) *YAMLLoader {
return &YAMLLoader{basePath: basePath}
}
func (yl *YAMLLoader) Supports() []string {
return []string{"yml", "yaml"}
}
func (yl *YAMLLoader) LoadAll() (configuration.IndexCollection, error) {
indexCollection := configuration.NewIndexCollection()
helpers, err := yl.loadHelpers()
if err != nil {
return nil, err
}
templater := template.New("yaml-loader")
yl.initFunMap(templater)
if len(helpers) > 0 {
if _, err := templater.ParseFiles(helpers...); err != nil {
return nil, err
}
}
templates, err := yl.loadTemplates()
if err != nil {
return nil, err
}
for _, t := range templates {
f, err := getFile(t)
if err != nil {
return nil, err
}
if _, err = templater.Parse(fmt.Sprintf("{{define `%s`}}%s{{end}}", f.Name, f.Content)); err != nil {
return nil, err
}
buf := new(bytes.Buffer)
if err := templater.ExecuteTemplate(buf, f.Name, nil); err != nil {
return nil, err
}
index := configuration.Index{}
if err := yaml.NewDecoder(buf).Decode(&index); err != nil {
return nil, err
}
indexCollection.Load(f.Name, index)
}
return indexCollection, nil
}
func (yl *YAMLLoader) Load(configurationName string) (configuration.Index, error) {
indexCollection, err := yl.LoadAll()
if err != nil {
return configuration.Index{}, err
}
return indexCollection.Get(configurationName)
}
func (yl *YAMLLoader) loadTemplates() ([]string, error) {
templates, err := listFilesByExtensions(yl.basePath, yl.Supports()...)
if err != nil {
return nil, err
}
return templates, nil
}
func (yl *YAMLLoader) loadHelpers() ([]string, error) {
helpers, err := listFilesByExtensions(yl.basePath, "tmpl")
if err != nil {
return nil, err
}
return helpers, nil
}
const recursionMaxNums = 1000
func (yl *YAMLLoader) initFunMap(t *template.Template) {
funcMap := sprig.TxtFuncMap()
includedNames := make(map[string]int)
// Load some extra functionality
funcMap["include"] = func(name string, data interface{}) (string, error) {
var buf strings.Builder
if v, ok := includedNames[name]; ok {
if v > recursionMaxNums {
return "", errors.Wrapf(
fmt.Errorf("unable to execute template"),
"rendering template has a nested reference name: %s",
name,
)
}
includedNames[name]++
} else {
includedNames[name] = 1
}
err := t.ExecuteTemplate(&buf, name, data)
includedNames[name]--
return buf.String(), err
}
t.Funcs(funcMap)
}