-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfile_sourcer.go
220 lines (175 loc) · 5.01 KB
/
file_sourcer.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
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 config
import (
"encoding/json"
"fmt"
"path/filepath"
"strings"
"github.com/BurntSushi/toml"
"github.com/ghodss/yaml"
)
type fileSourcer struct {
filename string
parser FileParser
fs FileSystem
optional bool
values map[string]string
}
var _ Sourcer = &fileSourcer{}
type FileParser func(content []byte) (map[string]interface{}, error)
var parserMap = map[string]FileParser{
".yaml": ParseYAML,
".yml": ParseYAML,
".json": ParseYAML,
".toml": ParseTOML,
}
// NewOptionalFileSourcer creates a file sourcer that does not error on init
// if the file does not exist. The underlying sourcer returns no values.
func NewOptionalFileSourcer(filename string, parser FileParser, configs ...FileSourcerConfigFunc) Sourcer {
return &fileSourcer{
filename: filename,
parser: parser,
fs: getFileSourcerConfigOptions(configs).fs,
optional: true,
}
}
// NewFileSourcer creates a sourcer that reads content from a file. The format
// of the file is read by the given FileParser. The content of the file must be
// an encoding of a map from string keys to JSON-serializable values. If a nil
// parser is supplied, one will be selected based on the extension of the file.
// JSON, YAML, and TOML files are supported.
func NewFileSourcer(filename string, parser FileParser, configs ...FileSourcerConfigFunc) Sourcer {
return &fileSourcer{
filename: filename,
parser: parser,
fs: getFileSourcerConfigOptions(configs).fs,
}
}
func (s *fileSourcer) Init() error {
if s.optional {
exists, err := s.fs.Exists(s.filename)
if err != nil || !exists {
return err
}
if !exists {
return nil
}
}
values, err := readFile(s.filename, s.fs, s.parser)
if err != nil {
return err
}
jsonValues, err := serializeJSONValues(values)
if err != nil {
return err
}
s.values = jsonValues
return nil
}
func (s *fileSourcer) Tags() []string {
return []string{FileTag}
}
func (s *fileSourcer) Get(values []string) (string, SourcerFlag, error) {
if values[0] == "" {
return "", FlagSkip, nil
}
segments := strings.Split(values[0], ".")
if val, ok := s.values[segments[0]]; ok {
if val, ok := extractJSONPath(val, segments[1:]); ok {
return val, FlagFound, nil
}
}
return "", FlagMissing, nil
}
func (s *fileSourcer) Assets() []string {
return []string{s.filename}
}
func (s *fileSourcer) Dump() map[string]string {
return s.values
}
//
// Parsers
// NewYAMLFileSourcer creates a file sourcer that parses conent as YAML.
func NewYAMLFileSourcer(filename string) Sourcer {
return NewFileSourcer(filename, ParseYAML)
}
// NewTOMLFileSourcer creates a file sourcer that parses conent as TOML.
func NewTOMLFileSourcer(filename string) Sourcer {
return NewFileSourcer(filename, ParseTOML)
}
// ParseYAML parses the given content as YAML.
func ParseYAML(content []byte) (map[string]interface{}, error) {
return commonParser(content, func(content []byte, values interface{}) error {
return yaml.Unmarshal(content, values)
})
}
// ParseTOML parses the given content as JSON.
func ParseTOML(content []byte) (map[string]interface{}, error) {
return commonParser(content, toml.Unmarshal)
}
func commonParser(content []byte, unmarshaller func([]byte, interface{}) error) (map[string]interface{}, error) {
values := map[string]interface{}{}
if err := unmarshaller(content, &values); err != nil {
return nil, fmt.Errorf("failed to unmarshal config (%s)", err.Error())
}
return values, nil
}
//
// Helpers
func readFile(filename string, fs FileSystem, parser FileParser) (map[string]interface{}, error) {
content, err := fs.ReadFile(filename)
if err != nil {
return nil, fmt.Errorf("failed to read config file '%s' (%s)", filename, err.Error())
}
parser, err = chooseParser(filename, parser)
if err != nil {
return nil, err
}
return parser(content)
}
func chooseParser(filename string, parser FileParser) (FileParser, error) {
if parser != nil {
return parser, nil
}
if parser, ok := parserMap[filepath.Ext(filename)]; ok {
return parser, nil
}
return nil, fmt.Errorf("failed to determine parser for file %s", filename)
}
func serializeJSONValues(values map[string]interface{}) (map[string]string, error) {
jsonValues := map[string]string{}
for key, value := range values {
serialized, err := serializeJSONValue(value)
if err != nil {
return nil, fmt.Errorf("illegal configuration value for '%s' (%s)", key, err.Error())
}
jsonValues[key] = serialized
}
return jsonValues, nil
}
func serializeJSONValue(value interface{}) (string, error) {
if str, ok := value.(string); ok {
return str, nil
}
serialized, err := json.Marshal(value)
if err != nil {
return "", err
}
return string(serialized), nil
}
func extractJSONPath(val string, path []string) (string, bool) {
if len(path) == 0 {
return val, true
}
for _, segment := range path {
mapping := map[string]json.RawMessage{}
if err := json.Unmarshal([]byte(val), &mapping); err != nil {
return "", false
}
inner, ok := mapping[segment]
if !ok {
return "", false
}
val = string(inner)
}
return val, true
}