-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.go
337 lines (304 loc) · 9.24 KB
/
util.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
package env
import (
"fmt"
"net/url"
"os"
"strconv"
"time"
)
// Set - sets an environment variable
func Set(key, value string) error {
return os.Setenv(key, value)
}
// Unset - unsets an environment variable
func Unset(key string) error {
return os.Unsetenv(key)
}
// Get - get an environment variable, empty string if does not exist
func Get(key string) string {
return os.Getenv(key)
}
// GetOr - get an environment variable or return default value if does not exist
func GetOr(key, defaultValue string) string {
value, ok := os.LookupEnv(key)
if ok {
return value
}
return defaultValue
}
// MustGet - get an environment variable or panic if does not exist
func MustGet(key string) string {
value, ok := os.LookupEnv(key)
if ok {
return value
}
panic(fmt.Sprintf("expected environment variable \"%s\" does not exist", key))
}
// GetBool - get an environment variable as boolean
func GetBool(key string) (bool, error) {
return strconv.ParseBool(os.Getenv(key))
}
// GetOrBool - get an environment variable or return default value if does not exist
func GetOrBool(key string, defaultValue bool) bool {
strValue, ok := os.LookupEnv(key)
if ok {
value, err := strconv.ParseBool(strValue)
if err == nil {
return value
}
}
return defaultValue
}
// MustGetBool - get an environment variable or panic if does not exist
func MustGetBool(key string) bool {
strValue, ok := os.LookupEnv(key)
if ok {
value, err := strconv.ParseBool(strValue)
if err == nil {
return value
} else {
panic(fmt.Sprintf("environment variable \"%s\" could not be converted to boolean", key))
}
}
panic(fmt.Sprintf("expected environment variable \"%s\" does not exist", key))
}
// GetInt - get an environment variable as int
func GetInt(key string) (int, error) {
value, err := strconv.ParseInt(os.Getenv(key), 10, 32)
return int(value), err
}
// GetOrInt - get an environment variable or return default value if does not exist
func GetOrInt(key string, defaultValue int) int {
strValue, ok := os.LookupEnv(key)
if ok {
value, err := strconv.ParseInt(strValue, 10, 32)
if err == nil {
return int(value)
}
}
return defaultValue
}
// MustGetInt - get an environment variable or panic if does not exist
func MustGetInt(key string) int {
strValue, ok := os.LookupEnv(key)
if ok {
value, err := strconv.ParseInt(strValue, 10, 32)
if err == nil {
return int(value)
} else {
panic(fmt.Sprintf("environment variable \"%s\" could not be converted to int", key))
}
}
panic(fmt.Sprintf("expected environment variable \"%s\" does not exist", key))
}
// GetUint - get an environment variable as uint
func GetUint(key string) (uint, error) {
value, err := strconv.ParseUint(os.Getenv(key), 10, 32)
return uint(value), err
}
// GetOrUint - get an environment variable or return default value if does not exist
func GetOrUint(key string, defaultValue uint) uint {
strValue, ok := os.LookupEnv(key)
if ok {
value, err := strconv.ParseUint(strValue, 10, 32)
if err == nil {
return uint(value)
}
}
return defaultValue
}
// MustGetUint - get an environment variable or panic if does not exist
func MustGetUint(key string) uint {
strValue, ok := os.LookupEnv(key)
if ok {
value, err := strconv.ParseUint(strValue, 10, 32)
if err == nil {
return uint(value)
} else {
panic(fmt.Sprintf("environment variable \"%s\" could not be converted to uint", key))
}
}
panic(fmt.Sprintf("expected environment variable \"%s\" does not exist", key))
}
// GetFloat32 - get an environment variable as float32
func GetFloat32(key string) (float32, error) {
value, err := strconv.ParseFloat(os.Getenv(key), 32)
return float32(value), err
}
// GetOrFloat32 - get an environment variable or return default value if does not exist
func GetOrFloat32(key string, defaultValue float32) float32 {
strValue, ok := os.LookupEnv(key)
if ok {
value, err := strconv.ParseFloat(strValue, 32)
if err == nil {
return float32(value)
}
}
return defaultValue
}
// MustGetUFloat32 - get an environment variable or panic if does not exist
func MustGetFloat32(key string) float32 {
strValue, ok := os.LookupEnv(key)
if ok {
value, err := strconv.ParseFloat(strValue, 32)
if err == nil {
return float32(value)
} else {
panic(fmt.Sprintf("environment variable \"%s\" could not be converted to float32", key))
}
}
panic(fmt.Sprintf("expected environment variable \"%s\" does not exist", key))
}
// GetFloat64 - get an environment variable as float64
func GetFloat64(key string) (float64, error) {
value, err := strconv.ParseFloat(os.Getenv(key), 64)
return float64(value), err
}
// GetOrFloat64 - get an environment variable or return default value if does not exist
func GetOrFloat64(key string, defaultValue float64) float64 {
strValue, ok := os.LookupEnv(key)
if ok {
value, err := strconv.ParseFloat(strValue, 64)
if err == nil {
return float64(value)
}
}
return defaultValue
}
// MustGetUFloat64 - get an environment variable or panic if does not exist
func MustGetFloat64(key string) float64 {
strValue, ok := os.LookupEnv(key)
if ok {
value, err := strconv.ParseFloat(strValue, 64)
if err == nil {
return float64(value)
} else {
panic(fmt.Sprintf("environment variable \"%s\" could not be converted to float64", key))
}
}
panic(fmt.Sprintf("expected environment variable \"%s\" does not exist", key))
}
// GetInt64 - get an environment variable as int64
func GetInt64(key string) (int64, error) {
value, err := strconv.ParseInt(os.Getenv(key), 10, 64)
return int64(value), err
}
// GetOrInt64 - get an environment variable or return default value if does not exist
func GetOrInt64(key string, defaultValue int64) int64 {
strValue, ok := os.LookupEnv(key)
if ok {
value, err := strconv.ParseInt(strValue, 10, 64)
if err == nil {
return int64(value)
}
}
return defaultValue
}
// MustGetInt64 - get an environment variable or panic if does not exist
func MustGetInt64(key string) int64 {
strValue, ok := os.LookupEnv(key)
if ok {
value, err := strconv.ParseInt(strValue, 10, 64)
if err == nil {
return int64(value)
} else {
panic(fmt.Sprintf("environment variable \"%s\" could not be converted to int64", key))
}
}
panic(fmt.Sprintf("expected environment variable \"%s\" does not exist", key))
}
// GetUint64 - get an environment variable as uint
func GetUint64(key string) (uint64, error) {
value, err := strconv.ParseUint(os.Getenv(key), 10, 64)
return uint64(value), err
}
// GetOrUint64 - get an environment variable or return default value if does not exist
func GetOrUint64(key string, defaultValue uint64) uint64 {
strValue, ok := os.LookupEnv(key)
if ok {
value, err := strconv.ParseUint(strValue, 10, 64)
if err == nil {
return uint64(value)
}
}
return defaultValue
}
// MustGetUint64 - get an environment variable or panic if does not exist
func MustGetUint64(key string) uint64 {
strValue, ok := os.LookupEnv(key)
if ok {
value, err := strconv.ParseUint(strValue, 10, 64)
if err == nil {
return uint64(value)
} else {
panic(fmt.Sprintf("environment variable \"%s\" could not be converted to uint64", key))
}
}
panic(fmt.Sprintf("expected environment variable \"%s\" does not exist", key))
}
// GetDuration - get an environment variable as time.Duration
func GetDuration(key string) (time.Duration, error) {
value, err := time.ParseDuration(os.Getenv(key))
return value, err
}
// GetOrDuration - get an environment variable or return default value if does not exist
func GetOrDuration(key string, defaultValue string) time.Duration {
strValue, ok := os.LookupEnv(key)
if ok {
value, err := time.ParseDuration(strValue)
if err == nil {
return value
}
}
defaultDuration, err := time.ParseDuration(defaultValue)
if err != nil {
panic(fmt.Sprintf("default duration \"%s\" could not be converted to time.Duration", key))
}
return defaultDuration
}
// MustGetDuration - get an environment variable or panic if does not exist
func MustGetDuration(key string) time.Duration {
strValue, ok := os.LookupEnv(key)
if ok {
value, err := time.ParseDuration(strValue)
if err == nil {
return value
} else {
panic(fmt.Sprintf("environment variable \"%s\" could not be converted to time.Duration", key))
}
}
panic(fmt.Sprintf("expected environment variable \"%s\" does not exist", key))
}
// GetUrl - get an environment variable as url.URL
func GetUrl(key string) (*url.URL, error) {
value, err := url.ParseRequestURI(os.Getenv(key))
return value, err
}
// GetOrUrl - get an environment variable or return default value if does not exist
func GetOrUrl(key string, defaultValue string) *url.URL {
strValue, ok := os.LookupEnv(key)
if ok {
value, err := url.ParseRequestURI(strValue)
if err == nil {
return value
}
}
defaultUrl, err := url.ParseRequestURI(defaultValue)
if err != nil {
panic(fmt.Sprintf("default duration \"%s\" could not be converted to url.URL", key))
}
return defaultUrl
}
// MustGetUrl - get an environment variable or panic if does not exist
func MustGetUrl(key string) *url.URL {
strValue, ok := os.LookupEnv(key)
if ok {
value, err := url.ParseRequestURI(strValue)
if err == nil {
return value
} else {
panic(fmt.Sprintf("environment variable \"%s\" could not be converted to url.URL", key))
}
}
panic(fmt.Sprintf("expected environment variable \"%s\" does not exist", key))
}