-
Notifications
You must be signed in to change notification settings - Fork 7
/
env.go
49 lines (42 loc) · 916 Bytes
/
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
package interpolate
import (
"runtime"
"strings"
)
type Env interface {
Get(key string) (string, bool)
}
// Creates an Env from a slice of environment variables
func NewSliceEnv(env []string) Env {
envMap := mapEnv{}
for _, l := range env {
parts := strings.SplitN(l, "=", 2)
if len(parts) == 2 {
envMap[normalizeKeyName(parts[0])] = parts[1]
}
}
return envMap
}
// Creates an Env from a map of environment variables
func NewMapEnv(env map[string]string) Env {
envMap := mapEnv{}
for k, v := range env {
envMap[normalizeKeyName(k)] = v
}
return envMap
}
type mapEnv map[string]string
func (m mapEnv) Get(key string) (string, bool) {
if m == nil {
return "", false
}
val, ok := m[normalizeKeyName(key)]
return val, ok
}
// Windows isn't case sensitive for env
func normalizeKeyName(key string) string {
if runtime.GOOS == "windows" {
return strings.ToUpper(key)
}
return key
}