-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathwatch.go
56 lines (50 loc) · 1.21 KB
/
watch.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
package taskrunner
import (
"context"
"log"
zglob "github.com/mattn/go-zglob"
"github.com/samsarahq/taskrunner/watcher"
)
// IsTaskSource checks if a provided path matches a source glob for the task.
func IsTaskSource(task *Task, path string) (matches bool) {
for _, source := range task.Sources {
if ok, err := zglob.Match(source, path); err != nil {
log.Fatalf("invalid glob (%s):\n%v\n", path, err)
} else if ok {
matches = true
}
}
for _, ignore := range task.Ignore {
if ok, err := zglob.Match(ignore, path); err != nil {
log.Fatalf("invalid glob (%s):\n%v\n", path, err)
} else if ok {
matches = false
}
}
return matches
}
func (e *Executor) runWatch(ctx context.Context) {
watcher := watcher.NewWatcher(e.config.WorkingDir)
for _, enhancer := range e.watcherEnhancers {
watcher = enhancer(watcher)
}
go func() {
for event := range watcher.Events() {
for task := range e.tasks {
if IsTaskSource(task, event.RelativeFilename) {
go e.Invalidate(task, FileChange{
File: event.RelativeFilename,
})
}
}
}
}()
e.wg.Go(func() error {
if err := watcher.Run(ctx); err != nil {
if ctx.Err() != context.Canceled {
return err
}
}
return nil
})
}