/
register.go
456 lines (408 loc) · 20.6 KB
/
register.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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
package contour
import (
"fmt"
"strconv"
)
// RegisterSetting registers a setting. For most settings, the data and setting
// type specific registration and add functions should be used. The exception
// would be when more granular control over what can update a registered
// setting is needed. This method allows Is[ConfFileVar|EnvVar|Flag] bools to
// be set independently.
//
// If a setting with the key k already exists, a SettingExistsError will be
// returned. If k is an empty string an ErrNoSettingName will be returned.
//
// The short, dflt, and usage parms only apply to settings whose IsFlag bool
// is true.
//
// For non-Core settings, IsCore must be false. If IsCore is true, k's value
// cannot be changed after registration, regardless of the truthiness of
// Is[ConfFileVar|IsEnvVar|IsFlag]. For Core settings, AddCore methods should
// be used.
//
// If the setting can be updated by a configuration file, environment variable
// or a flag, the Is[ConfFileVar|IsEnv|IsFlag] bools should be set to true as
// appropriate. These conditionals are independent; e.g. a setting can have
// both IsConfFileVar and IsFlag set to true if the setting is not to be
// updateable from an environment variable.
//
// If Is[Core|ConfFileVar|Env|Flag] are all false, the setting will only be
// updateable by using the Update methods. For these kind of settings, the
// usage of Add functions should be preferred. These setting will not be
// exposed to the configuration file, as an environment variable, or as a flag.
//
// For non string, bool, int, and int64 types, the type must be "interface{}"
func (s *Settings) RegisterSetting(typ, name, short string, value interface{}, dflt, usage string, IsCore, IsConfFileVar, IsEnvVar, IsFlag bool) error {
dType := parseDataType(typ)
s.mu.Lock()
defer s.mu.Unlock()
return s.registerSetting(0, dType, name, short, value, dflt, usage, IsCore, IsConfFileVar, IsEnvVar, IsFlag)
}
func (s *Settings) registerSetting(sTyp SettingType, typ dataType, name, short string, value interface{}, dflt, usage string, IsCore, IsConfFileVar, IsEnvVar, IsFlag bool) error {
if name == "" {
return ErrNoSettingName
}
_, ok := s.settings[name]
if ok {
// Settings can't be re-registered.
return SettingExistsError{typ: sTyp, k: name}
}
// mapping shortcodes make lookup easier
if short != "" && IsFlag {
v, ok := s.shortFlags[short]
if ok {
return ShortFlagExistsError{k: name, short: short, shortName: v}
}
s.shortFlags[short] = name
}
// Add the setting
s.settings[name] = setting{
Type: typ,
Name: name,
Short: short,
Value: value,
Default: dflt,
Usage: usage,
IsCore: IsCore,
IsConfFileVar: IsConfFileVar,
IsEnvVar: IsEnvVar,
IsFlag: IsFlag,
}
// if it's a conf file setting, add it to the confFileVars map
if IsConfFileVar {
s.confFileVars[name] = struct{}{}
}
if IsEnvVar {
s.useEnvVars = IsEnvVar
}
// If a setting is a confFile setting, enable using a conf file.
if IsConfFileVar {
s.useConfFile = true
}
if IsFlag {
s.useFlags = IsFlag
}
return nil
}
// RegisterBoolConfFileVar registers a bool setting using k for its key and v
// for its value. Once registered, the value of this setting can only be
// updated from a configuration file. If k already exists a SettingExistsError
// will be returned. If k is empty, an ErrNoSettingName will be returned.
func (s *Settings) RegisterBoolConfFileVar(k string, v bool) error {
s.mu.Lock()
defer s.mu.Unlock()
return s.registerBoolConfFileVar(k, v)
}
// assumes the lock has been obtained. Unexported register methods always
// return an error.
func (s *Settings) registerBoolConfFileVar(k string, v bool) error {
return s.registerConfFileVar(_bool, k, v, strconv.FormatBool(v))
}
// RegisterIntConfFileVar registers an int setting using k for its key and v
// for its value. Once registered, the value of this setting can only be
// updated from a configuration file. If k already exists a SettingExistsError
// will be returned. If k is empty, an ErrNoSettingName will be returned.
func (s *Settings) RegisterIntConfFileVar(k string, v int) error {
s.mu.Lock()
defer s.mu.Unlock()
return s.registerIntConfFileVar(k, v)
}
// assumes the lock has been obtained. Unexported register methods always
// return an error.
func (s *Settings) registerIntConfFileVar(k string, v int) error {
return s.registerConfFileVar(_int, k, v, strconv.Itoa(v))
}
// RegisterInt64ConfFileVar registers an int64 setting using k for its key and
// v for its value. Once registered, the value of this setting can only be
// updated from a configuration file. If k already exists a SettingExistsError
// will be returned. If k is empty, an ErrNoSettingName will be returned.
func (s *Settings) RegisterInt64ConfFileVar(k string, v int64) error {
s.mu.Lock()
defer s.mu.Unlock()
return s.registerInt64ConfFileVar(k, v)
}
// assumes the lock has been obtained. Unexported register methods always
// return an error.
func (s *Settings) registerInt64ConfFileVar(k string, v int64) error {
return s.registerConfFileVar(_int64, k, v, strconv.FormatInt(v, 10))
}
// RegisterInterfaceConfFileVar registers an int setting using k for its key
// and v for its value. Once registered, the value of this setting can only be
// updated from a configuration file. If k already exists a SettingExistsError
// will be returned. If k is empty, an ErrNoSettingName will be returned.
func (s *Settings) RegisterInterfaceConfFileVar(k string, v interface{}) error {
s.mu.Lock()
defer s.mu.Unlock()
return s.registerInterfaceConfFileVar(k, v)
}
// assumes the lock has been obtained. Unexported register methods always
// return an error.
func (s *Settings) registerInterfaceConfFileVar(k string, v interface{}) error {
return s.registerConfFileVar(_int, k, v, fmt.Sprintf("%v", v))
}
// RegisterStringConfFileVar registers a string setting using k for its key and
// v for its value. Once registered, the value of this setting can only be
// updated from a configuration file. If k already exists a SettingExistsError
// will be returned. If k is empty, an ErrNoSettingName will be returned.
func (s *Settings) RegisterStringConfFileVar(k, v string) error {
s.mu.Lock()
defer s.mu.Unlock()
return s.registerStringConfFileVar(k, v)
}
// assumes the lock has been obtained. Unexported register methods always
// return an error.
func (s *Settings) registerStringConfFileVar(k, v string) error {
return s.registerConfFileVar(_string, k, v, v)
}
func (s *Settings) registerConfFileVar(typ dataType, k string, v interface{}, dflt string) error {
s.useConfFile = true // registerng a conf file var means use a conf file unless explicitly set not to
return s.registerSetting(ConfFileVar, typ, k, "", v, dflt, "", false, true, false, false)
}
// RegisterBoolEnvVar registers a bool setting using k for its key and v for
// its value. Once registered, the value of this setting can only be updated
// from a configuration file or an environment variable. If k already exists a
// SettingExistsError will be returned. If k is empty, an ErrNoSettingName will
// be returned.
func (s *Settings) RegisterBoolEnvVar(k string, v bool) error {
s.mu.Lock()
defer s.mu.Unlock()
return s.registerBoolEnvVar(k, v)
}
// assumes the lock has been obtained. Unexported register methods always
// return an error.
func (s *Settings) registerBoolEnvVar(k string, v bool) error {
return s.registerEnvVar(_bool, k, v, strconv.FormatBool(v))
}
// RegisterIntEnvVar registers an int setting using k for its key and v for its
// value. Once registered, the value of this setting can only be updated from a
// configuration file or an environment variable. If k already exists a
// SettingExistsError will be returned. If k is empty, an ErrNoSettingName will
// be returned.
func (s *Settings) RegisterIntEnvVar(k string, v int) error {
s.mu.Lock()
defer s.mu.Unlock()
return s.registerIntEnvVar(k, v)
}
// assumes the lock has been obtained.
func (s *Settings) registerIntEnvVar(k string, v int) error {
return s.registerEnvVar(_int, k, v, strconv.Itoa(v))
}
// RegisterInt64EnvVar registers an int64 setting using k for its key and v for
// its value. Once registered, the value of this setting can only be updated
// from a configuration file or an environment variable. If k already exists a
// SettingExistsError will be returned. If k is empty, an ErrNoSettingName will
// be returned.
func (s *Settings) RegisterInt64EnvVar(k string, v int64) error {
s.mu.Lock()
defer s.mu.Unlock()
return s.registerInt64EnvVar(k, v)
}
// assumes the lock has been obtained.
func (s *Settings) registerInt64EnvVar(k string, v int64) error {
return s.registerEnvVar(_int64, k, v, strconv.FormatInt(v, 10))
}
// RegisterStringEnvVar registers a string setting using k for its key and v
// for its value. Once registered, the value of this setting can only be
// updated from a configuration file or an environment variable. If k already
// exists a SettingExistsError will be returned. If k is empty, an
// ErrNoSettingName will be returned.
func (s *Settings) RegisterStringEnvVar(k, v string) error {
s.mu.Lock()
defer s.mu.Unlock()
return s.registerStringEnvVar(k, v)
}
// assumes the lock has been obtained.
func (s *Settings) registerStringEnvVar(k, v string) error {
return s.registerEnvVar(_string, k, v, v)
}
func (s *Settings) registerEnvVar(typ dataType, k string, v interface{}, dflt string) error {
s.useConfFile = true // registering a conf file var means use a conf file unless explicitly set not to
s.useEnvVars = true // registering an env var means use env vars unless explictly set not to
return s.registerSetting(EnvVar, typ, k, "", v, dflt, "", false, true, true, false)
}
// RegisterBoolFlag registers a bool setting using k for its key and v for its
// value. Once registered, the value of this setting can be updated from a
// configuration file, an environment variable, or a flag. If k already exists
// a SettingExistsError will be returned. If k is empty, an ErrNoSettingName
// will be returned.
func (s *Settings) RegisterBoolFlag(k, short string, v bool, dflt, usage string) error {
s.mu.Lock()
defer s.mu.Unlock()
return s.registerBoolFlag(k, short, v, dflt, usage)
}
// assumes the lock has been obtained. Unexported register methods always
// return an error.
func (s *Settings) registerBoolFlag(k, short string, v bool, dflt, usage string) error {
return s.registerFlag(_bool, k, short, v, dflt, usage)
}
// RegisterIntFlag registers an int setting using k for its key and v for its
// value. Once registered, the value of this setting can be updated from a
// configuration file, an environment variable, or a flag. If k already exists
// a SettingExistsError will be returned. If k is empty, an ErrNoSettingName
// will be returned
func (s *Settings) RegisterIntFlag(k, short string, v int, dflt, usage string) error {
s.mu.Lock()
defer s.mu.Unlock()
return s.registerIntFlag(k, short, v, dflt, usage)
}
// assumes the lock has been obtained. Unexported register methods always
// return an error.
func (s *Settings) registerIntFlag(k, short string, v int, dflt, usage string) error {
return s.registerFlag(_int, k, short, v, dflt, usage)
}
// RegisterInt64Flag registers an int64 setting using k for its key and v for
// its value. Once registered, the value of this setting can be updated from a
// configuration file, an environment variable, or a flag. If k already exists
// a SettingExistsError will be returned. If k is empty, an ErrNoSettingName
// will be returned
func (s *Settings) RegisterInt64Flag(k, short string, v int64, dflt, usage string) error {
s.mu.Lock()
defer s.mu.Unlock()
return s.registerInt64Flag(k, short, v, dflt, usage)
}
// assumes the lock has been obtained. Unexported register methods always
// return an error.
func (s *Settings) registerInt64Flag(k, short string, v int64, dflt, usage string) error {
return s.registerFlag(_int64, k, short, v, dflt, usage)
}
// RegisterStringFlag registers a string setting using k for its key and v for
// its value. Once registered, the value of this setting can be updated from a
// configuration file, an environment variable, or a flag. If k already exists
// a SettingExistsError will be returned. If k is empty, an ErrNoSettingName
// will be returned
func (s *Settings) RegisterStringFlag(k, short, v, dflt, usage string) error {
s.mu.Lock()
defer s.mu.Unlock()
return s.registerStringFlag(k, short, v, dflt, usage)
}
// assumes the lock has been obtained. Unexported register methods always
// return an error.
func (s *Settings) registerStringFlag(k, short, v, dflt, usage string) error {
return s.registerFlag(_string, k, short, v, dflt, usage)
}
func (s *Settings) registerFlag(typ dataType, k, short string, v interface{}, dflt, usage string) error {
s.useConfFile = true // registering a conf file var means use a conf file unless explicitly set not to
s.useEnvVars = true // registering an env var means use env vars unless explictly set not to
s.useFlags = true // registering a flag means use flags unless explictly set not to
return s.registerSetting(Flag, typ, k, short, v, dflt, usage, false, true, true, true)
}
// RegisterSetting registers a setting with the standard settings. For most
// settings, the data and setting type specific registration and add functions
// should be used. The exception would be when more granular control over what
// can update a registered setting is needed. This method allows
// Is[ConfFileVar|EnvVar|Flag] bools to be set independently.
//
// If a setting with the key k already exists, a SettingExistsError will be
// returned. If k is an empty string an ErrNoSettingName will be returned.
//
// The short, dflt, and usage parms only apply to settings whose IsFlag bool is
// true.
//
// For non-Core settings, IsCore must be false. If IsCore is true, k's value
// cannot be changed after registration, regardless of the truthiness of
// Is[ConfFileVar|IsEnvVar|IsFlag]. For Core settings, AddCore methods should
// be used.
//
// If the setting can be updated by a configuration file, environment variable
// or a flag, the Is[ConfFileVar|IsEnv|IsFlag] bools should be set to true as
// appropriate. These conditionals are independent; e.g. a setting can have
// both IsConfFileVar and IsFlag set to true if the setting is not to be
// updateable from an environment variable.
//
// If Is[Core|ConfFileVar|Env|Flag] are all false, the setting will only be
// updateable by using the Update methods. For these kind of settings, the
// usage of Add functions should be preferred. These setting will not be
// exposed to the configuration file, as an environment variable, or as a flag.
//
// For non string, bool, int, and int64 types, the type must be "interface{}"
func RegisterSetting(typ, name, short string, value interface{}, dflt, usage string, IsCore, IsConfFileVar, IsEnv, IsFlag bool) error {
return std.RegisterSetting(typ, name, short, value, dflt, usage, IsCore, IsConfFileVar, IsEnv, IsFlag)
}
// RegisterBoolConfFileVar registers a bool setting with the standard settings
// using k for its key and v for its value. Once registered, the value of this
// setting can only be updated from a configuration file. If k already exists a
// SettingExistsError will be returned. If k is empty, an ErrNoSettingName will
// be returned.
func RegisterBoolConfFileVar(k string, v bool) error { return std.RegisterBoolConfFileVar(k, v) }
// RegisterIntConfFileVar registers an int setting with the standard settings
// using k for its key and v for its value. Once registered, the value of this
// setting can only be updated from a configuration file. If k already exists a
// SettingExistsError will be returned. If k is empty, an ErrNoSettingName will
// be returned.
func RegisterIntConfFileVar(k string, v int) error { return std.RegisterIntConfFileVar(k, v) }
// RegisterInt64ConfFileVar registers an int64 setting with the standard
// settings using k for its key and v for its value. Once registered, the value
// of this setting can only be updated from a configuration file. If k already
// exists a SettingExistsError will be returned. If k is empty, an
// ErrNoSettingName will be returned.
func RegisterInt64ConfFileVar(k string, v int64) error { return std.RegisterInt64ConfFileVar(k, v) }
// RegisterInterfaceConfFileVar registers an int setting with the standard
// settings using k for its key and v for its value. Once registered, the value
// of this setting can only be updated from a configuration file. If k already
// exists a SettingExistsError will be returned. If k is empty, an
// ErrNoSettingName will be returned.
func RegisterInterfaceConfFileVar(k string, v interface{}) error {
return std.RegisterInterfaceConfFileVar(k, v)
}
// RegisterStringConfFileVar registers a string setting with the standard
// settings using k for its key and v for its value. Once registered, the value
// of this setting can only be updated from a configuration file. If k already
// exists a SettingExistsError will be returned. If k is empty, an
// ErrNoSettingName will be returned.
func RegisterStringConfFileVar(k, v string) error { return std.RegisterStringConfFileVar(k, v) }
// RegisterBoolEnvVar registers a bool setting with the standard settings using
// k for its key and v for its value. Once registered, the value of this
// setting can only be updated from a configuration file or an environment
// variable. If k already exists a SettingExistsError will be returned. If k is
// empty, an ErrNoSettingName will be returned.
func RegisterBoolEnvVar(k string, v bool) error { return std.RegisterBoolEnvVar(k, v) }
// RegisterIntEnvVar registers an int setting with the standard settings using
// k for its key and v for its value. Once registered, the value of this
// setting can only be updated from a configuration file or an environment
// variable. If k already exists a SettingExistsError will be returned. If k is
// empty, an ErrNoSettingName will be returned.
func RegisterIntEnvVar(k string, v int) error { return std.RegisterIntEnvVar(k, v) }
// RegisterInt64EnvVar registers an int64 setting with the standard settings
// using k for its key and v for its value. Once registered, the value of this
// setting can only be updated from a configuration file or an environment
// variable. If k already exists a SettingExistsError will be returned. If k is
// empty, an ErrNoSettingName will be returned.
func RegisterInt64EnvVar(k string, v int64) error { return std.RegisterInt64EnvVar(k, v) }
// RegisterStringEnvVar registers a string setting with the standard settings
// using k for its key and v for its value. Once registered, the value of this
// setting can only be updated from a configuration file or an environment
// variable. If k already exists a SettingExistsError will be returned. If k is
// empty, an ErrNoSettingName will be returned.
func RegisterStringEnvVar(k, v string) error { return std.RegisterStringEnvVar(k, v) }
// RegisterBoolFlag registers a bool setting with the standard settings using k
// for its key and v for its value. Once registered, the value of this setting
// can be updated from a configuration file, an environment variable, or a
// flag. If k already exists a SettingExistsError will be returned. If k is
// empty, an ErrNoSettingName will be returned.
func RegisterBoolFlag(k, short string, v bool, dflt, usage string) error {
return std.RegisterBoolFlag(k, short, v, dflt, usage)
}
// RegisterIntFlag registers an int setting with the standard settings using k
// for its key and v for its value. Once registered, the value of this setting
// can be updated from a configuration file, an environment variable, or a
// flag. If k already exists a SettingExistsError will be returned. If k is
// empty, an ErrNoSettingName will be returned.
func RegisterIntFlag(k, short string, v int, dflt, usage string) error {
return std.RegisterIntFlag(k, short, v, dflt, usage)
}
// RegisterInt64Flag registers an int64 setting with the standard settings
// using k for its key and v for its value. Once registered, the value of this
// setting can be updated from a configuration file, an environment variable,
// or a flag. If k already exists a SettingExistsError will be returned. If k is
// empty, an ErrNoSettingName will be returned.
func RegisterInt64Flag(k, short string, v int64, dflt, usage string) error {
return std.RegisterInt64Flag(k, short, v, dflt, usage)
}
// RegisterStringFlag registers a string setting with the standard settings
// using k for its key and v for its value. Once registered, the value of this
// setting can be updated from a configuration file, an environment variable,
// or a flag. If k already exists a SettingExistsError will be returned. If k is
// empty, an ErrNoSettingName will be returned.
func RegisterStringFlag(k, short, v, dflt, usage string) error {
return std.RegisterStringFlag(k, short, v, dflt, usage)
}