forked from direnv/direnv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmd_watch_list.go
81 lines (68 loc) · 1.51 KB
/
cmd_watch_list.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package main
import (
"bufio"
"fmt"
"io"
"os"
"strconv"
"strings"
)
// CmdWatchList is `direnv watch-list`
var CmdWatchList = &Cmd{
Name: "watch-list",
Desc: "Pipe pairs of `mtime path` to stdin to build a list of files to watch.",
Args: []string{"[SHELL]"},
Private: true,
Action: actionSimple(watchListCommand),
}
func watchListCommand(env Env, args []string) (err error) {
var shellName string
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 err
}
}
// Read `mtime path` lines from stdin
reader := bufio.NewReader(os.Stdin)
i := 1
for {
line, err := reader.ReadString('\n')
if err == nil {
elems := strings.SplitN(line, " ", 2)
if len(elems) != 2 {
return fmt.Errorf("line %d: expected to contain two elements", i)
}
mtime, err := strconv.Atoi(elems[0])
if err != nil {
return fmt.Errorf("line %d: %s", i, err)
}
path := elems[1][:len(elems[1])-1]
// add to watches
err = watches.NewTime(path, int64(mtime), true)
if err != nil {
return err
}
} else if err == io.EOF {
break
} else {
return fmt.Errorf("line %d: %s", i, err)
}
i++
}
e := make(ShellExport)
e.Add(DIRENV_WATCHES, watches.Marshal())
os.Stdout.WriteString(shell.Export(e))
return
}