-
Notifications
You must be signed in to change notification settings - Fork 0
/
watcher.go
45 lines (36 loc) · 812 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
package main
import (
"log"
"time"
"github.com/radovskyb/watcher"
)
func initializeWatcher(path string) {
w := watcher.New()
w.SetMaxEvents(1)
w.FilterOps(watcher.Rename, watcher.Move, watcher.Create, watcher.Write)
if err := w.Add(path); err != nil {
log.Printf("%s: error trying to watch change on %s directory", err, path)
}
go func() {
if err := w.Start(time.Second); err != nil {
log.Println(err)
}
}()
readEventsFromWatcher(w, path)
}
func readEventsFromWatcher(w *watcher.Watcher, path string) {
go func() {
for {
select {
case evt := <-w.Event:
log.Println("modified file:", evt.Name())
LoadMockConfig(path)
case err := <-w.Error:
log.Println("error checking file change:", err)
LoadMockConfig(path)
case <-w.Closed:
return
}
}
}()
}