-
Notifications
You must be signed in to change notification settings - Fork 0
/
watcher.go
50 lines (46 loc) · 899 Bytes
/
watcher.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
package file
import (
"github.com/fsnotify/fsnotify"
"github.com/huckops/auto_config/source"
"os"
)
type watcher struct {
f *file
fw *fsnotify.Watcher
}
func (w *watcher) Next() (*source.ChangeSet, error) {
select {
case event, _ := <-w.fw.Events:
if event.Op == fsnotify.Rename {
// check existence of file, and add watch again
_, err := os.Stat(event.Name)
if err == nil || os.IsExist(err) {
err := w.fw.Add(event.Name)
if err != nil {
return nil, err
}
}
}
c, err := w.f.Read()
if err != nil {
return nil, err
}
return c, nil
case err := <-w.fw.Errors:
return nil, err
}
}
func (w *watcher) Stop() error {
return w.fw.Close()
}
func NewWatcher(f *file) (source.Watcher, error) {
fswatcher, err := fsnotify.NewWatcher()
if err != nil {
return nil, err
}
fswatcher.Add(f.path)
return &watcher{
f: f,
fw: fswatcher,
}, nil
}