forked from direnv/direnv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd_export.go
191 lines (158 loc) · 3.89 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
186
187
188
189
190
191
package main
import (
"context"
"fmt"
"log"
"os"
"os/signal"
"sort"
"strings"
)
// CmdExport is `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,
Action: cmdWithWarnTimeout(actionWithConfig(actionWithCancel(exportCommand))),
}
func actionWithCancel(fn func(ctx context.Context, env Env, args []string, config *Config) error) actionWithConfig {
return func(env Env, args []string, config *Config) error {
ctx, cancel := context.WithCancel(context.Background())
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
// cancel the context on Ctrl-C
go func() {
<-c
cancel()
}()
return fn(ctx, env, args, config)
}
}
func exportCommand(ctx context.Context, env Env, args []string, config *Config) (err error) {
defer log.SetPrefix(log.Prefix())
log.SetPrefix(log.Prefix() + "export:")
logDebug("start")
ec := ExportContext{
env: env,
config: config,
}
var target string
if len(args) > 1 {
target = args[1]
}
shell := DetectShell(target)
if shell == nil {
return fmt.Errorf("unknown target shell '%s'", target)
}
logDebug("loading RCs")
if ec.getRCs(); !ec.hasRC() {
return nil
}
logDebug("updating RC")
if err = ec.updateRC(ctx); err != nil {
logDebug("err: %v", err)
}
if ec.newEnv == nil {
logDebug("newEnv nil, exiting")
return
}
diffString := ec.diffString(shell)
logDebug("env diff %s", diffString)
fmt.Print(diffString)
return
}
// ExportContext is a sort of state holder struct that is being used to
// record changes before the export finishes.
type ExportContext struct {
config *Config
foundRC *RC
loadedRC *RC
env Env
oldEnv Env
newEnv Env
}
func (ec *ExportContext) getRCs() {
ec.loadedRC = ec.config.LoadedRC()
ec.foundRC, _ = ec.config.FindRC()
}
func (ec *ExportContext) hasRC() bool {
return ec.foundRC != nil || ec.loadedRC != nil
}
func (ec *ExportContext) updateRC(ctx context.Context) (err error) {
defer log.SetPrefix(log.Prefix())
log.SetPrefix(log.Prefix() + "update:")
ec.oldEnv = ec.env.Copy()
var backupDiff *EnvDiff
if backupDiff, err = ec.config.EnvDiff(); err == nil && backupDiff != nil {
ec.oldEnv = backupDiff.Reverse().Patch(ec.env)
}
if err != nil {
err = fmt.Errorf("EnvDiff() failed: %q", err)
return
}
logDebug("Determining action:")
logDebug("foundRC: %#v", ec.foundRC)
logDebug("loadedRC: %#v", ec.loadedRC)
switch {
case ec.foundRC == nil:
logDebug("no RC found, unloading")
ec.unloadEnv()
case ec.loadedRC == nil:
logDebug("no RC (implies no DIRENV_DIFF),loading")
err = ec.loadRC(ctx)
case ec.loadedRC.path != ec.foundRC.path:
logDebug("new RC, loading")
err = ec.loadRC(ctx)
case ec.loadedRC.times.Check() != nil:
logDebug("file changed, reloading")
err = ec.loadRC(ctx)
default:
logDebug("no update needed")
}
return
}
func (ec *ExportContext) loadRC(ctx context.Context) (err error) {
ec.newEnv, err = ec.foundRC.Load(ctx, ec.config, ec.oldEnv)
return
}
func (ec *ExportContext) unloadEnv() {
logStatus(ec.env, "unloading")
ec.newEnv = ec.oldEnv.Copy()
cleanEnv(ec.newEnv)
}
func cleanEnv(env Env) {
env.CleanContext()
}
func (ec *ExportContext) diffString(shell Shell) string {
oldDiff := ec.oldEnv.Diff(ec.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 {
logStatus(ec.env, "export %s", strings.Join(out, " "))
}
}
diff := ec.env.Diff(ec.newEnv)
return diff.ToShell(shell)
}
func direnvKey(key string) bool {
return strings.HasPrefix(key, "DIRENV_")
}