-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
env.go
56 lines (50 loc) · 1.02 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
package env
import (
"os"
"strconv"
"time"
log "github.com/sirupsen/logrus"
)
func LookupEnvDurationOr(key string, o time.Duration) time.Duration {
v, found := os.LookupEnv(key)
if found && v != "" {
d, err := time.ParseDuration(v)
if err != nil {
log.WithField(key, v).WithError(err).Panic("failed to parse")
} else {
return d
}
}
return o
}
func LookupEnvIntOr(key string, o int) int {
v, found := os.LookupEnv(key)
if found && v != "" {
d, err := strconv.Atoi(v)
if err != nil {
log.WithField(key, v).WithError(err).Panic("failed to convert to int")
} else {
return d
}
}
return o
}
func LookupEnvFloatOr(key string, o float64) float64 {
v, found := os.LookupEnv(key)
if found && v != "" {
d, err := strconv.ParseFloat(v, 64)
if err != nil {
log.WithField(key, v).WithError(err).Panic("failed to convert to float")
} else {
return d
}
}
return o
}
func LookupEnvStringOr(key string, o string) string {
v, found := os.LookupEnv(key)
if found && v != "" {
return v
}
return o
}