forked from direnv/direnv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenv.go
115 lines (94 loc) · 2.7 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package main
import (
"encoding/json"
"os"
"strings"
"github.com/direnv/direnv/gzenv"
)
// Env is a map representation of environment variables.
type Env map[string]string
// GetEnv turns the classic unix environment variables into a map of
// key->values which is more handy to work with.
//
// 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
}
// CleanContext removes all the direnv-related environment variables. Call
// this after reverting the environment, otherwise direnv will just be amnesic
// about the previously-loaded environment.
func (env Env) CleanContext() {
delete(env, DIRENV_DIR)
delete(env, DIRENV_WATCHES)
delete(env, DIRENV_DIFF)
}
// LoadEnv unmarshals the env back from a gzenv string
func LoadEnv(gzenvStr string) (env Env, err error) {
env = make(Env)
err = gzenv.Unmarshal(gzenvStr, &env)
return
}
// LoadEnvJSON unmarshals the env back from a JSON string
func LoadEnvJSON(jsonBytes []byte) (env Env, err error) {
env = make(Env)
err = json.Unmarshal(jsonBytes, &env)
return env, err
}
// Copy returns a fresh copy of the env. Because the env is a map under the
// hood, we want to get a copy whenever we mutate it and want to keep the
// original around.
func (env Env) Copy() Env {
newEnv := make(Env)
for key, value := range env {
newEnv[key] = value
}
return newEnv
}
// ToGoEnv should really be named ToUnixEnv. It turns the env back into a list
// of "key=value" strings like returns by os.Environ().
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++
}
return goEnv
}
// ToShell outputs the environment into an evaluatable string that is
// understood by the target shell
func (env Env) ToShell(shell Shell) string {
e := make(ShellExport)
for key, value := range env {
e.Add(key, value)
}
return shell.Export(e)
}
// Serialize marshals the env into the gzenv format
func (env Env) Serialize() string {
return gzenv.Marshal(env)
}
// Diff returns the diff between the current env and the passed env
func (env Env) Diff(other Env) *EnvDiff {
return BuildEnvDiff(env, other)
}
// Fetch tries to get the value associated with the given 'key', or returns
// the provided default if none is set.
//
// Note that empty environment variables are considered to be set.
func (env Env) Fetch(key, def string) string {
v, ok := env[key]
if !ok {
v = def
}
return v
}