forked from direnv/direnv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
env.go
161 lines (132 loc) · 3.14 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package main
import (
"bytes"
"compress/zlib"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"os"
"strings"
)
type Env map[string]string
func EnvToShell(env Env, shell Shell) string {
str := ""
for key, value := range env {
// FIXME: This is not exacly as the ruby nil
if value == "" {
if key == "PS1" {
// unsetting PS1 doesn't restore the default in OSX's bash
} else {
str += shell.Unset(key)
}
} else {
str += shell.Export(key, value)
}
}
return str
}
func EnvDiff(env1 map[string]string, env2 map[string]string) Env {
envDiff := make(Env)
for key, _ := range env1 {
if env2[key] != env1[key] && !ignoredKey(key) {
envDiff[key] = env2[key]
}
}
// FIXME: I'm sure there is a smarter way to do that
for key, _ := range env2 {
if env2[key] != env1[key] && !ignoredKey(key) {
envDiff[key] = env2[key]
}
}
return envDiff
}
// A list of keys we don't want to deal with
var IGNORED_KEYS = map[string]bool{
"OLDPWD": true,
"PS1": true,
"PWD": true,
"SHELL": true,
"SHLVL": true,
"_": true,
}
func ignoredKey(key string) bool {
if len(key) > 6 && key[0:6] == "__fish" {
return true
}
_, found := IGNORED_KEYS[key]
return found
}
func direnvVar(key string) bool {
return strings.HasPrefix(key, "DIRENV_")
}
// 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 (env Env) Filtered() Env {
newEnv := make(Env)
for key, value := range env {
if !ignoredKey(key) && !direnvVar(key) {
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 ParseEnv(base64env string) (Env, error) {
base64env = strings.TrimSpace(base64env)
zlibData, err := base64.URLEncoding.DecodeString(base64env)
if err != nil {
return nil, fmt.Errorf("ParseEnv() base64 decoding: %v", err)
}
zlibReader := bytes.NewReader(zlibData)
w, err := zlib.NewReader(zlibReader)
if err != nil {
return nil, fmt.Errorf("ParseEnv() zlib opening: %v", err)
}
envData := bytes.NewBuffer([]byte{})
_, err = io.Copy(envData, w)
if err != nil {
return nil, fmt.Errorf("ParseEnv() zlib decoding: %v", err)
}
w.Close()
env := make(Env)
err = json.Unmarshal(envData.Bytes(), &env)
if err != nil {
return nil, fmt.Errorf("ParseEnv() json parsing: %v", err)
}
return env, nil
}
func (env Env) Serialize() string {
// We can safely ignore the err because it's only thrown
// for unsupported datatype. We know that a map[string]string
// is supported.
jsonData, err := json.Marshal(env)
if err != nil {
panic(fmt.Errorf("Serialize(): %q", err))
}
zlibData := bytes.NewBuffer([]byte{})
w := zlib.NewWriter(zlibData)
w.Write(jsonData)
w.Close()
base64Data := base64.URLEncoding.EncodeToString(zlibData.Bytes())
return base64Data
}