forked from direnv/direnv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd_dump.go
52 lines (43 loc) · 938 Bytes
/
cmd_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
48
49
50
51
52
package main
import (
"fmt"
"os"
"strconv"
)
// CmdDump is `direnv dump`
var CmdDump = &Cmd{
Name: "dump",
Desc: "Used to export the inner bash state at the end of execution",
Args: []string{"[SHELL]", "[FILE]"},
Private: true,
Action: actionSimple(cmdDumpAction),
}
func cmdDumpAction(env Env, args []string) (err error) {
target := "gzenv"
w := os.Stdout
if len(args) > 1 {
target = args[1]
}
var filePath string
if len(args) > 2 {
filePath = args[2]
} else {
filePath = os.Getenv(DIRENV_DUMP_FILE_PATH)
}
if filePath != "" {
if num, err := strconv.Atoi(filePath); err == nil {
w = os.NewFile(uintptr(num), filePath)
} else {
w, err = os.OpenFile(filePath, os.O_WRONLY, 0666)
if err != nil {
return err
}
}
}
shell := DetectShell(target)
if shell == nil {
return fmt.Errorf("unknown target shell '%s'", target)
}
_, err = fmt.Fprintln(w, shell.Dump(env))
return
}