-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstring.go
More file actions
48 lines (44 loc) · 973 Bytes
/
Copy pathstring.go
File metadata and controls
48 lines (44 loc) · 973 Bytes
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
package getenv
import (
"os"
"strings"
)
func String(key string, def ...string) string {
Logger.Dump(key, def)
var d string
if len(def) != 0 {
d = def[0]
}
v, ok := os.LookupEnv(key)
if !ok {
return d
}
return v
}
// StringSlice resolves key as a comma-separated list, e.g. SOME_ENV=a,b,c.
//
// Each element is trimmed of surrounding whitespace and empty elements are dropped, so
// "a, b," yields ["a", "b"]. An unset or empty value is treated the same: the default
// is returned when present, otherwise an empty (non-nil) slice.
func StringSlice(key string, def ...[]string) []string {
Logger.Dump(key, def)
var d []string
if len(def) != 0 {
d = def[0]
}
v, ok := os.LookupEnv(key)
if !ok || v == "" {
if len(d) == 0 {
return []string{}
}
return d
}
parts := strings.Split(v, ",")
out := make([]string, 0, len(parts))
for _, p := range parts {
if p = strings.TrimSpace(p); p != "" {
out = append(out, p)
}
}
return out
}