-
Notifications
You must be signed in to change notification settings - Fork 0
/
subenv.go
101 lines (86 loc) · 1.74 KB
/
subenv.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
package subenv
import (
"os"
"strconv"
)
var (
override = make(map[string]interface{})
)
/**
Override any vars that might be fetched in the future
*/
func Override(key string, value interface{}) {
override[key] = value
}
/**
Get an integer equivalent of environmental variable. Return default value of the value is not found or is empty
*/
func EnvI(key string, defaultIfNull int) int {
var val string
var ok bool
// Check for any overridden values
if val, ok := override[key]; ok {
iVal, iOk := val.(int)
if iOk {
return iVal
}
}
if val, ok = os.LookupEnv(key); ok {
if len(val) == 0 {
return defaultIfNull
}
}
if eint, err := strconv.Atoi(val); err != nil {
return defaultIfNull
} else {
return eint
}
}
/**
Get the string equivalent of environmental variable. Return default value of the value is not found or is empty
*/
func Env(key string, defaultIfNull string) string {
var val string
var ok bool
// Check for any overridden values
if val, ok := override[key]; ok {
iVal, iOk := val.(string)
if iOk {
return iVal
}
}
if val, ok = os.LookupEnv(key); ok {
if len(val) == 0 {
return defaultIfNull
} else {
return val
}
}
return defaultIfNull
}
/**
Get the boolean equivalent of environmental variable. Return default value of the value is not found or is empty
*/
func EnvB(key string, defaultIfNull bool) bool {
var val string
var ok bool
// Check for any overridden values
if val, ok := override[key]; ok {
iVal, iOk := val.(bool)
if iOk {
return iVal
}
}
if val, ok = os.LookupEnv(key); ok {
if len(val) == 0 {
return defaultIfNull
} else {
if v, e := strconv.ParseBool(val); e != nil {
return defaultIfNull
} else {
return v
}
}
}
return defaultIfNull
}