-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
56 lines (45 loc) · 1.01 KB
/
main.go
File metadata and controls
56 lines (45 loc) · 1.01 KB
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
package main
import (
"bufio"
"flag"
"fmt"
"os"
"github.com/fsnotify/fsnotify"
)
func watch(watcher *fsnotify.Watcher, file *string, verbose *bool) {
err := watcher.Add(*file)
if err != nil {
fmt.Fprintf(os.Stderr, "ignore %s\n", *file)
return
}
if *verbose == true {
fmt.Fprintf(os.Stderr, "watch %s\n", *file)
}
}
func main() {
verbose := flag.Bool("verbose", false, "more output (default: false)")
flag.Parse()
watcher, watcher_err := fsnotify.NewWatcher()
if watcher_err != nil {
panic(watcher_err)
}
defer watcher.Close()
for _, arg := range flag.Args() {
if arg != "-" {
watch(watcher, &arg, verbose)
continue
}
stdin := bufio.NewScanner(os.Stdin)
for stdin.Scan() {
line := stdin.Text()
watch(watcher, &line, verbose)
}
}
fmt.Fprintf(os.Stderr, "[%4d files] wait for changes...\n", len(watcher.WatchList()))
event, ok := <-watcher.Events
if ok == false {
panic(fmt.Errorf("event not ok"))
}
fmt.Fprintf(os.Stderr, "[%s] %s\n", event.Op, event.Name)
os.Exit(0)
}