forked from direnv/direnv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenv.go
70 lines (53 loc) · 1.1 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package main
import (
"os"
"strings"
)
type Env map[string]string
// NOTE: We don't support having two variables with the same name.
// I've never seen it used in the wild but accoding to POSIX
// it's allowed.
func GetEnv() Env {
env := make(Env)
for _, kv := range os.Environ() {
kv2 := strings.SplitN(kv, "=", 2)
key := kv2[0]
value := kv2[1]
env[key] = value
}
return env
}
func LoadEnv(base64env string) (env Env, err error) {
env = make(Env)
err = unmarshal(base64env, &env)
return
}
func (env Env) Copy() Env {
newEnv := make(Env)
for key, value := range env {
newEnv[key] = value
}
return newEnv
}
func (env Env) ToGoEnv() []string {
goEnv := make([]string, len(env))
index := 0
for key, value := range env {
goEnv[index] = strings.Join([]string{key, value}, "=")
index += 1
}
return goEnv
}
func (env Env) ToShell(shell Shell) string {
str := ""
for key, value := range env {
str += shell.Export(key, value)
}
return str
}
func (env Env) Serialize() string {
return marshal(env)
}
func (e1 Env) Diff(e2 Env) *EnvDiff {
return BuildEnvDiff(e1, e2)
}