/
watch.go
134 lines (120 loc) · 2.71 KB
/
watch.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package watch
/*
* @see https://github.com/gophertown/looper/blob/master/watch.go
*/
import (
"errors"
"fmt"
"log"
"os"
"path/filepath"
"github.com/howeyc/fsnotify"
)
func debugMessage(format string, a ...interface{}) {
msg := fmt.Sprintf(format, a...)
fmt.Println(msg)
}
func debugError(msg error) {
fmt.Println(msg.Error())
}
// type FileNotifyEvent fsnotify.FileEvent
type RecursiveWatcher struct {
Watcher *fsnotify.Watcher
Flags uint32
OnEventFunc func(event *fsnotify.FileEvent)
path string
Filter func(path string) bool
}
func NewRecurisveWatcher(path string) (*RecursiveWatcher, error) {
watcher, err := fsnotify.NewWatcher()
if err != nil {
return nil, err
}
rw := &RecursiveWatcher{
Watcher: watcher,
path: path,
Flags: fsnotify.FSN_ALL,
}
rw.Filter = func(path string) bool {
return true
}
return rw, nil
}
func (rw *RecursiveWatcher) Close() error {
return rw.Watcher.Close()
}
func (rw *RecursiveWatcher) watchFolder(folder string) {
err := rw.Watcher.WatchFlags(folder, rw.Flags)
if err != nil {
log.Println("Error watching: ", folder, err)
}
fmt.Println("watch:", folder, rw.Flags)
}
func (rw *RecursiveWatcher) AddFolder(path string) {
folders := rw.walkSubfolders(path)
if len(folders) == 0 {
errors.New("No folders to watch.")
return
}
for _, folder := range folders {
rw.watchFolder(folder)
}
}
func (rw *RecursiveWatcher) Run(debug bool) {
rw.AddFolder(rw.path)
// go func() {
for {
select {
case event := <-rw.Watcher.Event:
if !rw.Filter(event.Name) {
if debug {
debugMessage("Skip %s", event)
}
continue
}
// create a file/directory
if event.IsCreate() {
fi, err := os.Stat(event.Name)
if err != nil {
// eg. stat .subl513.tmp : no such file or directory
if debug {
debugError(err)
}
} else if fi.IsDir() {
if debug {
debugMessage("Detected new directory %s", event.Name)
}
rw.watchFolder(event.Name)
}
}
debugMessage("Detected %s", event)
rw.OnEventFunc(event)
case err := <-rw.Watcher.Error:
log.Println("error", err)
}
}
// }()
}
// returns a slice of subfolders (recursive), including the folder passed in
func (rw *RecursiveWatcher) walkSubfolders(path string) (paths []string) {
filepath.Walk(path, func(newPath string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !rw.Filter(newPath) {
return filepath.SkipDir
}
if info.IsDir() {
name := info.Name()
// skip folders that begin with a dot
hidden := filepath.HasPrefix(name, ".") && name != "." && name != ".."
if hidden {
return filepath.SkipDir
} else {
paths = append(paths, newPath)
}
}
return nil
})
return paths
}