1package main
2
3import (
4	"fmt"
5	"os"
6	"strconv"
7)
8
9// CmdDump is `direnv dump`
10var CmdDump = &Cmd{
11	Name:    "dump",
12	Desc:    "Used to export the inner bash state at the end of execution",
13	Args:    []string{"[SHELL]", "[FILE]"},
14	Private: true,
15	Action:  actionSimple(cmdDumpAction),
16}
17
18func cmdDumpAction(env Env, args []string) (err error) {
19	target := "gzenv"
20	w := os.Stdout
21
22	if len(args) > 1 {
23		target = args[1]
24	}
25
26	var filePath string
27	if len(args) > 2 {
28		filePath = args[2]
29	} else {
30		filePath = os.Getenv(DIRENV_DUMP_FILE_PATH)
31	}
32
33	if filePath != "" {
34		if num, err := strconv.Atoi(filePath); err == nil {
35			w = os.NewFile(uintptr(num), filePath)
36		} else {
37			w, err = os.OpenFile(filePath, os.O_WRONLY, 0666)
38			if err != nil {
39				return err
40			}
41		}
42	}
43
44	shell := DetectShell(target)
45	if shell == nil {
46		return fmt.Errorf("unknown target shell '%s'", target)
47	}
48
49	_, err = fmt.Fprintln(w, shell.Dump(env))
50
51	return
52}
53