/
fileconf.go
195 lines (151 loc) · 3.96 KB
/
fileconf.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
// Copyright (c) 2018, Eugene Ponizovsky, <ponizovsky@gmail.com>. All rights
// reserved. Use of this source code is governed by a MIT License that can
// be found in the LICENSE file.
/*
Package fileconf is configuration loader for the conf package. It loads
configuration layers from YAML, JSON or TOML files. Configuration locators for
this loader are relative pathes or glob patterns. See standart package
path/filepath for more information about syntax of glob patterns. Here some
examples:
file:myapp/dirs.yml
file:myapp/servers.toml
file:myapp/*.json
file:myapp/*.*
*/
package fileconf
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"reflect"
"regexp"
"github.com/BurntSushi/toml"
"github.com/iph0/conf"
"github.com/iph0/merger"
yaml "gopkg.in/yaml.v3"
)
const errPref = "fileconf"
var (
parsers = map[string]func(bytes []byte) (interface{}, error){
"yml": unmarshalYAML,
"yaml": unmarshalYAML,
"json": unmarshalJSON,
"toml": unmarshalTOML,
}
fileExtRe = regexp.MustCompile("\\.([^.]+)$")
)
// FileLoader loads configuration layers from YAML, JSON and TOML configuration
// files.
type FileLoader struct {
dirs []string
}
// NewLoader method creates new loader instance. Method accepts a list of
// directories, in which the loader will search configuration files. The merge
// priority of loaded configuration layers depends on the order of directories.
// Layers loaded from rightmost directory have highest priority.
func NewLoader(dirs ...string) *FileLoader {
if len(dirs) == 0 {
panic(fmt.Errorf("%s: no directories specified", errPref))
}
return &FileLoader{
dirs: dirs,
}
}
// Load method loads configuration layer from YAML, JSON and TOML configuration
// files.
func (l *FileLoader) Load(loc *conf.Locator) (interface{}, error) {
var config interface{}
globPattern := loc.BareLocator
for _, dir := range l.dirs {
absPattern := filepath.Join(dir, globPattern)
pathes, err := filepath.Glob(absPattern)
if err != nil {
return nil, fmt.Errorf("%s: %s", errPref, err)
}
for _, path := range pathes {
matches := fileExtRe.FindStringSubmatch(path)
if matches == nil {
return nil, fmt.Errorf("%s: file extension not specified: %s",
errPref, path)
}
ext := matches[1]
parser, ok := parsers[ext]
if !ok {
return nil, fmt.Errorf("%s: unknown file extension .%s",
errPref, ext)
}
f, err := os.Open(path)
if err != nil {
return nil, fmt.Errorf("%s: %s", errPref, err)
}
defer f.Close()
bytes, err := ioutil.ReadAll(f)
if err != nil {
return nil, fmt.Errorf("%s: %s", errPref, err)
}
data, err := parser(bytes)
if err != nil {
return nil, fmt.Errorf("%s: %s", errPref, err)
}
config = merger.Merge(config, data)
}
}
return config, nil
}
func unmarshalYAML(rawData []byte) (interface{}, error) {
decoder := yaml.NewDecoder(bytes.NewReader(rawData))
var config interface{}
for {
var doc interface{}
err := decoder.Decode(&doc)
if err != nil {
if err == io.EOF {
break
}
return nil, err
}
switch d := doc.(type) {
case map[interface{}]interface{}:
doc = adaptYAMLMap(d)
}
config = merger.Merge(config, doc)
}
return config, nil
}
func unmarshalJSON(bytes []byte) (interface{}, error) {
var iData interface{}
err := json.Unmarshal(bytes, &iData)
if err != nil {
return nil, err
}
return iData, nil
}
func unmarshalTOML(bytes []byte) (interface{}, error) {
var iData interface{}
err := toml.Unmarshal(bytes, &iData)
if err != nil {
return nil, err
}
return iData, nil
}
func adaptYAMLMap(from map[interface{}]interface{}) conf.M {
fromType := reflect.ValueOf(from).Type()
to := make(conf.M)
for key, value := range from {
if value == nil {
continue
}
keyStr := fmt.Sprintf("%v", key)
valType := reflect.ValueOf(value).Type()
if fromType == valType {
to[keyStr] = adaptYAMLMap(value.(map[interface{}]interface{}))
continue
}
to[keyStr] = value
}
return to
}