File: cmd_dump.go

package info (click to toggle)
direnv 2.37.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 872 kB
  • sloc: sh: 1,499; csh: 83; makefile: 7
file content (56 lines) | stat: -rw-r--r-- 995 bytes parent folder | download
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
package cmd

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)
	}

	dumpStr, err := shell.Dump(env)
	if err != nil {
		return err
	}
	_, err = fmt.Fprintln(w, dumpStr)

	return
}