forked from jbert/gop
/
config.go
437 lines (384 loc) · 12.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
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
package gop
import (
"encoding/json"
"io/ioutil"
"log"
"time"
"github.com/vaughan0/go-ini"
"fmt"
"os"
"strconv"
"strings"
)
type ConfigSource interface {
Get(sectionName, optionName string, defaultValue string) (string, bool)
Add(sectionName, optionName, optionValue string)
Sections() []string
SectionKeys(sectionName string) []string
}
type Config struct {
source ConfigMap
persistentOverrides ConfigMap
transientOverrides ConfigMap
overrideFname string
onChangeCallbacks []func(cfg *Config)
}
type ConfigMap map[string]map[string]string
func (a *App) getConfigFilename(forceCurrentWorkingDir bool) string {
rootEnvName := strings.ToUpper(a.ProjectName) + "_CFG_ROOT"
configRoot := os.Getenv(rootEnvName)
if configRoot == "" {
configRoot = "/etc/" + a.ProjectName
}
if forceCurrentWorkingDir {
configRoot = "."
}
fileEnvName := strings.ToUpper(a.ProjectName) + "_" + strings.ToUpper(a.AppName) + "_CFG_FILE"
configFname := os.Getenv(fileEnvName)
if configFname == "" {
configFname = configRoot + "/" + a.AppName + ".conf"
}
return configFname
}
func (cm *ConfigMap) loadFromIniFile(fname string) error {
iniCfg, err := ini.LoadFile(fname)
if err != nil {
return err
}
for section, m := range iniCfg {
for k, v := range m {
cm.Add(section, k, v)
}
}
return nil
}
func (cm *ConfigMap) loadFromJsonFile(fname string) error {
overrideJsonBytes, err := ioutil.ReadFile(fname)
if err != nil {
return err
}
err = json.Unmarshal(overrideJsonBytes, cm)
if err != nil {
return err
}
return nil
}
func (cm *ConfigMap) saveToJsonFile(fname string) error {
jsonBytes, err := json.Marshal(cm)
if err != nil {
return err
}
return ioutil.WriteFile(fname, jsonBytes, 0644)
}
func (a *App) loadAppConfigFile(requireConfig bool) {
// We do not have logging set up yet. We just panic() on error.
// Set up a null config so we have the structure in place on early return
source := make(ConfigMap)
persistentOverrides := make(ConfigMap)
a.Cfg = Config{
source: source,
persistentOverrides: persistentOverrides,
transientOverrides: make(ConfigMap),
onChangeCallbacks: make([]func(cfg *Config), 0),
}
configFname := a.getConfigFilename(false)
err := source.loadFromIniFile(configFname)
if err != nil && !os.IsNotExist(err) {
// Can't log, it's all too early. This is fatal, tho
panic(fmt.Sprintf("Can't load config file [%s]: %s", configFname, err.Error()))
}
if err != nil {
// Try again in cwd
configFname = a.getConfigFilename(true)
err = source.loadFromIniFile(configFname)
if err != nil {
if os.IsNotExist(err) && !requireConfig {
// OK - you're allowed to not fail in this case
return
}
// Can't log, it's all too early. This is fatal, tho
panic(fmt.Sprintf("Can't load config file [%s] after fallback to cwd: %s", configFname, err.Error()))
}
}
a.Cfg.overrideFname = configFname + ".override"
fi, err := os.Stat(a.Cfg.overrideFname)
if err == nil && fi.Size() > 0 {
err = persistentOverrides.loadFromJsonFile(a.Cfg.overrideFname)
if err != nil {
// Don't have logging yet, so use log. and hope
log.Printf("Failed to load or parse override config file [%s]: %s\n", a.Cfg.overrideFname, err.Error())
// Don't want to fail here, just continue without overrides
err = nil
}
}
}
// Get an option value for the given sectionName.
// Will return defaultValue if the section or the option does not exist.
// The second return value is True if the requested option value was returned and False if the default value was returned.
func (cfgMap *ConfigMap) Get(sectionName, optionName string, defaultValue string) (string, bool) {
s, ok := map[string]map[string]string(*cfgMap)[sectionName]
if !ok {
return defaultValue, false
}
v, ok := map[string]string(s)[optionName]
if !ok {
return defaultValue, false
}
return v, true
}
// Set the given option to the specified value for the named section.
// Create the section if it does not exist.
func (cfgMap *ConfigMap) Add(sectionName, optionName, optionValue string) {
_, ok := (*cfgMap)[sectionName]
if !ok {
(*cfgMap)[sectionName] = make(map[string]string)
}
(*cfgMap)[sectionName][optionName] = optionValue
}
// Get a list of the names of the avaliable sections.
func (cfgMap *ConfigMap) Sections() []string {
sections := make([]string, 0)
for k := range *cfgMap {
sections = append(sections, k)
}
return sections
}
// Get a list of options for the named section.
// Will return an empty list if the section does not exist.
func (cfgMap *ConfigMap) SectionKeys(sectionName string) []string {
keys := make([]string, 0)
section, ok := (*cfgMap)[sectionName]
if !ok {
return keys
}
for k := range section {
keys = append(keys, k)
}
return keys
}
func (cfg *Config) AddOnChangeCallback(f func(cfg *Config)) {
cfg.onChangeCallbacks = append(cfg.onChangeCallbacks, f)
}
func (cfg *Config) notifyChange() {
for _, f := range cfg.onChangeCallbacks {
// These should be quick!
f(cfg)
}
}
func (cfg *Config) savePersistentOverrides() error {
return cfg.persistentOverrides.saveToJsonFile(cfg.overrideFname)
}
// Get a list of the names of the available sections, including those specified in the override file.
func (cfg *Config) Sections() []string {
sectionMap := make(map[string]bool)
sourceSections := cfg.source.Sections()
for _, section := range sourceSections {
sectionMap[section] = true
}
for section := range cfg.persistentOverrides {
sectionMap[section] = true
}
for section := range cfg.transientOverrides {
sectionMap[section] = true
}
sections := make([]string, 0)
for k := range sectionMap {
sections = append(sections, k)
}
return sections
}
// Get a list of options for the named section, including those specified in the override file.
func (cfg *Config) SectionKeys(sectionName string) []string {
keyMap := make(map[string]bool)
sourceKeys := cfg.source.SectionKeys(sectionName)
for _, key := range sourceKeys {
keyMap[key] = true
}
overrideSection, ok := cfg.persistentOverrides[sectionName]
if ok {
for key := range overrideSection {
keyMap[key] = true
}
}
overrideSection, ok = cfg.transientOverrides[sectionName]
if ok {
for key := range overrideSection {
keyMap[key] = true
}
}
keys := make([]string, 0)
for k := range keyMap {
keys = append(keys, k)
}
return keys
}
// Get a copy of the config as a map that maps each section to a map that maps the options to the values.
func (cfg *Config) AsMap() map[string]map[string]string {
configMap := make(map[string]map[string]string)
sections := cfg.Sections()
for _, section := range sections {
configMap[section] = make(map[string]string)
keys := cfg.SectionKeys(section)
for _, key := range keys {
configMap[section][key], _ = cfg.Get(section, key, "")
}
}
return configMap
}
func (cfg *Config) PersistentOverride(sectionName, optionName, optionValue string) {
section, ok := cfg.persistentOverrides[sectionName]
if !ok {
cfg.persistentOverrides[sectionName] = make(map[string]string)
section = cfg.persistentOverrides[sectionName]
}
section[optionName] = optionValue
err := cfg.savePersistentOverrides()
if err != nil {
log.Printf("Failed to save to override file [%s]: %s\n", cfg.overrideFname, err.Error())
}
cfg.notifyChange()
return
}
func (cfg *Config) TransientOverride(sectionName, optionName, optionValue string) {
section, ok := cfg.transientOverrides[sectionName]
if !ok {
cfg.transientOverrides[sectionName] = make(map[string]string)
section = cfg.transientOverrides[sectionName]
}
section[optionName] = optionValue
cfg.notifyChange()
return
}
func (cfg *Config) Get(sectionName, optionName string, defaultValue string) (string, bool) {
str, found := cfg.transientOverrides.Get(sectionName, optionName, defaultValue)
if found {
return str, true
}
str, found = cfg.persistentOverrides.Get(sectionName, optionName, defaultValue)
if found {
return str, true
}
// Not found, just punt it to the base
return cfg.source.Get(sectionName, optionName, defaultValue)
}
// Same as Config.Get, but returns the value as int.
func (cfg *Config) GetInt(sectionName, optionName string, defaultValue int) (int, bool) {
v, found := cfg.Get(sectionName, optionName, "")
if !found {
return defaultValue, false
}
r, err := strconv.Atoi(v)
if err == nil {
return r, true
}
panic(fmt.Sprintf("Non-numeric config key %s: %s [%s]", optionName, v, err))
}
// Same as Config.Get, but returns the value as int64.
// The integer has to be written in the config in decimal format. This means that for the value written in
// the config as "08" this method will return 8 instead of 10. And "0x8" will generate an error.
func (cfg *Config) GetInt64(sectionName, optionName string, defaultValue int64) (int64, bool) {
v, found := cfg.Get(sectionName, optionName, "")
if !found {
return defaultValue, false
}
r, err := strconv.ParseInt(v, 10, 64)
if err == nil {
return r, true
}
panic(fmt.Sprintf("Non-numeric config key %s: %s [%s]", optionName, v, err))
}
// Same as Config.Get, but returns the value as boolean.
// The option value should be one that strconv.ParseBool understands.
func (cfg *Config) GetBool(sectionName, optionName string, defaultValue bool) (bool, bool) {
v, found := cfg.Get(sectionName, optionName, "")
if !found {
return defaultValue, false
}
r, err := strconv.ParseBool(v)
if err == nil {
return r, true
}
panic(fmt.Sprintf("Bad boolean config key %s: %s", optionName, v))
}
// Same as Config.Get, but returns the value as float32.
func (cfg *Config) GetFloat32(sectionName, optionName string, defaultValue float32) (float32, bool) {
v, found := cfg.Get(sectionName, optionName, "")
if !found {
return defaultValue, false
}
r, err := strconv.ParseFloat(v, 32)
if err == nil {
return float32(r), true
}
panic(fmt.Sprintf("Non-numeric float32 config key %s: %s [%s]", optionName, v, err))
}
// Same as Config.Get, but returns the value as float64
func (cfg *Config) GetFloat64(sectionName, optionName string, defaultValue float64) (float64, bool) {
v, found := cfg.Get(sectionName, optionName, "")
if !found {
return defaultValue, false
}
r, err := strconv.ParseFloat(v, 64)
if err == nil {
return float64(r), true
}
panic(fmt.Sprintf("Non-numeric float64 config key %s: %s [%s]", optionName, v, err))
}
// Return a list of strings for a config value that is written as a comma-separated list.
// Each value will be stripped out of leading and trailing white spaces as defined by Unicode.
func (cfg *Config) GetList(sectionName, optionName string, defaultValue []string) ([]string, bool) {
vStr, found := cfg.Get(sectionName, optionName, "")
if !found {
return defaultValue, false
}
v := strings.Split(vStr, ",")
for i := 0; i < len(v); i++ {
v[i] = strings.TrimSpace(v[i])
}
return v, true
}
// Same as Config.Get but returns the value as time.Duration.
// The value in the config file should be in the format that time.ParseDuration() understands.
func (cfg *Config) GetDuration(sectionName, optionName string, defaultValue time.Duration) (time.Duration, bool) {
vStr, found := cfg.Get(sectionName, optionName, "")
if !found {
return defaultValue, false
}
v, err := time.ParseDuration(vStr)
if err != nil {
return defaultValue, false
}
return v, true
}
func expandTildeToHome(fname string) string {
homeDir := os.Getenv("HOME")
if homeDir == "" {
return fname
}
return strings.Replace(fname, "~", homeDir, -1)
}
// Same as Config.Get but consider the string as a filename path and
// expands ~ characters to the homedir of the current uid
func (cfg *Config) GetPath(sectionName, optionName string, defaultValue string) (string, bool) {
vStr, found := cfg.Get(sectionName, optionName, "")
if !found {
return expandTildeToHome(defaultValue), false
}
v := expandTildeToHome(vStr)
return v, true
}
func (cfg *Config) GetMap(sectionName, kPrefix string, defaultValue map[string]string) (map[string]string, bool) {
keys := cfg.SectionKeys(sectionName)
v := make(map[string]string)
for _, k := range keys {
if strings.HasPrefix(k, kPrefix) {
kTrimmed := strings.TrimPrefix(k, kPrefix)
v[kTrimmed], _ = cfg.Get(sectionName, k, "")
}
}
found := len(v) > 0
if !found {
return defaultValue, false
}
return v, true
}