-
Notifications
You must be signed in to change notification settings - Fork 0
/
notify.go
85 lines (67 loc) · 1.43 KB
/
notify.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
package walkie
import (
"github.com/Sirupsen/logrus"
"github.com/fsnotify/fsnotify"
"path/filepath"
)
// init_notify (re) create a watcher
func (w *Walkie) notify_init() error {
if w.watcher != nil {
w.watcher.Close()
}
watcher, err := fsnotify.NewWatcher()
w.watcher = watcher
if err != nil {
return err
}
go w.notify_loop()
return nil
}
// Start Watcher loop
func (w *Walkie) notify_loop() {
// done := make(chan bool)
for {
// logrus.Debug("Notify loop")
select {
case event, ok := <-w.watcher.Events:
if !ok {
return
}
logrus.Debugf("event:%s", event)
if event.Op&fsnotify.Write == fsnotify.Write {
logrus.Debugf("modified file:%s", event.Name)
}
case err, ok := <-w.watcher.Errors:
if !ok {
return
}
logrus.Errorf("error:%s", err)
}
}
// <-done
}
// Start Watcher loop
func (w *Walkie) notify_all() {
// done := make(chan bool)
err := w.watcher.Add(w.path)
if err != nil {
logrus.Errorf("Can't watch self directory %s", w.path)
}
for path := range w.directories {
err = w.watcher.Add(filepath.Join(w.path, path))
if err != nil {
logrus.Errorf("Can't watch directory %s", filepath.Join(w.path, path))
}
}
for path := range w.files {
err = w.watcher.Add(filepath.Join(w.path, path))
if err != nil {
logrus.Errorf("Can't watch file %s", filepath.Join(w.path, path))
}
}
// <-done
}
// Start Watcher loop
func (w *Walkie) Watch() {
w.notify_all()
}