/
main.go
67 lines (57 loc) · 1.03 KB
/
main.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
package main
import (
"errors"
"fmt"
"os"
"path/filepath"
"time"
"github.com/fsnotify/fsnotify"
)
func fatal(err error) {
if err == nil {
return
}
fmt.Fprintf(os.Stderr, "%s: %s\n", filepath.Base(os.Args[0]), err)
os.Exit(1)
}
func line(s string, args ...interface{}) {
fmt.Printf(time.Now().Format("15:16:05.0000")+" "+s+"\n", args...)
}
func main() {
if len(os.Args) < 2 {
fatal(errors.New("must specify at least one path to watch"))
}
w, err := fsnotify.NewWatcher()
fatal(err)
defer w.Close()
go func() {
i := 0
for {
select {
case e, ok := <-w.Events:
if !ok {
return
}
i++
m := ""
if e.Op&fsnotify.Write == fsnotify.Write {
m = "(modified)"
}
line("%3d %-10s %-10s %q", i, e.Op, m, e.Name)
case err, ok := <-w.Errors:
if !ok {
return
}
line("ERROR: %s", err)
}
}
}()
for _, p := range os.Args[1:] {
err = w.Add(p)
if err != nil {
fatal(fmt.Errorf("%q: %w", p, err))
}
}
line("watching; press ^C to exit")
<-make(chan struct{})
}