-
Notifications
You must be signed in to change notification settings - Fork 4
/
watcher.go
159 lines (129 loc) · 2.78 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
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package leaf
import (
"context"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/fsnotify/fsnotify"
)
// WatchResult has the file changed or the error that occurred
// during watching.
type WatchResult struct {
File string
Err error
}
// Watcher watches a directory for changes and updates the
// stream when a file change (valid by filters) is updated.
type Watcher struct {
root string
paths []string
exclude []string
fc *FilterCollection
notifier *fsnotify.Watcher
res chan WatchResult
}
// NewWatcher returns a watcher from the given options.
func NewWatcher(root string, exclude []string, fc *FilterCollection) (*Watcher, error) {
w := &Watcher{
fc: fc,
res: make(chan WatchResult),
}
isdir, err := isDir(root)
if err != nil {
return nil, err
}
if !isdir {
return nil, fmt.Errorf(
"path '%s' is not a directory", root)
}
w.root = filepath.Clean(root)
w.paths, err = getAllDirs(w.root)
if err != nil {
return nil, err
}
w.exclude = []string{}
for _, path := range exclude {
var absPath string
if _, err = isDir(path); err != nil {
continue
}
absPath, err = filepath.Abs(path)
if err != nil {
continue
}
w.exclude = append(w.exclude, absPath)
}
w.notifier, err = fsnotify.NewWatcher()
if err != nil {
return nil, err
}
for _, f := range w.paths {
exclude := false
for _, e := range w.exclude {
if strings.HasPrefix(f, e) {
exclude = true
break
}
}
if exclude {
continue
}
if err := w.notifier.Add(f); err != nil {
return nil, err
}
}
return w, nil
}
// Watch executes the watching of files. Exits on cancellation
// of the context.
func (w *Watcher) Watch(ctx context.Context) <-chan WatchResult {
go w.startWatcher(ctx)
return w.res
}
// startWatcher starts the fs.Notifier and watches for changes
// in files in the root directory.
func (w *Watcher) startWatcher(ctx context.Context) {
defer w.notifier.Close() // nolint:errcheck
for {
select {
case event := <-w.notifier.Events:
if event.Op == fsnotify.Write {
file := event.Name
if w.fc.ShouldHandlePath(file) {
w.res <- WatchResult{File: file}
}
}
case err := <-w.notifier.Errors:
if err != nil {
w.res <- WatchResult{Err: err}
}
case <-ctx.Done():
close(w.res)
return
}
}
}
// getAllDirs gets all the directories (including the root)
// inside the given root directory.
func getAllDirs(root string) ([]string, error) {
paths := []string{}
walkFn := func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
absPath, err := filepath.Abs(path)
if err != nil {
return err
}
paths = append(paths, absPath)
}
return nil
}
err := filepath.Walk(root, walkFn)
if err != nil {
return nil, err
}
return paths, nil
}