-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenv.go
53 lines (46 loc) · 1.13 KB
/
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
50
51
52
53
package interpolate
import (
"runtime"
"strings"
)
// Env is an interface for getting environment variables by name and returning a boolean indicating
// whether the variable was found.
type Env interface {
Get(key string) (string, bool)
}
// NewSliceEnv creates an Env from a slice of environment variables in the form "key=value".
//
// This can be used with [os.Environ] to create an Env.
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
}
// NewMapEnv 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
}