-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
144 lines (127 loc) · 2.83 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package main
import (
"os"
"os/exec"
"path/filepath"
"time"
"github.com/fsnotify/fsnotify"
)
func main() {
debounce := 300 * time.Millisecond
eventMask := fsnotify.Create | fsnotify.Write | fsnotify.Chmod | fsnotify.Rename
globs := []string{}
i := 1
for i < len(os.Args)-1 {
if os.Args[i] == "-d" {
var err error
debounce, err = time.ParseDuration(os.Args[i+1])
if err != nil {
usageError()
}
i += 2
continue
}
if os.Args[i] == "-m" {
eventMask = 0
for _, c := range os.Args[i+1] {
switch c {
case 'c':
eventMask |= fsnotify.Create
case 'w':
eventMask |= fsnotify.Write
case 'r':
eventMask |= fsnotify.Remove
case 'm':
eventMask |= fsnotify.Rename
case 'a':
eventMask |= fsnotify.Chmod
}
}
i += 2
continue
}
if os.Args[i] == "-g" {
globs = append(globs, os.Args[i+1])
i += 2
continue
}
break
}
args := os.Args[i:]
if len(args) < 2 {
usageError()
}
files := args[:len(args)-1]
command := args[len(args)-1]
shell := os.Getenv("SHELL")
if shell == "" {
shell = "/bin/sh"
}
watcher, err := fsnotify.NewWatcher()
if err != nil {
os.Stdout.WriteString(err.Error())
os.Exit(1)
}
defer watcher.Close()
failed := 0
for _, f := range files {
err := watcher.Add(f)
if err != nil {
os.Stderr.WriteString("can't watch " + f + ": " + err.Error() + "\n")
failed++
}
}
if failed == len(files) {
os.Exit(1)
}
events := map[string]fsnotify.Op{}
timer := time.NewTimer(debounce)
timer.Stop()
for {
select {
case <-timer.C:
args := make([]string, 2, len(events)+3)
args[0] = "-c"
args[1] = command
if len(shell) < 2 || (shell[len(shell)-2:] != "es" && shell[len(shell)-2:] != "rc") {
args = append(args, shell)
}
for ev := range events {
args = append(args, ev)
}
cmd := exec.Command(shell, args...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Run()
events = map[string]fsnotify.Op{}
case ev := <-watcher.Events:
match := false
for _, glob := range globs {
if matched, _ := filepath.Match(glob, filepath.Base(ev.Name)); matched {
match = true
}
}
if (match || len(globs) == 0) && ev.Op&eventMask == ev.Op {
events[ev.Name] = ev.Op
// docs say not to call reset concurrently with <-timer.C
// is this ok? haven't had problems yet
timer.Reset(debounce)
}
case err := <-watcher.Errors:
os.Stderr.WriteString(err.Error())
}
}
}
func usageError() {
os.Stderr.WriteString(`usage: on-change [-d debounce] [-e eventmask] [-g glob] FILES... CMD
-d debounce time. (default: 300ms)
-e event mask. (default: cwma)
include these characters to listen for these events:
c create
w write
r remove
m rename (move)
a chmod (access)
-g trigger events only when the file basename matches one of the given globs.`)
os.Exit(2)
}