-
Notifications
You must be signed in to change notification settings - Fork 0
/
misc.go
77 lines (66 loc) · 1.46 KB
/
misc.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
package main
import (
"log"
"os"
"regexp"
"strconv"
"time"
)
var envVarFilterRegex *regexp.Regexp
func init() {
envVarFilterRegex = regexp.MustCompile("^(_|DISPLAY|MAIL|USER|TERM|HOME|LOGNAME|SHELL|SHLVL|PWD|SSH_.+)=")
}
// if we got an error, panic and log it. otherwise do nothing
func check(e error) {
if e != nil {
log.Println(e)
panic(e)
}
}
// same as check but do not panic
func checkNoPanic(e error) {
if e != nil {
log.Println("ERROR:", e)
}
}
// filter the current environment variables according to the regex
func filteredEnvironmentVars() []string {
filteredVars := []string{}
for _, envVarLine := range os.Environ() {
if !envVarFilterRegex.MatchString(envVarLine) {
filteredVars = append(filteredVars, envVarLine)
}
}
return filteredVars
}
// fetches an environment variable. if the variable is not set, it returns a default
func fetchEnvValue(key string, fallback string) string {
value, isset := os.LookupEnv(key)
if !isset {
return fallback
} else {
return value
}
}
func fetchEnvValueInt(key string, fallback int) int {
value, isset := os.LookupEnv(key)
if !isset {
return fallback
}
intValue, err := strconv.Atoi(value)
if err != nil {
return fallback
}
return intValue
}
func fetchEnvValueDuration(key string, fallback time.Duration) time.Duration {
value, isset := os.LookupEnv(key)
if !isset {
return fallback
}
durValue, err := time.ParseDuration(value)
if err != nil {
return fallback
}
return durValue
}