forked from gravityblast/fresh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
watcher.go
57 lines (47 loc) · 944 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
51
52
53
54
55
56
57
package runner
import (
"os"
"path/filepath"
"strings"
"github.com/howeyc/fsnotify"
)
func watchFolder(path string) {
watcher, err := fsnotify.NewWatcher()
if err != nil {
fatal(err)
}
go func() {
for {
select {
case ev := <-watcher.Event:
if isWatchedFile(ev.Name) {
watcherLog("sending event %s", ev)
startChannel <- ev.String()
}
case err := <-watcher.Error:
watcherLog("error: %s", err)
}
}
}()
watcherLog("Watching %s", path)
err = watcher.Watch(path)
if err != nil {
fatal(err)
}
}
func watch() {
root := root()
filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
if info.IsDir() && !isTmpDir(path) {
if len(path) > 1 && strings.HasPrefix(filepath.Base(path), ".") {
return filepath.SkipDir
}
if isIgnoredFolder(path) {
watcherLog("Ignoring %s", path)
return filepath.SkipDir
}
watchFolder(path)
}
return err
})
}