forked from direnv/direnv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd_watch.go
57 lines (46 loc) · 1 KB
/
cmd_watch.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
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
}