File: cmd_watch.go

package info (click to toggle)
direnv 2.25.2-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 9,512 kB
  • sloc: asm: 1,643; sh: 1,595; csh: 76; makefile: 27; ansic: 24
file content (57 lines) | stat: -rw-r--r-- 1,029 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
57
package main

import (
	"fmt"
	"os"
)

// CmdWatch is `direnv watch SHELL [PATH...]`
var CmdWatch = &Cmd{
	Name:    "watch",
	Desc:    "Adds a path to the list that direnv watches for changes",
	Args:    []string{"SHELL", "PATH..."},
	Private: true,
	Action:  actionSimple(cmdWatchAction),
}

func cmdWatchAction(env Env, args []string) (err error) {
	var shellName string

	if len(args) < 2 {
		return fmt.Errorf("a path is required to add to the list of watches")
	}
	if len(args) >= 2 {
		shellName = args[1]
	} else {
		shellName = "bash"
	}

	shell := DetectShell(shellName)

	if shell == nil {
		return fmt.Errorf("unknown target shell '%s'", shellName)
	}

	watches := NewFileTimes()
	watchString, ok := env[DIRENV_WATCHES]
	if ok {
		err = watches.Unmarshal(watchString)
		if err != nil {
			return
		}
	}

	for _, arg := range args[2:] {
		err = watches.Update(arg)
		if err != nil {
			return
		}
	}

	e := make(ShellExport)
	e.Add(DIRENV_WATCHES, watches.Marshal())

	os.Stdout.WriteString(shell.Export(e))

	return
}