forked from readdig/her
-
Notifications
You must be signed in to change notification settings - Fork 0
/
watcher.go
70 lines (61 loc) · 1.22 KB
/
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package her
import (
"github.com/go-code/her/notify"
"log"
"os"
"path/filepath"
"sync"
)
type Watcher struct {
watchers []*notify.Watcher
watcherPath string
notifyMutex sync.Mutex
}
func NewWatcher() *Watcher {
return &Watcher{}
}
func (w *Watcher) Listen(p string) {
w.watcherPath = p
if w.watcherPath != "" {
watcher, err := notify.NewWatcher()
if err != nil {
log.Fatal(err)
}
watcher.Event = make(chan *notify.FileEvent, 100)
watcher.Error = make(chan error, 10)
filepath.Walk(p, func(path string, info os.FileInfo, err error) error {
if err != nil {
log.Println("Error walking path:", err)
return nil
}
if info.IsDir() {
err = watcher.Watch(path)
if err != nil {
log.Println("Failed to watch", path, ":", err)
}
log.Println("Watching:", path)
}
return nil
})
w.watchers = append(w.watchers, watcher)
}
}
func (w *Watcher) Notify() {
w.notifyMutex.Lock()
defer w.notifyMutex.Unlock()
for _, watcher := range w.watchers {
for {
select {
case ev := <-watcher.Event:
templates = loadTemplate()
log.Println("event:", ev)
continue
case err := <-watcher.Error:
log.Println("error:", err)
continue
default:
}
break
}
}
}