forked from direnv/direnv
-
Notifications
You must be signed in to change notification settings - Fork 2
/
env_diff.go
127 lines (101 loc) · 2.15 KB
/
env_diff.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
package main
import (
"strings"
)
// A list of keys we don't want to deal with
var IGNORED_KEYS = map[string]bool{
// direnv env config
"DIRENV_CONFIG": true,
"DIRENV_BASH": true,
"COMP_WORDBREAKS": true, // Avoids segfaults in bash
"PS1": true, // PS1 should not be exported, fixes problem in bash
// variables that should change freely
"OLDPWD": true,
"PWD": true,
"SHELL": true,
"SHELLOPTS": true,
"SHLVL": true,
"_": true,
}
type EnvDiff struct {
Prev map[string]string `json:"p"`
Next map[string]string `json:"n"`
}
func NewEnvDiff() *EnvDiff {
return &EnvDiff{make(map[string]string), make(map[string]string)}
}
func BuildEnvDiff(e1, e2 Env) *EnvDiff {
diff := NewEnvDiff()
in := func(key string, e Env) bool {
_, ok := e[key]
return ok
}
for key := range e1 {
if IgnoredEnv(key) {
continue
}
if e2[key] != e1[key] || !in(key, e2) {
diff.Prev[key] = e1[key]
}
}
for key := range e2 {
if IgnoredEnv(key) {
continue
}
if e2[key] != e1[key] || !in(key, e1) {
diff.Next[key] = e2[key]
}
}
return diff
}
func LoadEnvDiff(base64env string) (diff *EnvDiff, err error) {
diff = new(EnvDiff)
err = unmarshal(base64env, diff)
return
}
func (self *EnvDiff) Any() bool {
return len(self.Prev) > 0 || len(self.Next) > 0
}
func (self *EnvDiff) ToShell(shell Shell) string {
e := make(ShellExport)
for key := range self.Prev {
_, ok := self.Next[key]
if !ok {
e.Remove(key)
}
}
for key, value := range self.Next {
e.Add(key, value)
}
return shell.Export(e)
}
func (self *EnvDiff) Patch(env Env) (newEnv Env) {
newEnv = make(Env)
for k, v := range env {
newEnv[k] = v
}
for key := range self.Prev {
delete(newEnv, key)
}
for key, value := range self.Next {
newEnv[key] = value
}
return newEnv
}
func (self *EnvDiff) Reverse() *EnvDiff {
return &EnvDiff{self.Next, self.Prev}
}
func (self *EnvDiff) Serialize() string {
return marshal(self)
}
//// Utils
func IgnoredEnv(key string) bool {
if strings.HasPrefix(key, "__fish") {
return true
}
if strings.HasPrefix(key, "BASH_FUNC_") {
return true
}
_, found := IGNORED_KEYS[key]
return found
}