-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
411 lines (359 loc) · 13.1 KB
/
config.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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/*
Copyright © 2022 ITRS Group
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
/*
This package adds local extensions to viper as well as supporting Geneos
encryption key files and basic encryption and decryption.
*/
package config
import (
"fmt"
"path"
"reflect"
"strconv"
"strings"
"github.com/mitchellh/mapstructure"
"github.com/spf13/viper"
)
// Config embeds Viper
type Config struct {
*viper.Viper
Type string // The type of configuration file loaded
defaultExpandOptions []ExpandOptions
delimiter string
appUserConfDir string
}
// global is the default configuration container for non-method callers
var global *Config
func init() {
// global = &Config{Viper: viper.NewWithOptions()}
global = New()
}
// GetConfig returns the global Config instance
func GetConfig() *Config {
return global
}
// ResetConfig reinitialises the global configuration object. Existing
// settings will be copied over. This is primarily to be able to change
// the default delimiter after start-up.
func ResetConfig(options ...FileOptions) {
tmp := global.AllSettings()
global = New(options...)
global.MergeConfigMap(tmp)
}
// New returns a Config instance initialised with a new viper instance.
// Can be called with config.DefaultExpandOptions(...) to set defaults for
// future calls that use Expand.
func New(options ...FileOptions) *Config {
opts := evalFileOptions(options...)
userConfDir, _ := UserConfigDir()
cf := &Config{
Viper: viper.NewWithOptions(
viper.KeyDelimiter(opts.delimiter),
viper.EnvKeyReplacer(strings.NewReplacer(opts.delimiter, opts.envdelimiter))),
delimiter: opts.delimiter,
appUserConfDir: path.Join(userConfDir, opts.appname),
}
if opts.envprefix != "" {
cf.SetEnvPrefix(opts.envprefix)
cf.AutomaticEnv()
}
return cf
}
// AppConfigDir returns the application configuration directory
func AppConfigDir() string {
return global.appUserConfDir
}
// AppConfigDir returns the application configuration directory
func (c *Config) AppConfigDir() string {
return c.appUserConfDir
}
// Join returns a configuration key made up of parts joined with the
// default delimiter for the global configuration object.
func Join(parts ...string) string {
elems := []string{}
return strings.Join(append(elems, parts...), global.delimiter)
}
// Join returns a configuration settings key joined with the delimiter
// for the c config object.
func (c *Config) Join(parts ...string) string {
elems := []string{}
return strings.Join(append(elems, parts...), c.delimiter)
}
// Sub returns a Config instance rooted at the key passed
func (c *Config) Sub(key string) *Config {
return &Config{Viper: c.Viper.Sub(key)}
}
// Set sets the key to value
func Set(key string, value interface{}) {
global.Set(key, value)
}
// SetMap iterates over a map[string]string and sets each key to the
// value given. Viper's Set() doesn't support maps until the
// configuration is written to and read back from a file.
func (c *Config) SetStringMapString(m string, vals map[string]string) {
for k, v := range vals {
c.Set(m+c.delimiter+k, v)
}
}
// SetMap iterates over a map[string]string and sets each key to the
// value given. Viper's Set() doesn't support maps until the
// configuration is written to and read back from a file.
func SetStringMapString(m string, vals map[string]string) {
global.SetStringMapString(m, vals)
}
// GetString functions like [viper.GetString] but additionally calls
// [ExpandString] with the configuration value, passing any "values" maps
func GetString(s string, options ...ExpandOptions) string {
return global.GetString(s, options...)
}
// GetString functions like [viper.GetString] on a Config instance, but
// additionally calls [ExpandString] with the configuration value, passing
// any "values" maps
func (c *Config) GetString(s string, options ...ExpandOptions) string {
return c.ExpandString(c.Viper.GetString(s), options...)
}
// GetPassword returns a sealed enclave containing the configuration item
// identified by key and expanded using the Expand function with the
// options supplied.
func GetPassword(s string, options ...ExpandOptions) *Plaintext {
return global.GetPassword(s, options...)
}
// GetPassword returns a sealed enclave containing the configuration item
// identified by key and expanded using the Expand function with the
// options supplied.
func (c *Config) GetPassword(key string, options ...ExpandOptions) *Plaintext {
return &Plaintext{c.ExpandToEnclave(c.Viper.GetString(key), options...)}
}
// GetInt functions like [viper.GetInt] but additionally calls
// [ExpandString] with the configuration value, passing any "values"
// maps. If the conversion fails then the value returned will be the one
// from [strconv.ParseInt] - typically 0 but can be the maximum integer
// value
func GetInt(s string, options ...ExpandOptions) int {
return global.GetInt(s, options...)
}
// GetInt functions like [viper.GetInt] on a Config instance, but
// additionally calls [ExpandString] with the configuration value,
// passing any "values" maps, before converting the result to an int. If
// the conversion fails then the value returned will be the one from
// [strconv.ParseInt] - typically 0 but can be the maximum integer value
func (c *Config) GetInt(s string, options ...ExpandOptions) (i int) {
value := c.ExpandString(c.Viper.GetString(s), options...)
i, _ = strconv.Atoi(value)
return
}
// GetInt64 functions like [viper.GetInt] but additionally calls
// [ExpandString] with the configuration value, passing any "values"
// maps. If the conversion fails then the value returned will be the one
// from [strconv.ParseInt] - typically 0 but can be the maximum integer
// value
func GetInt64(s string, options ...ExpandOptions) int64 {
return global.GetInt64(s, options...)
}
// GetInt64 functions like [viper.GetInt] on a Config instance, but
// additionally calls [ExpandString] with the configuration value,
// passing any "values" maps, before converting the result to an int. If
// the conversion fails then the value returned will be the one from
// [strconv.ParseInt] - typically 0 but can be the maximum integer value
func (c *Config) GetInt64(s string, options ...ExpandOptions) (i int64) {
value := c.ExpandString(c.Viper.GetString(s), options...)
i, _ = strconv.ParseInt(value, 10, 64)
return
}
// GetBytes functions like [viper.GetString] but additionally calls
// [Expand] with the configuration value, passing any "values" maps and
// returning a byte slice
func GetBytes(s string, options ...ExpandOptions) []byte {
return global.GetBytes(s, options...)
}
// GetBytes functions like [viper.GetString] on a Config instance, but
// additionally calls [Expand] with the configuration value, passing
// any "values" maps and returning a byte slice
func (c *Config) GetBytes(s string, options ...ExpandOptions) []byte {
return c.Expand(c.Viper.GetString(s), options...)
}
// GetStringSlice functions like [viper.GetStringSlice] but additionally calls
// [ExpandString] on each element of the slice, passing any "values" maps
func GetStringSlice(s string, options ...ExpandOptions) []string {
return global.GetStringSlice(s, options...)
}
// GetStringSlice functions like [viper.GetStringSlice] on a Config
// instance but additionally calls [ExpandString] on each element of the
// slice, passing any "values" maps
func (c *Config) GetStringSlice(s string, options ...ExpandOptions) (slice []string) {
var result []string
opts := evalExpandOptions(c, options...)
if c.Viper.IsSet(s) {
result = c.Viper.GetStringSlice(s)
} else if init, ok := opts.initialValue.([]string); ok {
result = init
}
if len(result) == 0 {
if def, ok := opts.defaultValue.([]string); ok {
result = def
}
}
for _, n := range result {
slice = append(slice, c.ExpandString(n, options...))
}
return
}
func isStringMapInterface(val interface{}) bool {
vt := reflect.TypeOf(val)
return vt.Kind() == reflect.Map &&
vt.Key().Kind() == reflect.String &&
vt.Elem().Kind() == reflect.Interface
}
// GetStringMapString functions like [viper.GetStringMapString] but additionally calls
// [ExpandString] on each value element of the map, passing any "values" maps
func GetStringMapString(s string, options ...ExpandOptions) map[string]string {
return global.GetStringMapString(s, options...)
}
// GetStringMapString functions like [viper.GetStringMapString] on a
// Config instance but additionally calls [ExpandString] on each value
// element of the map, passing any "values" maps
//
// Use a version of https://github.com/spf13/viper/pull/1504 to fix viper bug #1106
func (c *Config) GetStringMapString(key string, options ...ExpandOptions) (m map[string]string) {
m = make(map[string]string)
key = strings.ToLower(key)
prefix := key + c.delimiter
i := c.Viper.Get(key)
if !isStringMapInterface(i) {
return
}
val := i.(map[string]interface{})
keys := c.AllKeys()
for _, k := range keys {
if !strings.HasPrefix(k, prefix) {
continue
}
mk := strings.TrimPrefix(key, prefix)
mk = strings.Split(mk, c.delimiter)[0]
if _, exists := val[mk]; exists {
continue
}
mv := c.Get(key + c.delimiter + mk)
if mv == nil {
continue
}
val[mk] = mv
}
for k, v := range val {
m[k] = c.ExpandString(fmt.Sprint(v), options...)
}
// r := c.Viper.GetStringMapString(key)
// for k, v := range r {
// m[k] = c.ExpandString(v, options...)
// }
return
}
// defaultDecoderConfig returns default mapstructure.DecoderConfig with support
// of time.Duration values & string slices
func defaultDecoderConfig(output interface{}, opts ...viper.DecoderConfigOption) *mapstructure.DecoderConfig {
c := &mapstructure.DecoderConfig{
Metadata: nil,
Result: output,
WeaklyTypedInput: true,
DecodeHook: mapstructure.ComposeDecodeHookFunc(
mapstructure.StringToTimeDurationHookFunc(),
mapstructure.StringToSliceHookFunc(","),
),
}
for _, opt := range opts {
opt(c)
}
return c
}
// A wrapper around mapstructure.Decode that mimics the WeakDecode functionality
func decode(input interface{}, config *mapstructure.DecoderConfig) error {
decoder, err := mapstructure.NewDecoder(config)
if err != nil {
return err
}
return decoder.Decode(input)
}
func UnmarshalKey(key string, rawVal interface{}, opts ...viper.DecoderConfigOption) error {
return global.UnmarshalKey(key, rawVal, opts...)
}
func (c *Config) UnmarshalKey(key string, rawVal interface{}, opts ...viper.DecoderConfigOption) error {
key = strings.ToLower(key)
prefix := key + c.delimiter
i := c.Viper.Get(key)
if isStringMapInterface(i) {
val := i.(map[string]interface{})
keys := c.AllKeys()
for _, k := range keys {
if !strings.HasPrefix(k, prefix) {
continue
}
mk := strings.TrimPrefix(k, prefix)
mk = strings.Split(mk, c.delimiter)[0]
if _, exists := val[mk]; exists {
continue
}
mv := c.Get(key + c.delimiter + mk)
if mv == nil {
continue
}
val[mk] = mv
}
i = val
}
return decode(i, defaultDecoderConfig(rawVal, opts...))
}
// GetSliceStringMapString returns a slice of string maps for the key s,
// it iterates over all values in all maps and applies the ExpandString
// with the options given
func (c *Config) GetSliceStringMapString(s string, options ...ExpandOptions) (result []map[string]string) {
err := c.UnmarshalKey(s, &result)
if err != nil {
return
}
for _, m := range result {
for k, v := range m {
m[k] = ExpandString(v, options...)
}
}
return
}
// GetSliceStringMapString returns a slice of string maps for the key s,
// it iterates over all values in all maps and applies the ExpandString
// with the options given
func GetSliceStringMapString(s string, options ...ExpandOptions) (result []map[string]string) {
return global.GetSliceStringMapString(s, options...)
}
// SetKeyValues takes a list of `key=value` pairs as strings and applies
// them to the config object. Any item without an `=` is skipped.
func (c *Config) SetKeyValues(items ...string) {
for _, item := range items {
if !strings.Contains(item, "=") {
continue
}
s := strings.SplitN(item, "=", 2)
k, v := s[0], s[1]
c.Set(k, v)
}
}
// SetKeyValues takes a list of `key-value` pairs as strings and
// applies them to the global configuration object. Items without an `=`
// are skipped.
func SetKeyValues(items ...string) {
global.SetKeyValues(items...)
}