-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathconfig.go
335 lines (275 loc) · 11.3 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
/*
Package config implements simple TOML-based configuration variables, based on
the flag package in the standard Go library (In fact, it's just a simple
wrapper around flag.FlagSet). It is used in a similar manner, minus the usage
strings and other command-line specific bits.
Usage:
Given the following TOML file:
country = "USA"
[atlanta]
enabled = true
population = 432427
temperature = 99.6
Define your config variables and give them defaults:
import "github.com/stvp/go-toml-config"
var (
country = config.String("country", "Unknown")
atlantaEnabled = config.Bool("atlanta.enabled", false)
alantaPopulation = config.Int("atlanta.population", 0)
atlantaTemperature = config.Float("atlanta.temperature", 0)
)
After all the config variables are defined, load the config file to overwrite
the default values with the user-supplied config settings:
if err := config.Parse("/path/to/myconfig.conf"); err != nil {
panic(err)
}
You can also create separate ConfigSets for different config files:
networkConfig = config.NewConfigSet("network settings", config.ExitOnError)
networkConfig.String("host", "localhost")
networkConfig.Int("port", 8080)
networkConfig.Parse("/path/to/network.conf")
*/
package config
import (
"errors"
"flag"
"fmt"
"io/ioutil"
"os"
"regexp"
"strings"
"time"
"github.com/pelletier/go-toml"
)
// -- ConfigSet
type ConfigSet struct {
*flag.FlagSet
}
// 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, "")
}
// 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.
func (c *ConfigSet) Parse(path string) error {
configBytes, err := ioutil.ReadFile(path)
if err != nil {
return err
}
tomlTree, err := toml.Load(string(configBytes))
if err != nil {
errorString := fmt.Sprintf("%s is not a valid TOML file. See https://github.com/mojombo/toml", path)
return errors.New(errorString)
}
err = c.loadTomlTree(tomlTree, []string{})
if err != nil {
return err
}
return nil
}
// loadTomlTree recursively loads a toml.Tree into this ConfigSet's config
// variables.
func (c *ConfigSet) loadTomlTree(tree *toml.Tree, path []string) error {
for _, key := range tree.Keys() {
fullPath := append(path, key)
value := tree.Get(key)
if subtree, isTree := value.(*toml.Tree); isTree {
err := c.loadTomlTree(subtree, fullPath)
if err != nil {
return err
}
} else {
fullPath := strings.Join(append(path, key), ".")
err := c.Set(fullPath, fmt.Sprintf("%v", value))
if err != nil {
return buildLoadError(fullPath, err)
}
}
}
return nil
}
// buildLoadError takes an error from flag.FlagSet#Set and makes it a bit more
// readable, if it recognizes the format.
func buildLoadError(path string, err error) error {
missingFlag := regexp.MustCompile(`^no such flag -([^\s]+)`)
invalidSyntax := regexp.MustCompile(`^.+ parsing "(.+)": invalid syntax$`)
errorString := err.Error()
if missingFlag.MatchString(errorString) {
errorString = missingFlag.ReplaceAllString(errorString, "$1 is not a valid config setting")
} else if invalidSyntax.MatchString(errorString) {
errorString = "The value for " + path + " is invalid"
}
return errors.New(errorString)
}
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{
flag.NewFlagSet(name, errorHandling),
}
}
// -- globalConfig
var globalConfig = NewConfigSet(os.Args[0], flag.ExitOnError)
// 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)
}
// 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)
}