forked from direnv/direnv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd_apply_dump.go
47 lines (37 loc) · 873 Bytes
/
cmd_apply_dump.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
package main
import (
"fmt"
"io/ioutil"
)
// CmdApplyDump is `direnv apply_dump FILE`
var CmdApplyDump = &Cmd{
Name: "apply_dump",
Desc: "Accepts a filename containing `direnv dump` output and generates a series of bash export statements to apply the given env",
Args: []string{"FILE"},
Private: true,
Action: actionSimple(cmdApplyDumpAction),
}
func cmdApplyDumpAction(env Env, args []string) (err error) {
if len(args) < 2 {
return fmt.Errorf("not enough arguments")
}
if len(args) > 2 {
return fmt.Errorf("too many arguments")
}
filename := args[1]
dumped, err := ioutil.ReadFile(filename)
if err != nil {
return err
}
dumpedEnv, err := LoadEnv(string(dumped))
if err != nil {
return err
}
diff := env.Diff(dumpedEnv)
exports := diff.ToShell(Bash)
_, err = fmt.Println(exports)
if err != nil {
return err
}
return
}