-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgolenv.go
61 lines (52 loc) · 1.26 KB
/
golenv.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
package golenv
import (
"os"
"strings"
"github.com/gol-gol/golconv"
)
func OverrideIfEnv(envVar string, defaultValue string) string {
if HasEnv(envVar) {
envVarValue := os.Getenv(envVar)
if envVarValue != "" {
return envVarValue
}
}
return defaultValue
}
func OverrideIfEnvBool(envVar string, defaultValue bool) bool {
if HasEnv(envVar) {
envVarValue := os.Getenv(envVar)
return golconv.StringToBool(envVarValue, defaultValue)
}
return defaultValue
}
func OverrideIfEnvInt(envVar string, defaultValue int) int {
if HasEnv(envVar) {
envVarValue := os.Getenv(envVar)
return golconv.StringToInt(envVarValue, defaultValue)
}
return defaultValue
}
func OverrideIfEnvUint64(envVar string, defaultValue uint64) uint64 {
if HasEnv(envVar) {
envVarValue := os.Getenv(envVar)
return golconv.StringToUint64(envVarValue, defaultValue)
}
return defaultValue
}
func HasEnv(envVar string) bool {
for _, envKeyVal := range os.Environ() {
if strings.Split(envKeyVal, "=")[0] == envVar {
return true
}
}
return false
}
func EnvMap() map[string]string {
var key_val = make(map[string]string)
for _, envKeyVal := range os.Environ() {
envKeyValSplit := strings.Split(envKeyVal, "=")
key_val[envKeyValSplit[0]] = envKeyValSplit[1]
}
return key_val
}