-
Notifications
You must be signed in to change notification settings - Fork 17
/
loader.go
341 lines (316 loc) · 8.24 KB
/
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
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/*************************************************************************
* Copyright 2017 Gravwell, Inc. All rights reserved.
* Contact: <legal@gravwell.io>
*
* This software may be modified and distributed under the terms of the
* BSD 2-clause license. See the LICENSE file for details.
**************************************************************************/
package config
import (
"bytes"
"errors"
"fmt"
"io"
"os"
"path/filepath"
"reflect"
"strconv"
"strings"
"github.com/gravwell/gcfg"
)
const (
maxConfigSize int64 = 4 * mb // This is a MASSIVE config file
confExt string = `.conf`
)
var (
ErrConfigFileTooLarge = errors.New("Config file is too large")
ErrFailedFileRead = errors.New("Failed to read entire config file")
ErrConfigNotOpen = errors.New("Configuration is not open")
ErrInvalidImportInterface = errors.New("Invalid import interface argument")
ErrInvalidImportParameter = errors.New("parameter is not a pointer")
ErrInvalidArgument = errors.New("Invalid argument")
ErrInvalidMapKeyType = errors.New("invalid map key type, must be string")
ErrInvalidMapValueType = errors.New("invalid map value type, must be pointer to struct")
ErrBadMap = errors.New("VariableConfig has not be initialized")
ErrNotFound = errors.New("not found")
ErrIsNotDirectory = errors.New("path is not a directory")
)
type VariableConfig struct {
gcfg.Idxer
Vals map[gcfg.Idx]*[]string
}
// LoadConfigFile will open a config file, check the file size
// and load the bytes using LoadConfigBytes
func LoadConfigFile(v interface{}, p string) (err error) {
var fin *os.File
var fi os.FileInfo
var n int64
if fin, err = os.Open(p); err != nil {
return
} else if fi, err = fin.Stat(); err != nil {
fin.Close()
return
} else if fi.Size() > maxConfigSize {
fin.Close()
err = ErrConfigFileTooLarge
return
}
bb := bytes.NewBuffer(nil)
if n, err = io.Copy(bb, fin); err != nil {
fin.Close()
return
} else if n != fi.Size() {
fin.Close()
err = ErrFailedFileRead
} else if err = fin.Close(); err == nil {
err = LoadConfigBytes(v, bb.Bytes())
}
return
}
// LoadConfigOverlays scans the given directory path for files that end in .conf
// if they exist we load them up into the interface
func LoadConfigOverlays(v interface{}, pth string) (err error) {
if pth == `` || v == nil {
return //just leave
}
//stat the path and make sure its a directory
var fi os.FileInfo
if fi, err = os.Stat(pth); err != nil {
if os.IsNotExist(err) {
err = nil //not a problem, move on
}
return
} else if !fi.IsDir() {
err = ErrIsNotDirectory
return
}
//ok, we have a directory, read it and consume the confs
var dents []os.DirEntry
if dents, err = os.ReadDir(pth); err != nil {
return //something failed
}
for _, dent := range dents {
if !dent.Type().IsRegular() {
continue
} else if filepath.Ext(dent.Name()) != confExt {
continue
}
p := filepath.Join(pth, dent.Name())
if err = LoadConfigFile(v, p); err != nil {
err = fmt.Errorf("failed to load %q %w", p, err)
return
}
}
return
}
// LoadConfigBytes parses the contents of b into the given interface v.
func LoadConfigBytes(v interface{}, b []byte) error {
if int64(len(b)) > maxConfigSize {
return ErrConfigFileTooLarge
}
return gcfg.ReadStringInto(v, string(b))
}
// importMaps walks the structure using reflection and imports any members that are map types
// if the map is nil we initialize it
// the the goINI package doesn't handle map types for sub sections so we have to do it ourselves
/*
func (c *Config) importMaps(v interface{}) error {
if reflect.ValueOf(v).Kind() != reflect.Ptr {
return ErrInvalidImportParameter
}
vv := reflect.ValueOf(v).Elem()
tot := vv.Type()
for i := 0; i < vv.NumField(); i++ {
fld := vv.Field(i)
if fld.Kind() == reflect.Map {
// and the value is a pointer to a struct
if err := c.importMap(tot.Field(i).Name, fld); err != nil {
return err
}
}
}
return nil
}
*/
func (vc VariableConfig) MapTo(v interface{}) (err error) {
if vc.Vals == nil {
err = ErrBadMap
} else if v == nil {
err = ErrInvalidImportParameter
} else if reflect.ValueOf(v).Kind() != reflect.Ptr {
return ErrInvalidImportParameter
} else {
err = vc.mapStruct(v)
}
return
}
func (vc VariableConfig) get(name string) (v string, ok bool) {
var temp *[]string
if temp = vc.Vals[vc.Idx(name)]; temp != nil {
var x []string
x = *temp
if len(x) > 0 {
v = x[0]
ok = true
}
}
return
}
func (vc VariableConfig) getSlice(name string) (v []string, ok bool) {
var temp *[]string
if temp = vc.Vals[vc.Idx(name)]; temp != nil {
v = *temp
ok = true
}
return
}
func (vc VariableConfig) mapStruct(v interface{}) error {
if reflect.ValueOf(v).Kind() != reflect.Ptr {
return ErrInvalidImportParameter
}
// ensure the value is a pointer to a struct
rv := reflect.ValueOf(v).Elem()
if rv.Type().Kind() != reflect.Struct {
return ErrInvalidMapValueType
}
typeOf := rv.Type()
for i := 0; i < rv.NumField(); i++ {
if err := vc.setField(typeOf.Field(i).Name, rv.Field(i)); err != nil {
return err
}
}
return nil
}
// TODO FIXME - figure out how to deal with slices of a type so that we add to them
func (vc VariableConfig) setField(name string, v reflect.Value) (err error) {
strv, ok := vc.get(nameMapper(name))
if !ok {
return
}
switch v.Type().Kind() {
case reflect.Int8:
fallthrough
case reflect.Int16:
fallthrough
case reflect.Int32:
fallthrough
case reflect.Int64:
fallthrough
case reflect.Int:
var vint int64
if vint, err = ParseInt64(strv); err == nil {
if v.OverflowInt(vint) {
err = fmt.Errorf("%d overflows %T", vint, v.Interface())
} else {
v.SetInt(vint)
}
}
case reflect.Uint8:
fallthrough
case reflect.Uint16:
fallthrough
case reflect.Uint32:
fallthrough
case reflect.Uint64:
fallthrough
case reflect.Uint:
var vint uint64
if vint, err = ParseUint64(strv); err == nil {
if v.OverflowUint(vint) {
err = fmt.Errorf("%d overflows %T", vint, v.Interface())
} else {
v.SetUint(vint)
}
}
case reflect.Float32:
fallthrough
case reflect.Float64:
var vf float64
if vf, err = strconv.ParseFloat(strv, 64); err == nil {
if v.OverflowFloat(vf) {
err = fmt.Errorf("%f overflows %T", vf, v.Interface())
} else {
v.SetFloat(vf)
}
}
case reflect.Bool:
var vb bool
if vb, err = strconv.ParseBool(strings.ToLower(strv)); err == nil {
v.SetBool(vb)
}
case reflect.String:
v.SetString(strv)
case reflect.Slice:
slc, ok := vc.getSlice(nameMapper(name))
if !ok {
return
}
v.Set(reflect.AppendSlice(v, reflect.ValueOf(slc)))
default:
err = fmt.Errorf("Cannot store into member %v: unknown type %T", name, v.Interface())
}
return
}
// just wraps setField with some type handling
func (vc VariableConfig) valueMapper(name string, v interface{}) (err error) {
if v == nil {
return ErrInvalidArgument
}
if x, ok := v.(*[]string); ok {
if ss, ok := vc.getSlice(nameMapper(name)); ok {
*x = ss
}
return
}
// because slices are different
strv, ok := vc.get(nameMapper(name))
if !ok {
return
}
switch x := v.(type) {
case *int64:
*x, err = ParseInt64(strv)
case *uint64:
*x, err = ParseUint64(strv)
case *float64:
*x, err = strconv.ParseFloat(strv, 64)
case *bool:
*x, err = strconv.ParseBool(strings.ToLower(strv))
case *string:
*x = strv
case *[]byte:
*x = []byte(strv)
default:
err = fmt.Errorf("Cannot store into member %v: unknown type %T", name, v)
}
return
}
func (vc VariableConfig) GetInt(name string) (r int64, err error) {
err = vc.valueMapper(name, &r)
return
}
func (vc VariableConfig) GetUint(name string) (r uint64, err error) {
err = vc.valueMapper(name, &r)
return
}
func (vc VariableConfig) GetFloat(name string) (r float64, err error) {
err = vc.valueMapper(name, &r)
return
}
func (vc VariableConfig) GetBool(name string) (r bool, err error) {
err = vc.valueMapper(name, &r)
return
}
func (vc VariableConfig) GetString(name string) (r string, err error) {
err = vc.valueMapper(name, &r)
return
}
func (vc VariableConfig) GetStringSlice(name string) (r []string, err error) {
if ss, ok := vc.getSlice(nameMapper(name)); ok {
r = ss
}
return
}
func nameMapper(v string) string {
return strings.ReplaceAll(v, "_", "-")
}