File: wait.go

package info (click to toggle)
golang-doozer 0.0~git20130909-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 224 kB
  • ctags: 283
  • sloc: makefile: 11
file content (62 lines) | stat: -rw-r--r-- 1,418 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
58
59
60
61
62
package main

import (
	"flag"
	"fmt"
	"os"
)

var timeout = flag.Duration("t", 0, "wait timeout")

func init() {
	flag.Parse()
	cmds["wait"] = cmd{wait, "<glob>", "wait for a change"}
	cmdHelp["wait"] = `Prints the next change to a file matching <glob>.

If flag -r is given, prints the next change on or after <rev>.
if flag -t is given, the wait will timeout after the specified
	duration (i.e. -t 1m30s will wait 1 minute and 30 seconds,
	valid time units: "ns", "us" (or "µs"), "ms", "s", "m", "h").

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> <rev> <set|del> <len> LF <body> LF

Here, <path> is the file's path, <rev> is the revision, <len> is the number of
bytes in the body, and LF is an ASCII line-feed char.
`
}

func wait(path string) {
	c := dial()

	if *rrev == -1 {
		var err error
		*rrev, err = c.Rev()
		if err != nil {
			bail(err)
		}
	}

	ev, err := c.WaitTimeout(path, *rrev, *timeout)
	if err != nil {
		bail(err)
	}

	var sd string
	switch {
	case ev.IsSet():
		sd = "set"
	case ev.IsDel():
		sd = "del"
	}
	fmt.Println(ev.Path, ev.Rev, sd, len(ev.Body))
	os.Stdout.Write(ev.Body)
	fmt.Println()
}