-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
env.go
111 lines (101 loc) · 2.97 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
package env
import (
"os"
"strconv"
"strings"
"time"
timeutil "github.com/argoproj/pkg/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.Atoi(str)
if err != nil {
log.Warnf("Could not parse '%s' as a number 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 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 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.
func ParseDurationFromEnv(env string, defaultValue, min, max time.Duration) time.Duration {
str := os.Getenv(env)
if str == "" {
return defaultValue
}
durPtr, err := timeutil.ParseDuration(str)
if err != nil {
log.Warnf("Could not parse '%s' as a number from environment %s", str, env)
return defaultValue
}
dur := *durPtr
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
}
func StringFromEnv(env string, defaultValue string) string {
if str := os.Getenv(env); str != "" {
return str
}
return defaultValue
}
// ParseBoolFromEnv retrieves a boolean value from given environment envVar.
// Returns default value if envVar is not set.
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
}