forked from ha/doozer
/
watch.go
50 lines (37 loc) · 1.05 KB
/
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
package main
import (
"doozer/client"
"fmt"
"os"
)
func init() {
cmds["watch"] = cmd{watch, "<glob>", "get notified of changes"}
cmdHelp["watch"] = `Prints changes to each file matching <glob>.
Rules for <glob> pattern-matching:
- '?' matches a single char in a single path component
- '*' matches zero or more chars in a single path component
- '**' matches zero or more chars in zero or more components
- any other sequence matches itself
Output is a sequence of records, one for each change. Format of each record:
<path> <cas> <len> LF <body> LF
Here, <path> is the file's path, <cas> is the cas token, <len> is the number of
bytes in the body, <body> is the bytes of the body, and LF is an ASCII
line-feed char.
If a file is deleted, <cas> will be 0.
`
}
func watch(glob string) {
c := client.New("<test>", *addr)
w, err := c.Watch(glob)
if err != nil {
bail(err)
}
for ev := range w.C {
if ev.Err != nil {
fmt.Fprintln(os.Stderr, ev.Err)
}
fmt.Println(ev.Path, ev.Cas, len(ev.Body))
os.Stdout.Write(ev.Body)
fmt.Println()
}
}