-
Notifications
You must be signed in to change notification settings - Fork 5.6k
/
env.go
221 lines (204 loc) · 6.49 KB
/
env.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
package env
import (
"math"
"os"
"strconv"
"strings"
"time"
log "github.com/sirupsen/logrus"
)
// Helper function to parse a number from an environment variable. Returns a
// default if env is not set, is not parseable to a number, exceeds max (if
// max is greater than 0) or is less than min.
//
// nolint:unparam
func ParseNumFromEnv(env string, defaultValue, min, max int) int {
str := os.Getenv(env)
if str == "" {
return defaultValue
}
num, err := strconv.ParseInt(str, 10, 0)
if err != nil {
log.Warnf("Could not parse '%s' as a number from environment %s", str, env)
return defaultValue
}
if num > math.MaxInt || num < math.MinInt {
log.Warnf("Value in %s is %d is outside of the min and max %d allowed values. Using default %d", env, num, min, defaultValue)
return defaultValue
}
if int(num) < min {
log.Warnf("Value in %s is %d, which is less than minimum %d allowed", env, num, min)
return defaultValue
}
if int(num) > max {
log.Warnf("Value in %s is %d, which is greater than maximum %d allowed", env, num, max)
return defaultValue
}
return int(num)
}
// Helper function to parse a int64 from an environment variable. Returns a
// default if env is not set, is not parseable to a number, exceeds max (if
// max is greater than 0) or is less than min.
//
// nolint:unparam
func ParseInt64FromEnv(env string, defaultValue, min, max int64) int64 {
str := os.Getenv(env)
if str == "" {
return defaultValue
}
num, err := strconv.ParseInt(str, 10, 64)
if err != nil {
log.Warnf("Could not parse '%s' as a int64 from environment %s", str, env)
return defaultValue
}
if num < min {
log.Warnf("Value in %s is %d, which is less than minimum %d allowed", env, num, min)
return defaultValue
}
if num > max {
log.Warnf("Value in %s is %d, which is greater than maximum %d allowed", env, num, max)
return defaultValue
}
return num
}
// Helper function to parse a float32 from an environment variable. Returns a
// default if env is not set, is not parseable to a number, exceeds max (if
// max is greater than 0) or is less than min (and min is greater than 0).
//
// nolint:unparam
func ParseFloatFromEnv(env string, defaultValue, min, max float32) float32 {
str := os.Getenv(env)
if str == "" {
return defaultValue
}
num, err := strconv.ParseFloat(str, 32)
if err != nil {
log.Warnf("Could not parse '%s' as a float32 from environment %s", str, env)
return defaultValue
}
if float32(num) < min {
log.Warnf("Value in %s is %f, which is less than minimum %f allowed", env, num, min)
return defaultValue
}
if float32(num) > max {
log.Warnf("Value in %s is %f, which is greater than maximum %f allowed", env, num, max)
return defaultValue
}
return float32(num)
}
// Helper function to parse a float64 from an environment variable. Returns a
// default if env is not set, is not parseable to a number, exceeds max (if
// max is greater than 0) or is less than min (and min is greater than 0).
//
// nolint:unparam
func ParseFloat64FromEnv(env string, defaultValue, min, max float64) float64 {
str := os.Getenv(env)
if str == "" {
return defaultValue
}
num, err := strconv.ParseFloat(str, 64)
if err != nil {
log.Warnf("Could not parse '%s' as a float32 from environment %s", str, env)
return defaultValue
}
if num < min {
log.Warnf("Value in %s is %f, which is less than minimum %f allowed", env, num, min)
return defaultValue
}
if num > max {
log.Warnf("Value in %s is %f, which is greater than maximum %f allowed", env, num, max)
return defaultValue
}
return num
}
// Helper function to parse a time duration from an environment variable. Returns a
// default if env is not set, is not parseable to a duration, exceeds max (if
// max is greater than 0) or is less than min.
//
// nolinit:unparam
func ParseDurationFromEnv(env string, defaultValue, min, max time.Duration) time.Duration {
str := os.Getenv(env)
if str == "" {
return defaultValue
}
dur, err := time.ParseDuration(str)
if err != nil {
log.Warnf("Could not parse '%s' as a duration string from environment %s", str, env)
return defaultValue
}
if dur < min {
log.Warnf("Value in %s is %s, which is less than minimum %s allowed", env, dur, min)
return defaultValue
}
if dur > max {
log.Warnf("Value in %s is %s, which is greater than maximum %s allowed", env, dur, max)
return defaultValue
}
return dur
}
type StringFromEnvOpts struct {
// AllowEmpty allows the value to be empty as long as the environment variable is set.
AllowEmpty bool
}
func StringFromEnv(env string, defaultValue string, opts ...StringFromEnvOpts) string {
opt := StringFromEnvOpts{}
for _, o := range opts {
opt.AllowEmpty = opt.AllowEmpty || o.AllowEmpty
}
if str, ok := os.LookupEnv(env); opt.AllowEmpty && ok || str != "" {
return str
}
return defaultValue
}
// StringsFromEnv parses given value from the environment as a list of strings,
// using separator as the delimeter, and returns them as a slice. The strings
// in the returned slice will have leading and trailing white space removed.
func StringsFromEnv(env string, defaultValue []string, separator string) []string {
if str := os.Getenv(env); str != "" {
ss := strings.Split(str, separator)
for i, s := range ss {
ss[i] = strings.TrimSpace(s)
}
return ss
}
return defaultValue
}
// ParseBoolFromEnv retrieves a boolean value from given environment envVar.
// Returns default value if envVar is not set.
//
// nolinit:unparam
func ParseBoolFromEnv(envVar string, defaultValue bool) bool {
if val := os.Getenv(envVar); val != "" {
if strings.ToLower(val) == "true" {
return true
} else if strings.ToLower(val) == "false" {
return false
}
}
return defaultValue
}
// ParseStringToStringVar parses given value from the environment as a map of string.
// Returns default value if envVar is not set.
func ParseStringToStringFromEnv(envVar string, defaultValue map[string]string, separator string) map[string]string {
str := os.Getenv(envVar)
str = strings.TrimSpace(str)
if str == "" {
return defaultValue
}
parsed := make(map[string]string)
for _, pair := range strings.Split(str, separator) {
keyvalue := strings.Split(pair, "=")
if len(keyvalue) != 2 {
log.Warnf("Invalid key-value pair when parsing environment '%s' as a string map", str)
return defaultValue
}
key := strings.TrimSpace(keyvalue[0])
value := strings.TrimSpace(keyvalue[1])
if _, ok := parsed[key]; ok {
log.Warnf("Duplicate key '%s' when parsing environment '%s' as a string map", key, str)
return defaultValue
}
parsed[key] = value
}
return parsed
}