-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenvconf.go
417 lines (329 loc) · 9.96 KB
/
envconf.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
// Copyright 2016 Stefan Böhmann.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file.
package envconf
import (
"log"
"os"
"strconv"
"strings"
"time"
)
var prefix = ""
func prepareKey(key string) string {
key = strings.TrimSpace(strings.ToUpper(key))
for strings.Contains(key, " ") {
key = strings.Replace(key, " ", " ", -1)
}
key = strings.Replace(key, " ", "_", -1)
if key != "" && prefix != "" {
key = prefix + key
}
return key
}
func parseBool(str string) (value bool, ok bool) {
switch strings.TrimSpace(strings.ToLower(str)) {
case "1", "y", "true", "yes", "on", "":
return true, true
case "0", "n", "false", "no", "off":
return false, true
}
return false, false
}
// GetPrefix returns the prefix that is automatically prepended to a environment variable.
func GetPrefix() string {
return prefix
}
// SetPrefix sets the prefix which is automatically prepended to an environment variable.
func SetPrefix(p string) {
p = strings.TrimSpace(strings.ToUpper(p))
if p != "" {
for strings.Contains(p, " ") {
p = strings.Replace(p, " ", " ", -1)
}
p = strings.Replace(p, " ", "_", -1)
if !strings.HasSuffix(p, "_") {
p += "_"
}
}
prefix = p
}
// UnsetKey unsets a single environment variable.
func UnsetKey(key string) {
os.Unsetenv(prepareKey(key))
}
// IssetKey determine if a environment variable is set.
func IssetKey(key string) bool {
_, ok := os.LookupEnv(prepareKey(key))
return ok
}
// SetDefaultString sets the environment if it is not already set.
func SetDefaultString(key string, value string) {
if !IssetKey(key) {
SetString(key, value)
}
}
// SetString sets the environment.
func SetString(key string, value string) {
os.Setenv(prepareKey(key), value)
}
// GetString returns the environment parsed as string.
// Returns an empty string if the environment does not exists or could not be
// parsed.
func GetString(key string) (value string, ok bool) {
key = prepareKey(key)
if v, ok := os.LookupEnv(key); ok {
return v, true
}
return "", false
}
// MustGetString returns the environment variable parsed as string
// if possible, otherwise it panics.
func MustGetString(key string) (value string) {
value, ok := GetString(key)
if !ok {
panic("Environment variable \"" + prepareKey(key) + "\" not found")
}
return value
}
// SetDefaultBool sets the environment if it is not already set.
func SetDefaultBool(key string, value bool) {
if !IssetKey(key) {
SetBool(key, value)
}
}
// SetBool sets the environment.
func SetBool(key string, value bool) {
SetString(key, strconv.FormatBool(value))
}
// GetBool ...
func GetBool(key string) (value bool, ok bool) {
str, ok := GetString(key)
if ok {
return parseBool(str)
}
return false, true
}
// MustGetBool returns the environment variable parsed as bool
// if possible, otherwise it panics.
func MustGetBool(key string) (value bool) {
str, ok := GetString(key)
if ok {
if value, ok := parseBool(str); ok {
return value
}
panic("Can not convert environment variable \"" +
prepareKey(key) + "\" to type boolean")
}
return false
}
// SetDefaultDuration sets the environment if it is not already set.
func SetDefaultDuration(key string, value time.Duration) {
if !IssetKey(key) {
SetDuration(key, value)
}
}
// SetDuration sets the environment.
func SetDuration(key string, value time.Duration) {
SetString(key, value.String())
}
// GetDuration returns the environment parsed as time.Duration.
func GetDuration(key string) (value time.Duration, ok bool) {
str, ok := GetString(key)
if ok {
v, err := time.ParseDuration(str)
if err == nil {
return v, true
}
log.Println(err)
}
return 0, false
}
// MustGetDuration returns the environment variable parsed as time.Duration
// if possible, otherwise it panics.
func MustGetDuration(key string) time.Duration {
str, ok := GetString(key)
if ok {
v, err := time.ParseDuration(str)
if err == nil {
return v
}
panic("Failed to parse duration from environment variable \"" + prepareKey(key) + "\" with error: " + err.Error())
}
panic("Environment variable \"" + prepareKey(key) + "\" not found")
}
// SetDefaultFloat64 sets the environment if it is not already set.
func SetDefaultFloat64(key string, value float64) {
if !IssetKey(key) {
SetFloat64(key, value)
}
}
// SetFloat64 sets the environment.
func SetFloat64(key string, value float64) {
SetString(key, strconv.FormatFloat(value, 'f', -1, 64))
}
// GetFloat64 returns the environment parsed as float64.
func GetFloat64(key string) (value float64, ok bool) {
str, ok := GetString(key)
if ok {
v, err := strconv.ParseFloat(str, 64)
if err == nil {
return v, true
}
log.Println(err)
}
return 0, false
}
// MustGetFloat64 returns the environment variable parsed as float64
// if possible, otherwise it panics.
func MustGetFloat64(key string) float64 {
str, ok := GetString(key)
if ok {
v, err := strconv.ParseFloat(str, 64)
if err == nil {
return v
}
panic("Failed to parse float64 from environment variable \"" + prepareKey(key) + "\" with error: " + err.Error())
}
panic("Environment variable \"" + prepareKey(key) + "\" not found")
}
// SetDefaultInt sets the environment if it is not already set.
func SetDefaultInt(key string, value int) {
if !IssetKey(key) {
SetInt(key, value)
}
}
// SetInt sets the environment.
func SetInt(key string, value int) {
SetString(key, strconv.FormatInt(int64(value), 10))
}
// GetInt returns the environment parsed as int.
func GetInt(key string) (value int, ok bool) {
str, ok := GetString(key)
if ok {
v, err := strconv.ParseInt(str, 10, 32)
if err == nil {
return int(v), true
}
log.Println(err)
}
return 0, false
}
// MustGetInt returns the environment variable parsed as int
// if possible, otherwise it panics.
func MustGetInt(key string) (value int) {
str, ok := GetString(key)
if ok {
v, err := strconv.ParseInt(str, 10, 32)
if err == nil {
return int(v)
}
panic("Failed to parse int from environment variable \"" + prepareKey(key) + "\" with error: " + err.Error())
}
panic("Environment variable \"" + prepareKey(key) + "\" not found")
}
// SetDefaultInt64 sets the environment if it is not already set.
func SetDefaultInt64(key string, value int64) {
if !IssetKey(key) {
SetInt64(key, value)
}
}
// SetInt64 sets the environment.
func SetInt64(key string, value int64) {
SetString(key, strconv.FormatInt(value, 10))
}
// GetInt64 returns the environment parsed as int64.
func GetInt64(key string) (value int64, ok bool) {
str, ok := GetString(key)
if ok {
v, err := strconv.ParseInt(str, 10, 64)
if err == nil {
return v, true
}
log.Println(err)
}
return 0, false
}
// MustGetInt64 returns the environment variable parsed as int64
// if possible, otherwise it panics.
func MustGetInt64(key string) (value int64) {
str, ok := GetString(key)
if ok {
v, err := strconv.ParseInt(str, 10, 64)
if err == nil {
return v
}
panic("Failed to parse int64 from environment variable \"" + prepareKey(key) + "\" with error: " + err.Error())
}
panic("Environment variable \"" + prepareKey(key) + "\" not found")
}
// SetDefaultUInt sets the environment if it is not already set.
func SetDefaultUInt(key string, value uint) {
if !IssetKey(key) {
SetUInt(key, value)
}
}
// SetUInt sets the environment.
func SetUInt(key string, value uint) {
SetString(key, strconv.FormatUint(uint64(value), 10))
}
// GetUInt returns the environment parsed as uint.
func GetUInt(key string) (value uint, ok bool) {
str, ok := GetString(key)
if ok {
v, err := strconv.ParseUint(str, 10, 32)
if err == nil {
return uint(v), true
}
log.Println(err)
}
return 0, false
}
// MustGetUInt returns the environment variable parsed as uint
// if possible, otherwise it panics.
func MustGetUInt(key string) (value uint) {
str, ok := GetString(key)
if ok {
v, err := strconv.ParseUint(str, 10, 32)
if err == nil {
return uint(v)
}
panic("Failed to parse uint from environment variable \"" + prepareKey(key) + "\" with error: " + err.Error())
}
panic("Environment variable \"" + prepareKey(key) + "\" not found")
}
// SetDefaultUInt64 sets the environment if it is not already set.
func SetDefaultUInt64(key string, value uint64) {
if !IssetKey(key) {
SetUInt64(key, value)
}
}
// SetUInt64 sets the environment.
func SetUInt64(key string, value uint64) {
SetString(key, strconv.FormatUint(value, 10))
}
// GetUInt64 returns the environment parsed as uint64.
func GetUInt64(key string) (value uint64, ok bool) {
str, ok := GetString(key)
if ok {
v, err := strconv.ParseUint(str, 10, 64)
if err == nil {
return v, true
}
log.Println(err)
}
return 0, false
}
// MustGetUInt64 returns the environment variable parsed as uint64
// if possible, otherwise it panics.
func MustGetUInt64(key string) (value uint64) {
str, ok := GetString(key)
if ok {
v, err := strconv.ParseUint(str, 10, 64)
if err == nil {
return v
}
panic("Failed to parse uint64 from environment variable \"" + prepareKey(key) + "\" with error: " + err.Error())
}
panic("Environment variable \"" + prepareKey(key) + "\" not found")
}