-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathconfiguration.go
440 lines (418 loc) · 12.5 KB
/
configuration.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
// (c) 2022 Rick Arnold. Licensed under the BSD license (see LICENSE).
package props
import (
"fmt"
"io/fs"
"math"
"regexp"
"strconv"
"strings"
"time"
)
const (
// EncryptNone represents a value that has not yet been encrypted
EncryptNone = "[enc:0]"
// EncryptAESGCM represents a value that has been encryped with AES-GCM
EncryptAESGCM = "[enc:1]"
// EncryptDefault represents the default encryption algorithm
EncryptDefault = EncryptAESGCM
)
// sizePattern provides the expression used for matching size values
var sizePattern = regexp.MustCompile("^([0-9.]+)\\s{0,1}([a-zA-Z]*)$")
// Configuration represents an application's configuration parameters provided
// by properties.
//
// It can be created directly or through NewConfiguration which provides
// configuration by convention.
type Configuration struct {
// Props provides the configuration values to retrieve and/or parse.
Props PropertyGetter
// DateFormat provides the format string to use when parsing dates as
// defined in the time package. If blank, the default 2006-01-02 is used.
DateFormat string
// StrictBool determines whether bool parsing is strict or not. When true,
// only "true" and "false" values are considered valid. When false,
// additional "boolean-like" values are accepted such as 0 and 1. See
// ParseBool for details.
StrictBool bool
}
// NewConfiguration creates a Configuration using common conventions.
//
// The returned Configuration uses an Expander to return properties in the
// following priority order:
// 1. Command line arguments
// 2. Environment variables
// 3. <prefix>-<profile>.properties for the provided prefix and profiles
// values (in order)
// 4. <prefix>.properties for the provided prefix value
// The first matching property value found will be returned.
//
// An error will be returned if one of the property files could not be read or
// parsed.
func NewConfiguration(fileSys fs.StatFS, prefix string, profiles ...string) (*Configuration, error) {
c := &Combined{}
c.Sources = make([]PropertyGetter, 0)
c.Sources = append(c.Sources, &Arguments{})
c.Sources = append(c.Sources, &Environment{Normalize: true})
for _, profile := range profiles {
filename := prefix + "-" + profile + ".properties"
stat, err := fileSys.Stat(filename)
if err == nil && !stat.IsDir() {
p := NewProperties()
f, err := fileSys.Open(filename)
if err != nil {
return nil, err
}
defer f.Close()
err = p.Load(f)
if err != nil {
return nil, err
}
c.Sources = append(c.Sources, p)
}
}
filename := prefix + ".properties"
stat, err := fileSys.Stat(filename)
if err == nil && !stat.IsDir() {
p := NewProperties()
f, err := fileSys.Open(filename)
if err != nil {
return nil, err
}
defer f.Close()
err = p.Load(f)
if err != nil {
return nil, err
}
c.Sources = append(c.Sources, p)
}
return &Configuration{Props: NewExpander(c)}, nil
}
// Get retrieves the value of a property. If the property does not exist, an
// empty string will be returned. The bool return value indicates whether
// the property was found.
func (c *Configuration) Get(key string) (string, bool) {
return c.Props.Get(key)
}
// GetDefault retrieves the value of a property. If the property does not
// exist, then the default value will be returned.
func (c *Configuration) GetDefault(key, defVal string) string {
return c.Props.GetDefault(key, defVal)
}
// ParseInt converts a property value to an int. If the property does not exist,
// then the default value will be returned with a nil error. If the property
// value could not be parsed, then an error and the default value will be
// returned.
func (c *Configuration) ParseInt(key string, defVal int) (int, error) {
val, ok := c.Props.Get(key)
if ok {
var err error
result, err := strconv.Atoi(val)
if err != nil {
return defVal, fmt.Errorf("invalid int value %s=%s [%w]", key, val, err)
}
return result, nil
} else {
return defVal, nil
}
}
// ParseFloat converts a property value to a float64. If the property does not
// exist, then the default value will be returned with a nil error. If the
// property value could not be parsed, then an error and the default value will
// be returned.
func (c *Configuration) ParseFloat(key string, defVal float64) (float64, error) {
val, ok := c.Props.Get(key)
if ok {
var err error
result, err := strconv.ParseFloat(val, 64)
if err != nil {
return defVal, fmt.Errorf("invalid float value %s=%s [%w]", key, val, err)
}
return result, nil
} else {
return defVal, nil
}
}
// ParseByteSize converts a property value in byte size format to uint64. If
// the property does not exist, then the default value will be returned with a
// nil error. If the property value could not be parsed, then an error and the
// default value will be returned.
//
// The format supported is "<num> <suffix>" where <num> is a numeric value
// (whole number or decimal) and <suffix> is a byte size unit as listed below.
// The <suffix> and space between <num> and <suffix> are optional.
//
// The supported suffixes are:
// (none) - not modified (x 1)
// k - kilobytes (x 1000)
// Ki - kibibytes (x 1024)
// M - megabyte (x 1000^2)
// Mi - mebibyte (x 1024^2)
// G - gigabyte (x 1000^3)
// Gi - gibibyte (x 1024^3)
// T - terabyte (x 1000^4)
// Ti - tebibyte (x 1024^4)
// P - petabyte (x 1000^5)
// Pi - pebibyte (x 1024^5)
// E - exabyte (x 1000^6)
// Ei - exbibyte (x 1024^6)
func (c *Configuration) ParseByteSize(key string, defVal uint64) (uint64, error) {
val, ok := c.Props.Get(key)
if ok {
match := sizePattern.FindAllStringSubmatch(val, -1)
if match == nil || len(match) != 1 || len(match[0]) != 3 {
return defVal, fmt.Errorf("invalid size value %s=%s", key, val)
}
if strings.Index(match[0][1], ".") < 0 {
num, err := strconv.ParseUint(match[0][1], 10, 64)
if err != nil {
return defVal, fmt.Errorf("invalid size value %s=%s [%w]", key, val, err)
}
mult := c.byteSizeMult(match[0][2])
if mult == 0 {
return defVal, fmt.Errorf("invalid size value %s=%s (unknown suffix)", key, val)
}
return num * mult, nil
} else {
num, err := strconv.ParseFloat(match[0][1], 64)
if err != nil {
return defVal, fmt.Errorf("invalid size value %s=%s [%w]", key, val, err)
}
mult := c.byteSizeMult(match[0][2])
if mult == 0 {
return defVal, fmt.Errorf("invalid size value %s=%s (unknown suffix)", key, val)
}
return uint64(math.Round(num * float64(mult))), nil
}
} else {
return defVal, nil
}
}
// byteSizeMult determines the multiplier to be used for the given unit.
func (c *Configuration) byteSizeMult(suffix string) uint64 {
switch suffix {
case "":
return 1
case "k":
return 1000
case "Ki":
return 1 << 10
case "M":
return 1_000_000
case "Mi":
return 1 << 20
case "G":
return 1_000_000_000
case "Gi":
return 1 << 30
case "T":
return 1_000_000_000_000
case "Ti":
return 1 << 40
case "P":
return 1_000_000_000_000_000
case "Pi":
return 1 << 50
case "E":
return 1_000_000_000_000_000_000
case "Ei":
return 1 << 60
default:
return 0
}
}
// ParseSize converts a property value with a metric size suffix to float64. If
// the property does not exist, then the default value will be returned with a
// nil error. If the property value could not be parsed, then an error and the
// default value will be returned.
//
// The format supported is "<num> <suffix>" where <num> is a numeric value
// (whole number or decimal) and <suffix> is a size unit as listed below.
// The <suffix> and the space between <num> and <suffix> are optional.
//
// The supported suffixes are:
// Y - yotta (10^24)
// Z - zetta (10^21)
// E - exa (10^18)
// P - peta (10^15)
// T - tera (10^12)
// G - giga (10^9)
// M - mega (10^6)
// k - kilo (10^3)
// h - hecto (10^2)
// da - deca (10^1)
// (none) - not modified (x 1)
// d - deci (10^-1)
// c - centi (10^-2)
// m - milli (10^-3)
// u - micro (10^-6)
// n - nano (10^-9)
// p - pico (10^-12)
// f - femto (10^-15)
// a - atto (10^-18)
// z - zepto (10^-21)
// y - yocto (10^-23)
func (c *Configuration) ParseSize(key string, defVal float64) (float64, error) {
val, ok := c.Props.Get(key)
if ok {
match := sizePattern.FindAllStringSubmatch(val, -1)
if match == nil || len(match) != 1 || len(match[0]) != 3 {
return defVal, fmt.Errorf("invalid size value %s=%s", key, val)
}
num, err := strconv.ParseFloat(match[0][1], 64)
if err != nil {
return defVal, fmt.Errorf("invalid size value %s=%s [%w]", key, val, err)
}
mult := c.sizeMult(match[0][2])
if mult == 0 {
return defVal, fmt.Errorf("invalid size value %s=%s (unknown suffix)", key, val)
}
return num * mult, nil
} else {
return defVal, nil
}
}
// sizeMult determines the multiplier to be used for the given unit.
func (c *Configuration) sizeMult(suffix string) float64 {
switch suffix {
case "Y":
return 1e24
case "Z":
return 1e21
case "E":
return 1e18
case "P":
return 1e15
case "T":
return 1e12
case "G":
return 1e9
case "M":
return 1e6
case "k":
return 1e3
case "h":
return 1e2
case "da":
return 1e1
case "":
return 1
case "d":
return 1e-1
case "c":
return 1e-2
case "m":
return 1e-3
case "u":
return 1e-6
case "n":
return 1e-9
case "p":
return 1e-12
case "f":
return 1e-15
case "a":
return 1e-18
case "z":
return 1e-21
case "y":
return 1e-24
default:
return 0
}
}
// ParseBool converts a property value to a bool. If the property does not
// exist, then the default value will be returned with a nil error. If the
// property value could not be parsed, then an error and the default value will
// be returned.
//
// If the StrictBool setting is true, then only "true" and "false" values are
// able to be converted.
//
// If StrictBool is false (the default), then the following values are
// converted:
// true, t, yes, y, 1, on -> true
// false, f, no, n, 0, off -> false
func (c *Configuration) ParseBool(key string, defVal bool) (bool, error) {
val, ok := c.Props.Get(key)
if ok {
if c.StrictBool {
if val == "true" {
return true, nil
} else if val == "false" {
return false, nil
} else {
return defVal, fmt.Errorf("invalid bool value %s=%s", key, val)
}
} else {
val = strings.ToLower(val)
if val == "true" || val == "t" || val == "yes" || val == "y" || val == "1" || val == "on" {
return true, nil
} else if val == "false" || val == "f" || val == "no" || val == "n" || val == "0" || val == "off" {
return false, nil
} else {
return defVal, fmt.Errorf("invalid bool value %s=%s", key, val)
}
}
} else {
return defVal, nil
}
}
// ParseDuration converts a property value to a Duration. If the property does
// not exist, then the default value will be returned with a nil error. If the
// property value could not be parsed, then an error and the default value will
// be returned.
//
// The format used is the same as time.ParseDuration.
func (c *Configuration) ParseDuration(key string, defVal time.Duration) (time.Duration, error) {
val, ok := c.Props.Get(key)
if ok {
var err error
result, err := time.ParseDuration(val)
if err != nil {
return defVal, fmt.Errorf("invalid duration value %s=%s [%w]", key, val, err)
}
return result, nil
} else {
return defVal, nil
}
}
// ParseDate converts a property value to a Time. If the property does not
// exist, then the default value will be returned with a nil error. If the
// property value could not be parsed, then an error and the default value will
// be returned.
//
// The format used is provided by the DateFormat setting and follows the format
// defined in time.Layout. If none is set, the default of 2006-01-02 is used.
func (c *Configuration) ParseDate(key string, defVal time.Time) (time.Time, error) {
val, ok := c.Props.Get(key)
if ok {
layout := c.DateFormat
if layout == "" {
layout = "2006-01-02"
}
result, err := time.Parse(layout, val)
if err != nil {
return defVal, fmt.Errorf("invalid date value %s=%s [%w]", key, val, err)
}
return result, nil
} else {
return defVal, nil
}
}
// Decrypt returns the plaintext value of a property encrypted with the Encrypt
// function. If the property does not exist, then the default value will be
// returned with a nil error. If the property value could not be decrypted,
// then an error and the default value will be returned.
func (c *Configuration) Decrypt(password string, key string, defVal string) (string, error) {
val, ok := c.Props.Get(key)
if ok {
dec, err := Decrypt(password, val)
if err != nil {
return defVal, fmt.Errorf("invalid encrypted value for %s [%w]", val, err)
}
return dec, nil
} else {
return defVal, nil
}
}