-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
394 lines (335 loc) · 12.9 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
package config
import (
"flag"
"fmt"
"github.com/pelletier/go-toml"
"io/ioutil"
"os"
"strings"
"time"
)
// -- ConfigSet
type ConfigSet struct {
*flag.FlagSet
prefix string
}
// SetPrefix sets the applicatin prefix to use when filtering environment variables.
// If prefix is empty, all environment variables are ignored.
func (c *ConfigSet) SetPrefix(prefix string) {
c.prefix = prefix
}
// BoolVar defines a bool config with a given name and default value for a ConfigSet.
// The argument p points to a bool variable in which to store the value of the config.
func (c *ConfigSet) BoolVar(p *bool, name string, value bool) {
c.FlagSet.BoolVar(p, name, value, "")
}
// Bool defines a bool config variable with a given name and default value for
// a ConfigSet.
func (c *ConfigSet) Bool(name string, value bool) *bool {
return c.FlagSet.Bool(name, value, "")
}
// IntVar defines a int config with a given name and default value for a ConfigSet.
// The argument p points to a int variable in which to store the value of the config.
func (c *ConfigSet) IntVar(p *int, name string, value int) {
c.FlagSet.IntVar(p, name, value, "")
}
// Int defines a int config variable with a given name and default value for a
// ConfigSet.
func (c *ConfigSet) Int(name string, value int) *int {
return c.FlagSet.Int(name, value, "")
}
// Int64Var defines a int64 config with a given name and default value for a ConfigSet.
// The argument p points to a int64 variable in which to store the value of the config.
func (c *ConfigSet) Int64Var(p *int64, name string, value int64) {
c.FlagSet.Int64Var(p, name, value, "")
}
// Int64 defines a int64 config variable with a given name and default value
// for a ConfigSet.
func (c *ConfigSet) Int64(name string, value int64) *int64 {
return c.FlagSet.Int64(name, value, "")
}
// UintVar defines a uint config with a given name and default value for a ConfigSet.
// The argument p points to a uint variable in which to store the value of the config.
func (c *ConfigSet) UintVar(p *uint, name string, value uint) {
c.FlagSet.UintVar(p, name, value, "")
}
// Uint defines a uint config variable with a given name and default value for
// a ConfigSet.
func (c *ConfigSet) Uint(name string, value uint) *uint {
return c.FlagSet.Uint(name, value, "")
}
// Uint64Var defines a uint64 config with a given name and default value for a ConfigSet.
// The argument p points to a uint64 variable in which to store the value of the config.
func (c *ConfigSet) Uint64Var(p *uint64, name string, value uint64) {
c.FlagSet.Uint64Var(p, name, value, "")
}
// Uint64 defines a uint64 config variable with a given name and default value
// for a ConfigSet.
func (c *ConfigSet) Uint64(name string, value uint64) *uint64 {
return c.FlagSet.Uint64(name, value, "")
}
// StringVar defines a string config with a given name and default value for a ConfigSet.
// The argument p points to a string variable in which to store the value of the config.
func (c *ConfigSet) StringVar(p *string, name string, value string) {
c.FlagSet.StringVar(p, name, value, "")
}
// String defines a string config variable with a given name and default value
// for a ConfigSet.
func (c *ConfigSet) String(name string, value string) *string {
return c.FlagSet.String(name, value, "")
}
// Float64Var defines a float64 config with a given name and default value for a ConfigSet.
// The argument p points to a float64 variable in which to store the value of the config.
func (c *ConfigSet) Float64Var(p *float64, name string, value float64) {
c.FlagSet.Float64Var(p, name, value, "")
}
// Float64 defines a float64 config variable with a given name and default
// value for a ConfigSet.
func (c *ConfigSet) Float64(name string, value float64) *float64 {
return c.FlagSet.Float64(name, value, "")
}
// DurationVar defines a time.Duration config with a given name and default value for a ConfigSet.
// The argument p points to a time.Duration variable in which to store the value of the config.
func (c *ConfigSet) DurationVar(p *time.Duration, name string, value time.Duration) {
c.FlagSet.DurationVar(p, name, value, "")
}
// Duration defines a time.Duration config variable with a given name and
// default value.
func (c *ConfigSet) Duration(name string, value time.Duration) *time.Duration {
return c.FlagSet.Duration(name, value, "")
}
// Strings defines a string slice config variable with a given name.
func (c *ConfigSet) Strings(name string) *StringParams {
p := new(StringParams)
c.FlagSet.Var(p, name, "")
return p
}
// Var defines a flag with the specified name and usage string. The type and
// value of the flag are represented by the first argument, of type Value, which
// typically holds a user-defined implementation of Value.
func (c *ConfigSet) Var(value flag.Value, name string) {
c.FlagSet.Var(value, name, "")
}
// Parse takes a path to a TOML file and loads it. This must be called after
// all the config flags in the ConfigSet have been defined but before the flags
// are accessed by the program.
//
// If the path is empty, no TOML file is loaded. Only environment variables
// matching the application Prefix will populate the config flags.
func (c *ConfigSet) Parse(path string) error {
data, err := ioutil.ReadFile(path)
switch {
case err != nil && len(path) != 0:
return err
case err != nil && len(path) == 0:
data = []byte{}
}
return c.ParseBytes(data)
}
// ParseBytes takes a TOML file in byte array format and loads it. This must be
// called after all the config flags in the ConfigSet have been defined but before
// the flags are accessed by the program.
func (c *ConfigSet) ParseBytes(data []byte) error {
tree, err := toml.Load(string(data))
if err != nil {
return err
}
err = c.loadTomlTree(tree, []string{})
if err != nil {
return err
}
if len(c.prefix) != 0 {
envs := parseEnvVars(os.Environ())
err = c.loadEnvVars(envs)
if err != nil {
return err
}
}
return nil
}
// loadTomlTree recursively loads a TomlTree into this ConfigSet's config
// variables.
func (c *ConfigSet) loadTomlTree(tree *toml.TomlTree, path []string) error {
for _, key := range tree.Keys() {
fullPath := append(path, key)
value := tree.Get(key)
if subtree, isTree := value.(*toml.TomlTree); isTree {
err := c.loadTomlTree(subtree, fullPath)
if err != nil {
return err
}
} else {
fullPath := strings.Join(append(path, key), "-")
fullPath = strings.Replace(fullPath, "_", "-", -1)
switch v := value.(type) {
case []interface{}:
var items []string
for _, item := range v {
items = append(items, fmt.Sprintf("%v", item))
}
value = strings.Join(items, ",")
}
// TODO(bradrydzewski) handle []int, []int64, []float64, []float, etc
// TODO(bradrydzewski) handle tables [[ ]] as map[interface{}]interface{}
err := c.Set(fullPath, fmt.Sprintf("%v", value))
if err != nil {
return err
}
}
}
return nil
}
// loadEnvVars loads environment variables into thie ConfigSet's config
// variables if they match the use-defined PREFIX.
func (c *ConfigSet) loadEnvVars(environ map[string]string) error {
for key, value := range environ {
if !strings.HasPrefix(key, c.prefix) {
continue
}
key = strings.ToLower(key)
key = strings.Replace(key, "_", "-", -1)
c.Set(key[len(c.prefix):], value)
}
return nil
}
// parseEnvVars parses a string of environment variables
// into a key-value map.
func parseEnvVars(environ []string) map[string]string {
envs := map[string]string{}
for _, env := range environ {
parts := strings.SplitN(env, "=", 2)
if len(parts) != 2 {
continue
}
envs[parts[0]] = parts[1]
}
return envs
}
const (
ContinueOnError flag.ErrorHandling = flag.ContinueOnError
ExitOnError flag.ErrorHandling = flag.ExitOnError
PanicOnError flag.ErrorHandling = flag.PanicOnError
)
// NewConfigSet returns a new ConfigSet with the given name and error handling
// policy. The three valid error handling policies are: flag.ContinueOnError,
// flag.ExitOnError, and flag.PanicOnError.
func NewConfigSet(name string, errorHandling flag.ErrorHandling) *ConfigSet {
return &ConfigSet{
FlagSet: flag.NewFlagSet(name, errorHandling),
}
}
// -- globalConfig
var globalConfig = NewConfigSet(os.Args[0], flag.ExitOnError)
// SetPrefix sets the applicatin prefix to use when filtering environment variables.
// If prefix is empty, all environment variables are ignored.
func SetPrefix(prefix string) {
globalConfig.prefix = prefix
}
// BoolVar defines a bool config with a given name and default value.
// The argument p points to a bool variable in which to store the value of the config.
func BoolVar(p *bool, name string, value bool) {
globalConfig.BoolVar(p, name, value)
}
// Bool defines a bool config variable with a given name and default value.
func Bool(name string, value bool) *bool {
return globalConfig.Bool(name, value)
}
// IntVar defines a int config with a given name and default value.
// The argument p points to a int variable in which to store the value of the config.
func IntVar(p *int, name string, value int) {
globalConfig.IntVar(p, name, value)
}
// Int defines a int config variable with a given name and default value.
func Int(name string, value int) *int {
return globalConfig.Int(name, value)
}
// Int64Var defines a int64 config with a given name and default value.
// The argument p points to a int64 variable in which to store the value of the config.
func Int64Var(p *int64, name string, value int64) {
globalConfig.Int64Var(p, name, value)
}
// Int64 defines a int64 config variable with a given name and default value.
func Int64(name string, value int64) *int64 {
return globalConfig.Int64(name, value)
}
// UintVar defines a uint config with a given name and default value.
// The argument p points to a uint variable in which to store the value of the config.
func UintVar(p *uint, name string, value uint) {
globalConfig.UintVar(p, name, value)
}
// Uint defines a uint config variable with a given name and default value.
func Uint(name string, value uint) *uint {
return globalConfig.Uint(name, value)
}
// Uint64Var defines a uint64 config with a given name and default value.
// The argument p points to a uint64 variable in which to store the value of the config.
func Uint64Var(p *uint64, name string, value uint64) {
globalConfig.Uint64Var(p, name, value)
}
// Uint64 defines a uint64 config variable with a given name and default value.
func Uint64(name string, value uint64) *uint64 {
return globalConfig.Uint64(name, value)
}
// StringVar defines a string config with a given name and default value.
// The argument p points to a string variable in which to store the value of the config.
func StringVar(p *string, name string, value string) {
globalConfig.StringVar(p, name, value)
}
// String defines a string config variable with a given name and default value.
func String(name string, value string) *string {
return globalConfig.String(name, value)
}
// Float64Var defines a float64 config with a given name and default value.
// The argument p points to a float64 variable in which to store the value of the config.
func Float64Var(p *float64, name string, value float64) {
globalConfig.Float64Var(p, name, value)
}
// Float64 defines a float64 config variable with a given name and default
// value.
func Float64(name string, value float64) *float64 {
return globalConfig.Float64(name, value)
}
// DurationVar defines a time.Duration config with a given name and default value.
// The argument p points to a time.Duration variable in which to store the value of the config.
func DurationVar(p *time.Duration, name string, value time.Duration) {
globalConfig.DurationVar(p, name, value)
}
// Duration defines a time.Duration config variable with a given name and
// default value.
func Duration(name string, value time.Duration) *time.Duration {
return globalConfig.Duration(name, value)
}
// Strings defines a string slice config variable with a given name.
func Strings(name string) *StringParams {
return globalConfig.Strings(name)
}
// Var defines a flag with the specified name and usage string. The type and value of the
// flag are represented by the first argument, of type Value, which typically holds a
// user-defined implementation of Value.
func Var(value flag.Value, name string) {
globalConfig.Var(value, name)
}
// Parse takes a path to a TOML file and loads it into the global ConfigSet.
// This must be called after all config flags have been defined but before the
// flags are accessed by the program.
func Parse(path string) error {
return globalConfig.Parse(path)
}
// ParseBytes takes a TOML file in byte array format and loads it into the
// global ConfigSet. This must be called after all config flags have been defined
// but before the flags are accessed by the program.
func ParseBytes(data []byte) error {
return globalConfig.ParseBytes(data)
}
// -- Custom Types
type StringParams []string
func (s *StringParams) String() string {
return fmt.Sprint(*s)
}
func (s *StringParams) Set(value string) error {
for _, ss := range strings.Split(value, ",") {
ss = strings.TrimSpace(ss)
*s = append(*s, ss)
}
return nil
}