forked from titansoft-pte-ltd/imagepullsecret-patcher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config_helper.go
63 lines (55 loc) · 1.38 KB
/
config_helper.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
package main
import (
"os"
"strconv"
"time"
)
// Reference:
// https://www.gmarik.info/blog/2019/12-factor-golang-flag-package/
// LookupEnvOrString lookup ENV string with given key,
// or returns default value if not exists
func LookupEnvOrString(key string, defaultVal string) string {
if val, ok := os.LookupEnv(key); ok {
return val
}
return defaultVal
}
// LookupEnvOrInt lookup ENV string with given key and convert to int,
// or returns default value if not exists or conversion failed
func LookupEnvOrInt(key string, defaultVal int) int {
str, ok := os.LookupEnv(key)
if !ok {
return defaultVal
}
val, err := strconv.Atoi(str)
if err != nil {
return defaultVal
}
return val
}
// LookUpEnvOrBool lookup ENV string with given key and convert to bool,
// or returns default value if not exists or conversion failed
func LookUpEnvOrBool(key string, defaultVal bool) bool {
str, ok := os.LookupEnv(key)
if !ok {
return defaultVal
}
val, err := strconv.ParseBool(str)
if err != nil {
return defaultVal
}
return val
}
// LookupEnvOrDuration lookup ENV string with given key and convert to time.Duration
// or returns default value if not exists
func LookupEnvOrDuration(key string, defaultVal time.Duration) time.Duration {
str, ok := os.LookupEnv(key)
if !ok {
return defaultVal
}
val, err := time.ParseDuration(str)
if err != nil {
return defaultVal
}
return val
}