-
Notifications
You must be signed in to change notification settings - Fork 6
/
watcher.go
257 lines (210 loc) · 5.4 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
package gaper
import (
"errors"
"fmt"
"os"
"path/filepath"
"regexp"
"strings"
"time"
zglob "github.com/mattn/go-zglob"
)
// Watcher is a interface for the watch process
type Watcher interface {
Watch()
Errors() chan error
Events() chan string
}
// watcher is a interface for the watch process
type watcher struct {
defaultIgnore bool
pollInterval int
watchItems map[string]bool
ignoreItems map[string]bool
allowedExtensions map[string]bool
events chan string
errors chan error
}
// WatcherConfig defines the settings available for the watcher
type WatcherConfig struct {
DefaultIgnore bool
PollInterval int
WatchItems []string
IgnoreItems []string
Extensions []string
}
// NewWatcher creates a new watcher
func NewWatcher(cfg WatcherConfig) (Watcher, error) {
if cfg.PollInterval == 0 {
cfg.PollInterval = DefaultPoolInterval
}
if len(cfg.Extensions) == 0 {
cfg.Extensions = DefaultExtensions
}
allowedExts := make(map[string]bool)
for _, ext := range cfg.Extensions {
allowedExts["."+ext] = true
}
watchPaths, err := resolvePaths(cfg.WatchItems, allowedExts)
if err != nil {
return nil, err
}
ignorePaths, err := resolvePaths(cfg.IgnoreItems, allowedExts)
if err != nil {
return nil, err
}
logger.Debugf("Resolved watch paths: %v", watchPaths)
logger.Debugf("Resolved ignore paths: %v", ignorePaths)
return &watcher{
events: make(chan string),
errors: make(chan error),
defaultIgnore: cfg.DefaultIgnore,
pollInterval: cfg.PollInterval,
watchItems: watchPaths,
ignoreItems: ignorePaths,
allowedExtensions: allowedExts,
}, nil
}
var startTime = time.Now()
var errDetectedChange = errors.New("done")
// Watch starts watching for file changes
func (w *watcher) Watch() {
for {
for watchPath := range w.watchItems {
fileChanged, err := w.scanChange(watchPath)
if err != nil {
w.errors <- err
return
}
if fileChanged != "" {
w.events <- fileChanged
startTime = time.Now()
}
}
time.Sleep(time.Duration(w.pollInterval) * time.Millisecond)
}
}
// Events get events occurred during the watching
// these events are emitted only a file changing is detected
func (w *watcher) Events() chan string {
return w.events
}
// Errors get errors occurred during the watching
func (w *watcher) Errors() chan error {
return w.errors
}
func (w *watcher) scanChange(watchPath string) (string, error) {
logger.Debug("Watching ", watchPath)
var fileChanged string
err := filepath.Walk(watchPath, func(path string, info os.FileInfo, err error) error {
if err != nil {
// Ignore attempt to acess go temporary unmask
if strings.Contains(err.Error(), "-go-tmp-umask") {
return filepath.SkipDir
}
return fmt.Errorf("couldn't walk to path \"%s\": %v", path, err)
}
if w.ignoreFile(path, info) {
return skipFile(info)
}
ext := filepath.Ext(path)
if _, ok := w.allowedExtensions[ext]; ok && info.ModTime().After(startTime) {
fileChanged = path
return errDetectedChange
}
return nil
})
if err != nil && err != errDetectedChange {
return "", err
}
return fileChanged, nil
}
func (w *watcher) ignoreFile(path string, info os.FileInfo) bool {
// if a file has been deleted after gaper was watching it
// info will be nil in the other iterations
if info == nil {
return true
}
// check if preset ignore is enabled
if w.defaultIgnore {
// check for hidden files and directories
if name := info.Name(); name[0] == '.' && name != "." {
return true
}
// check if it is a Go testing file
if strings.HasSuffix(path, "_test.go") {
return true
}
// check if it is the vendor folder
if info.IsDir() && info.Name() == "vendor" {
return true
}
}
if _, ignored := w.ignoreItems[path]; ignored {
return true
}
return false
}
func resolvePaths(paths []string, extensions map[string]bool) (map[string]bool, error) {
result := map[string]bool{}
for _, path := range paths {
matches := []string{path}
isGlob := strings.Contains(path, "*")
if isGlob {
var err error
matches, err = zglob.Glob(path)
if err != nil {
return nil, fmt.Errorf("couldn't resolve glob path \"%s\": %v", path, err)
}
}
for _, match := range matches {
// ignore existing files that don't match the allowed extensions
if f, err := os.Stat(match); !os.IsNotExist(err) && !f.IsDir() {
if ext := filepath.Ext(match); ext != "" {
if _, ok := extensions[ext]; !ok {
continue
}
}
}
if _, ok := result[match]; !ok {
result[match] = true
}
}
}
removeOverlappedPaths(result)
return result, nil
}
// remove overlapped paths so it makes the scan for changes later faster and simpler
func removeOverlappedPaths(mapPaths map[string]bool) {
startDot := regexp.MustCompile(`^\./`)
for p1 := range mapPaths {
p1 = startDot.ReplaceAllString(p1, "")
// skip to next item if this path has already been checked
if v, ok := mapPaths[p1]; ok && !v {
continue
}
for p2 := range mapPaths {
p2 = startDot.ReplaceAllString(p2, "")
if p1 == p2 {
continue
}
if strings.HasPrefix(p2, p1) {
mapPaths[p2] = false
} else if strings.HasPrefix(p1, p2) {
mapPaths[p1] = false
}
}
}
// cleanup path list
for p := range mapPaths {
if !mapPaths[p] {
delete(mapPaths, p)
}
}
}
func skipFile(info os.FileInfo) error {
if info.IsDir() {
return filepath.SkipDir
}
return nil
}