forked from DataDog/datadog-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
viper.go
366 lines (322 loc) · 9.14 KB
/
viper.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
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016-2019 Datadog, Inc.
package config
import (
"io"
"strings"
"sync"
"time"
"github.com/ninnemana/datadog-agent/pkg/util/log"
"github.com/spf13/viper"
"github.com/spf13/afero"
"github.com/spf13/pflag"
)
// safeConfig implements Config:
// - wraps viper with a safety lock
// - implements the additional DDHelpers
type safeConfig struct {
*viper.Viper
sync.RWMutex
envPrefix string
configEnvVars []string
}
// Set wraps Viper for concurrent access
func (c *safeConfig) Set(key string, value interface{}) {
c.Lock()
defer c.Unlock()
c.Viper.Set(key, value)
}
// SetDefault wraps Viper for concurrent access
func (c *safeConfig) SetDefault(key string, value interface{}) {
c.Lock()
defer c.Unlock()
c.Viper.SetDefault(key, value)
}
// SetKnown adds a key to the set of known valid config keys
func (c *safeConfig) SetKnown(key string) {
c.Lock()
defer c.Unlock()
c.Viper.SetKnown(key)
}
// GetKnownKeys returns all the keys that meet at least one of these criteria:
// 1) have a default, 2) have an environment variable binded or 3) have been SetKnown()
func (c *safeConfig) GetKnownKeys() map[string]interface{} {
c.Lock()
defer c.Unlock()
return c.Viper.GetKnownKeys()
}
// SetFs wraps Viper for concurrent access
func (c *safeConfig) SetFs(fs afero.Fs) {
c.Lock()
defer c.Unlock()
c.Viper.SetFs(fs)
}
// IsSet wraps Viper for concurrent access
func (c *safeConfig) IsSet(key string) bool {
c.RLock()
defer c.RUnlock()
return c.Viper.IsSet(key)
}
// Get wraps Viper for concurrent access
func (c *safeConfig) Get(key string) interface{} {
c.RLock()
defer c.RUnlock()
val, err := c.Viper.GetE(key)
if err != nil {
log.Warnf("failed to get configuration value for key %q: %s", key, err)
}
return val
}
// GetString wraps Viper for concurrent access
func (c *safeConfig) GetString(key string) string {
c.RLock()
defer c.RUnlock()
val, err := c.Viper.GetStringE(key)
if err != nil {
log.Warnf("failed to get configuration value for key %q: %s", key, err)
}
return val
}
// GetBool wraps Viper for concurrent access
func (c *safeConfig) GetBool(key string) bool {
c.RLock()
defer c.RUnlock()
val, err := c.Viper.GetBoolE(key)
if err != nil {
log.Warnf("failed to get configuration value for key %q: %s", key, err)
}
return val
}
// GetInt wraps Viper for concurrent access
func (c *safeConfig) GetInt(key string) int {
c.RLock()
defer c.RUnlock()
val, err := c.Viper.GetIntE(key)
if err != nil {
log.Warnf("failed to get configuration value for key %q: %s", key, err)
}
return val
}
// GetInt64 wraps Viper for concurrent access
func (c *safeConfig) GetInt64(key string) int64 {
c.RLock()
defer c.RUnlock()
val, err := c.Viper.GetInt64E(key)
if err != nil {
log.Warnf("failed to get configuration value for key %q: %s", key, err)
}
return val
}
// GetFloat64 wraps Viper for concurrent access
func (c *safeConfig) GetFloat64(key string) float64 {
c.RLock()
defer c.RUnlock()
val, err := c.Viper.GetFloat64E(key)
if err != nil {
log.Warnf("failed to get configuration value for key %q: %s", key, err)
}
return val
}
// GetTime wraps Viper for concurrent access
func (c *safeConfig) GetTime(key string) time.Time {
c.RLock()
defer c.RUnlock()
val, err := c.Viper.GetTimeE(key)
if err != nil {
log.Warnf("failed to get configuration value for key %q: %s", key, err)
}
return val
}
// GetDuration wraps Viper for concurrent access
func (c *safeConfig) GetDuration(key string) time.Duration {
c.RLock()
defer c.RUnlock()
val, err := c.Viper.GetDurationE(key)
if err != nil {
log.Warnf("failed to get configuration value for key %q: %s", key, err)
}
return val
}
// GetStringSlice wraps Viper for concurrent access
func (c *safeConfig) GetStringSlice(key string) []string {
c.RLock()
defer c.RUnlock()
val, err := c.Viper.GetStringSliceE(key)
if err != nil {
log.Warnf("failed to get configuration value for key %q: %s", key, err)
}
return val
}
// GetStringMap wraps Viper for concurrent access
func (c *safeConfig) GetStringMap(key string) map[string]interface{} {
c.RLock()
defer c.RUnlock()
val, err := c.Viper.GetStringMapE(key)
if err != nil {
log.Warnf("failed to get configuration value for key %q: %s", key, err)
}
return val
}
// GetStringMapString wraps Viper for concurrent access
func (c *safeConfig) GetStringMapString(key string) map[string]string {
c.RLock()
defer c.RUnlock()
val, err := c.Viper.GetStringMapStringE(key)
if err != nil {
log.Warnf("failed to get configuration value for key %q: %s", key, err)
}
return val
}
// GetStringMapStringSlice wraps Viper for concurrent access
func (c *safeConfig) GetStringMapStringSlice(key string) map[string][]string {
c.RLock()
defer c.RUnlock()
val, err := c.Viper.GetStringMapStringSliceE(key)
if err != nil {
log.Warnf("failed to get configuration value for key %q: %s", key, err)
}
return val
}
// GetSizeInBytes wraps Viper for concurrent access
func (c *safeConfig) GetSizeInBytes(key string) uint {
c.RLock()
defer c.RUnlock()
val, err := c.Viper.GetSizeInBytesE(key)
if err != nil {
log.Warnf("failed to get configuration value for key %q: %s", key, err)
}
return val
}
// SetEnvPrefix wraps Viper for concurrent access, and keeps the envPrefix for
// future reference
func (c *safeConfig) SetEnvPrefix(in string) {
c.Lock()
defer c.Unlock()
c.Viper.SetEnvPrefix(in)
c.envPrefix = in
}
// BindEnv wraps Viper for concurrent access, and adds tracking of the configurable env vars
func (c *safeConfig) BindEnv(input ...string) error {
c.Lock()
defer c.Unlock()
if len(input) == 1 {
// FIXME: for the purposes of GetEnvVars implementation, we only track env var keys
// that are interpolated by viper from the config option key name
key := input[0]
envVarName := strings.Join([]string{c.envPrefix, strings.ToUpper(key)}, "_")
c.configEnvVars = append(c.configEnvVars, envVarName)
}
return c.Viper.BindEnv(input...)
}
// SetEnvKeyReplacer wraps Viper for concurrent access
func (c *safeConfig) SetEnvKeyReplacer(r *strings.Replacer) {
c.RLock()
defer c.RUnlock()
c.Viper.SetEnvKeyReplacer(r)
}
// UnmarshalKey wraps Viper for concurrent access
func (c *safeConfig) UnmarshalKey(key string, rawVal interface{}) error {
c.Lock()
defer c.Unlock()
return c.Viper.UnmarshalKey(key, rawVal)
}
// Unmarshal wraps Viper for concurrent access
func (c *safeConfig) Unmarshal(rawVal interface{}) error {
c.Lock()
defer c.Unlock()
return c.Viper.Unmarshal(rawVal)
}
// UnmarshalExact wraps Viper for concurrent access
func (c *safeConfig) UnmarshalExact(rawVal interface{}) error {
c.Lock()
defer c.Unlock()
return c.Viper.UnmarshalExact(rawVal)
}
// ReadInConfig wraps Viper for concurrent access
func (c *safeConfig) ReadInConfig() error {
c.Lock()
defer c.Unlock()
return c.Viper.ReadInConfig()
}
// ReadConfig wraps Viper for concurrent access
func (c *safeConfig) ReadConfig(in io.Reader) error {
c.Lock()
defer c.Unlock()
return c.Viper.ReadConfig(in)
}
// MergeConfig wraps Viper for concurrent access
func (c *safeConfig) MergeConfig(in io.Reader) error {
c.Lock()
defer c.Unlock()
return c.Viper.MergeConfig(in)
}
// MergeConfigOverride wraps Viper for concurrent access
func (c *safeConfig) MergeConfigOverride(in io.Reader) error {
c.Lock()
defer c.Unlock()
return c.Viper.MergeConfigOverride(in)
}
// AllSettings wraps Viper for concurrent access
func (c *safeConfig) AllSettings() map[string]interface{} {
c.Lock()
defer c.Unlock()
return c.Viper.AllSettings()
}
// AddConfigPath wraps Viper for concurrent access
func (c *safeConfig) AddConfigPath(in string) {
c.Lock()
defer c.Unlock()
c.Viper.AddConfigPath(in)
}
// SetConfigName wraps Viper for concurrent access
func (c *safeConfig) SetConfigName(in string) {
c.Lock()
defer c.Unlock()
c.Viper.SetConfigName(in)
}
// SetConfigFile wraps Viper for concurrent access
func (c *safeConfig) SetConfigFile(in string) {
c.Lock()
defer c.Unlock()
c.Viper.SetConfigFile(in)
}
// SetConfigType wraps Viper for concurrent access
func (c *safeConfig) SetConfigType(in string) {
c.Lock()
defer c.Unlock()
c.Viper.SetConfigType(in)
}
// ConfigFileUsed wraps Viper for concurrent access
func (c *safeConfig) ConfigFileUsed() string {
c.Lock()
defer c.Unlock()
return c.Viper.ConfigFileUsed()
}
// BindPFlag wraps Viper for concurrent access
func (c *safeConfig) BindPFlag(key string, flag *pflag.Flag) error {
c.Lock()
defer c.Unlock()
return c.Viper.BindPFlag(key, flag)
}
// GetEnvVars implements the Config interface
func (c *safeConfig) GetEnvVars() []string {
return c.configEnvVars
}
// BindEnvAndSetDefault implements the Config interface
func (c *safeConfig) BindEnvAndSetDefault(key string, val interface{}) {
c.SetDefault(key, val)
c.BindEnv(key)
}
// NewConfig returns a new Config object.
func NewConfig(name string, envPrefix string, envKeyReplacer *strings.Replacer) Config {
config := safeConfig{
Viper: viper.New(),
}
config.SetConfigName(name)
config.SetEnvPrefix(envPrefix)
config.SetEnvKeyReplacer(envKeyReplacer)
config.SetTypeByDefaultValue(true)
return &config
}