/
watch.go
51 lines (40 loc) · 837 Bytes
/
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
package fs
import (
"github.com/fsnotify/fsnotify"
log "github.com/sirupsen/logrus"
)
// Watch notifies for file changes for the given file or directory
func Watch(paths []string) error {
// Initialize
watcher, err := fsnotify.NewWatcher()
defer watcher.Close()
if err != nil {
return err
}
// Set up a watcher for all paths
for _, path := range paths {
err = watcher.Add(path)
if err != nil {
return err
}
}
select {
case event, ok := <-watcher.Events:
// Not 100% sure what a !ok event is
// so debug for now
if !ok {
log.Debugf("Got a !ok event %v", event)
}
case err, ok := <-watcher.Errors:
// Not 100% sure what an ok error is
// so debug for now
if ok {
log.Debugf("Got an ok error %v", err)
}
// A very clear !ok plus error
if !ok {
return err
}
}
return nil
}