-
Notifications
You must be signed in to change notification settings - Fork 52
/
main.go
316 lines (267 loc) · 6.21 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
package main
import (
"errors"
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"os/exec"
"path"
"reflect"
"regexp"
"strconv"
"strings"
"syscall"
"time"
"github.com/fsnotify/fsnotify"
)
var (
debug = flag.Bool("v", false, "Enable verbose debugging output")
term = flag.Bool("t", false, "Just run in the terminal (instead of an acme win)")
exclude = flag.String("x", "", "Exclude files and directories matching this regular expression")
watchPath = flag.String("p", ".", "The path to watch")
)
var excludeRe *regexp.Regexp
const rebuildDelay = 200 * time.Millisecond
// The name of the syscall.SysProcAttr.Setpgid field.
const setpgidName = "Setpgid"
var (
hasSetPGID bool
killChan = make(chan time.Time, 1)
)
type ui interface {
redisplay(func(io.Writer))
// An empty struct is sent when the command should be rerun.
rerun() <-chan struct{}
}
type writerUI struct{ io.Writer }
func (w writerUI) redisplay(f func(io.Writer)) { f(w) }
func (w writerUI) rerun() <-chan struct{} { return nil }
func main() {
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage of %s: [flags] command [command args…]\n", os.Args[0])
flag.PrintDefaults()
}
flag.Parse()
t := reflect.TypeOf(syscall.SysProcAttr{})
f, ok := t.FieldByName(setpgidName)
if ok && f.Type.Kind() == reflect.Bool {
debugPrint("syscall.SysProcAttr.Setpgid exists and is a bool")
hasSetPGID = true
} else if ok {
debugPrint("syscall.SysProcAttr.Setpgid exists but is a %s, not a bool", f.Type.Kind())
} else {
debugPrint("syscall.SysProcAttr.Setpgid does not exist")
}
if flag.NArg() == 0 {
flag.Usage()
os.Exit(1)
}
ui := ui(writerUI{os.Stdout})
if !*term {
wd, err := os.Getwd()
if err != nil {
log.Fatalln("Failed to get the current directory")
}
if ui, err = newWin(wd); err != nil {
log.Fatalln("Failed to open a win:", err)
}
}
if *exclude != "" {
var err error
excludeRe, err = regexp.Compile(*exclude)
if err != nil {
log.Fatalln("Bad regexp: ", *exclude)
}
}
timer := time.NewTimer(0)
changes := startWatching(*watchPath)
lastRun := time.Time{}
lastChange := time.Now()
for {
select {
case lastChange = <-changes:
timer.Reset(rebuildDelay)
case <-ui.rerun():
lastRun = run(ui)
case <-timer.C:
if lastRun.Before(lastChange) {
lastRun = run(ui)
}
}
}
}
func run(ui ui) time.Time {
ui.redisplay(func(out io.Writer) {
cmd := exec.Command(flag.Arg(0), flag.Args()[1:]...)
cmd.Stdout = out
cmd.Stderr = out
if hasSetPGID {
var attr syscall.SysProcAttr
reflect.ValueOf(&attr).Elem().FieldByName(setpgidName).SetBool(true)
cmd.SysProcAttr = &attr
}
io.WriteString(out, strings.Join(flag.Args(), " ")+"\n")
start := time.Now()
if err := cmd.Start(); err != nil {
io.WriteString(out, "fatal: "+err.Error()+"\n")
os.Exit(1)
}
if s := wait(start, cmd); s != 0 {
io.WriteString(out, "exit status "+strconv.Itoa(s)+"\n")
}
io.WriteString(out, time.Now().String()+"\n")
})
return time.Now()
}
func wait(start time.Time, cmd *exec.Cmd) int {
var n int
ticker := time.NewTicker(5 * time.Millisecond)
defer ticker.Stop()
for {
select {
case t := <-killChan:
if t.Before(start) {
continue
}
p := cmd.Process.Pid
if hasSetPGID {
p = -p
}
if n == 0 {
debugPrint("Sending SIGTERM")
syscall.Kill(p, syscall.SIGTERM)
} else {
debugPrint("Sending SIGKILL")
syscall.Kill(p, syscall.SIGKILL)
}
n++
case <-ticker.C:
var status syscall.WaitStatus
p := cmd.Process.Pid
switch q, err := syscall.Wait4(p, &status, syscall.WNOHANG, nil); {
case err != nil:
panic(err)
case q > 0:
cmd.Wait() // Clean up any goroutines created by cmd.Start.
return status.ExitStatus()
}
}
}
}
func kill() {
select {
case killChan <- time.Now():
debugPrint("Killing")
}
}
func startWatching(p string) <-chan time.Time {
w, err := fsnotify.NewWatcher()
if err != nil {
panic(err)
}
switch isdir, err := isDir(p); {
case err != nil:
log.Fatalf("Failed to watch %s: %s", p, err)
case isdir:
watchDir(w, p)
default:
watch(w, p)
}
changes := make(chan time.Time)
go sendChanges(w, changes)
return changes
}
func sendChanges(w *fsnotify.Watcher, changes chan<- time.Time) {
for {
select {
case err := <-w.Errors:
log.Fatalf("Watcher error: %s\n", err)
case ev := <-w.Events:
if excludeRe != nil && excludeRe.MatchString(ev.Name) {
debugPrint("ignoring event for excluded %s", ev.Name)
continue
}
time, err := modTime(ev.Name)
if err != nil {
log.Printf("Failed to get even time: %s", err)
continue
}
debugPrint("%s at %s", ev, time)
if ev.Op&fsnotify.Create != 0 {
switch isdir, err := isDir(ev.Name); {
case err != nil:
log.Printf("Couldn't check if %s is a directory: %s", ev.Name, err)
continue
case isdir:
watchDir(w, ev.Name)
}
}
changes <- time
}
}
}
func modTime(p string) (time.Time, error) {
switch s, err := os.Stat(p); {
case os.IsNotExist(err):
q := path.Dir(p)
if q == p {
err := errors.New("Failed to find directory for " + p)
return time.Time{}, err
}
return modTime(q)
case err != nil:
return time.Time{}, err
default:
return s.ModTime(), nil
}
}
func watchDir(w *fsnotify.Watcher, p string) {
ents, err := ioutil.ReadDir(p)
switch {
case os.IsNotExist(err):
return
case err != nil:
log.Printf("Failed to watch %s: %s", p, err)
}
for _, e := range ents {
sub := path.Join(p, e.Name())
if excludeRe != nil && excludeRe.MatchString(sub) {
debugPrint("excluding %s", sub)
continue
}
switch isdir, err := isDir(sub); {
case err != nil:
log.Printf("Failed to watch %s: %s", sub, err)
case isdir:
watchDir(w, sub)
}
}
watch(w, p)
}
func watch(w *fsnotify.Watcher, p string) {
debugPrint("Watching %s", p)
switch err := w.Add(p); {
case os.IsNotExist(err):
debugPrint("%s no longer exists", p)
case err != nil:
log.Printf("Failed to watch %s: %s", p, err)
}
}
func isDir(p string) (bool, error) {
switch s, err := os.Stat(p); {
case os.IsNotExist(err):
return false, nil
case err != nil:
return false, err
default:
return s.IsDir(), nil
}
}
func debugPrint(f string, vals ...interface{}) {
if *debug {
log.Printf("DEBUG: "+f, vals...)
}
}