-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfuncs.go
45 lines (40 loc) · 1.13 KB
/
funcs.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
package vxrouter
import (
"os"
"strconv"
"time"
log "github.com/sirupsen/logrus"
)
func getEnvOpt(val, opt string) string { //nolint: unparam
e := os.Getenv(val)
if e == "" {
e = opt
}
return e
}
// GetEnvIntWithDefault gets value, prioritizing first opt, if it is not empty, then the environment variable specified by val, and lastly the default.
func GetEnvIntWithDefault(val, opt string, def int) int { //nolint: unparam
e := getEnvOpt(val, opt)
if e == "" {
return def
}
ei, err := strconv.Atoi(e)
if err != nil {
log.WithField("string", e).WithError(err).Warnf("failed to convert string to int, using default")
return def
}
return ei
}
// GetEnvDurWithDefault gets value, prioritizing first opt, if it is not empty, then the environment variable specified by val, and lastly the default.
func GetEnvDurWithDefault(val, opt string, def time.Duration) time.Duration { //nolint: unparam
e := getEnvOpt(val, opt)
if e == "" {
return def
}
ei, err := time.ParseDuration(e)
if err != nil {
log.WithField("string", e).WithError(err).Warnf("failed to convert string to duration, using default")
return def
}
return ei
}