-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.go
More file actions
220 lines (177 loc) · 4.19 KB
/
config.go
File metadata and controls
220 lines (177 loc) · 4.19 KB
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
package darius
import (
"errors"
"fmt"
"path/filepath"
"regexp"
"strings"
"gopkg.in/yaml.v2"
)
type Config struct {
includeStack []string
ReadFile func(string) ([]byte, error)
Glob func(string) ([]string, error)
}
func (config Config) Load(file string) (map[interface{}]interface{}, error) {
plain, err := config.load(file)
if err != nil {
return nil, err
}
result, ok := plain.(map[interface{}]interface{})
if !ok {
return nil, errors.New("config value should be map in " + file)
}
return result, nil
}
func (config Config) load(file string) (interface{}, error) {
for _, fileInProcess := range config.includeStack {
if fileInProcess == file {
return nil, errors.New("recursive include detected: " +
strings.Join(config.includeStack, " <- "))
}
}
config.includeStack = append(config.includeStack, file)
defer func() {
config.includeStack = config.includeStack[:len(config.includeStack)-1]
}()
contents, err := config.ReadFile(file)
if err != nil {
return nil, err
}
var raw interface{} = nil
slice := yaml.MapSlice{}
err = yaml.Unmarshal(contents, &slice)
rootIncluded := false
if err == nil {
raw = slice
} else {
trimmed := strings.Trim(string(contents), " \n")
if strings.HasPrefix(trimmed, "-") {
contents = []byte("__root: \n" + string(contents))
} else {
contents = []byte("__root: " + string(contents))
}
err = yaml.Unmarshal(contents, &slice)
if err != nil {
return nil, err
}
rootIncluded = true
raw = slice
}
parsed, err := config.parseYaml(raw)
if err != nil {
return nil, err
}
if rootIncluded {
parsed = parsed.(map[interface{}]interface{})["__root"]
}
result, err := config.include(parsed)
if err != nil {
return nil, err
}
return result, nil
}
func (config Config) parseYaml(raw interface{}) (interface{}, error) {
slice, ok := raw.(yaml.MapSlice)
if ok {
result := map[interface{}]interface{}{}
for _, element := range slice {
parsed, err := config.parseYaml(element.Value)
if err != nil {
return nil, err
}
result[element.Key] = parsed
}
return result, nil
}
array, ok := raw.([]interface{})
if ok {
for index, element := range array {
var err error
array[index], err = config.parseYaml(element)
if err != nil {
return nil, err
}
}
return array, nil
}
return raw, nil
}
var (
includeRegexp = regexp.MustCompile(`^\$\{include (.*?)\}$`)
includeRegexpOld = regexp.MustCompile(`^\$include (.*?)$`)
)
func (config Config) include(
value interface{},
) (interface{}, error) {
var err error
str, ok := value.(string)
if ok {
match := includeRegexp.FindStringSubmatch(str)
if len(match) == 0 {
match = includeRegexpOld.FindStringSubmatch(str)
}
if len(match) > 0 {
file := match[1]
if !strings.HasPrefix(file, "/") {
current := filepath.Dir(file)
if len(config.includeStack) > 0 {
current = config.includeStack[len(config.includeStack)-1]
}
file = filepath.Join(filepath.Dir(current), file)
}
if !strings.Contains(file, "*") {
return config.load(file)
}
files, err := config.Glob(file)
if err != nil {
return nil, err
}
return config.loadFiles(files)
}
return value, nil
}
mapping, ok := value.(map[interface{}]interface{})
if ok {
for key, value := range mapping {
mapping[key], err = config.include(value)
if err != nil {
return nil, err
}
}
return mapping, nil
}
array, ok := value.([]interface{})
if ok {
for index, value := range array {
array[index], err = config.include(value)
if err != nil {
return nil, err
}
}
return array, nil
}
return value, nil
}
func (config *Config) loadFiles(files []string) (interface{}, error) {
result := map[interface{}]interface{}{}
for _, file := range files {
current, err := config.load(file)
if err != nil {
return nil, err
}
mapping, ok := current.(map[interface{}]interface{})
if !ok {
return nil, errors.New("config in " + file + " should be map")
}
for key, value := range mapping {
_, ok := result[key]
if ok {
return nil, errors.New("value is alredy present in " +
fmt.Sprint(key) + " (loading " + file + ")")
}
result[key] = value
}
}
return result, nil
}