forked from direnv/direnv
-
Notifications
You must be signed in to change notification settings - Fork 2
/
cmd_export.go
185 lines (153 loc) · 3.68 KB
/
cmd_export.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package main
import (
"fmt"
"log"
"sort"
"strings"
)
type ExportContext struct {
config *Config
foundRC *RC
loadedRC *RC
env Env
oldEnv Env
newEnv Env
}
func (self *ExportContext) loadConfig() (err error) {
self.config, err = LoadConfig(self.env)
return
}
func (self *ExportContext) getRCs() {
self.loadedRC = self.config.LoadedRC()
self.foundRC = self.config.FindRC()
}
func (self *ExportContext) hasRC() bool {
return self.foundRC != nil || self.loadedRC != nil
}
func (self *ExportContext) updateRC() (err error) {
defer log.SetPrefix(log.Prefix())
log.SetPrefix(log.Prefix() + "update:")
self.oldEnv = self.env.Copy()
var backupDiff *EnvDiff
if backupDiff, err = self.config.EnvDiff(); err != nil {
err = fmt.Errorf("EnvDiff() failed: %q", err)
return
}
self.oldEnv = backupDiff.Reverse().Patch(self.env)
log_debug("Determining action:")
log_debug("foundRC: %#v", self.foundRC)
log_debug("loadedRC: %#v", self.loadedRC)
switch {
case self.foundRC == nil:
log_debug("no RC found, unloading")
err = self.unloadEnv()
case self.loadedRC == nil:
log_debug("no RC (implies no DIRENV_DIFF),loading")
err = self.loadRC()
case self.loadedRC.path != self.foundRC.path:
log_debug("new RC, loading")
err = self.loadRC()
case self.loadedRC.times.Check() != nil:
log_debug("file changed, reloading")
err = self.loadRC()
default:
log_debug("no update needed")
}
return
}
func (self *ExportContext) loadRC() (err error) {
self.newEnv, err = self.foundRC.Load(self.config, self.oldEnv)
return
}
func (self *ExportContext) unloadEnv() (err error) {
log_status(self.env, "unloading")
self.newEnv = self.oldEnv.Copy()
cleanEnv(self.newEnv)
return
}
func (self *ExportContext) resetEnv() {
self.newEnv = self.oldEnv.Copy()
cleanEnv(self.oldEnv)
if self.foundRC != nil {
delete(self.newEnv, DIRENV_DIFF)
self.foundRC.RecordState(self.oldEnv, self.newEnv)
}
}
func cleanEnv(env Env) {
env.CleanContext()
}
func (self *ExportContext) diffString(shell Shell) string {
oldDiff := self.oldEnv.Diff(self.newEnv)
if oldDiff.Any() {
var out []string
for key := range oldDiff.Prev {
_, ok := oldDiff.Next[key]
if !ok && !direnvKey(key) {
out = append(out, "-"+key)
}
}
for key := range oldDiff.Next {
_, ok := oldDiff.Prev[key]
if direnvKey(key) {
continue
}
if ok {
out = append(out, "~"+key)
} else {
out = append(out, "+"+key)
}
}
sort.Strings(out)
if len(out) > 0 {
log_status(self.env, "export %s", strings.Join(out, " "))
}
}
diff := self.env.Diff(self.newEnv)
return diff.ToShell(shell)
}
func exportCommand(env Env, args []string) (err error) {
defer log.SetPrefix(log.Prefix())
log.SetPrefix(log.Prefix() + "export:")
log_debug("start")
context := ExportContext{env: env}
var target string
if len(args) > 1 {
target = args[1]
}
shell := DetectShell(target)
if shell == nil {
return fmt.Errorf("Unknown target shell '%s'", target)
}
log_debug("load config")
if err = context.loadConfig(); err != nil {
return
}
log_debug("loading RCs")
if context.getRCs(); !context.hasRC() {
return nil
}
log_debug("updating RC")
if err = context.updateRC(); err != nil {
log_debug("err: %v", err)
context.resetEnv()
}
if context.newEnv == nil {
log_debug("newEnv nil, exiting")
return nil
}
diffString := context.diffString(shell)
log_debug("env diff %s", diffString)
fmt.Print(diffString)
return
}
// `direnv export $0`
var CmdExport = &Cmd{
Name: "export",
Desc: "loads an .envrc and prints the diff in terms of exports",
Args: []string{"SHELL"},
Private: true,
Fn: exportCommand,
}
func direnvKey(key string) bool {
return strings.HasPrefix(key, "DIRENV_")
}