forked from githubnemo/CompileDaemon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
watcher.go
203 lines (177 loc) · 4.24 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
package main
import (
"errors"
"fmt"
"log"
"os"
"path/filepath"
"regexp"
"syscall"
"time"
"github.com/fsnotify/fsnotify"
pollingWatcher "github.com/radovskyb/watcher"
)
func directoryShouldBeTracked(cfg *WatcherConfig, path string) bool {
return cfg.flagRecursive == true && !cfg.flagExcludedDirs.Matches(path)
}
func pathMatches(cfg *WatcherConfig, path string) bool {
base := filepath.Base(path)
return (cfg.flagIncludedFiles.Matches(base) || matchesPattern(cfg.pattern, path)) &&
!cfg.flagExcludedFiles.Matches(base)
}
type WatcherConfig struct {
flagVerbose bool
flagPolling bool
flagRecursive bool
flagPollingInterval int
flagDirectories globList
flagExcludedDirs globList
flagExcludedFiles globList
flagIncludedFiles globList
pattern *regexp.Regexp
}
type FileWatcher interface {
Close() error
AddFiles() error
add(path string) error
Watch(jobs chan<- string)
getConfig() *WatcherConfig
}
type NotifyWatcher struct {
watcher *fsnotify.Watcher
cfg *WatcherConfig
}
func (n NotifyWatcher) Close() error {
return n.watcher.Close()
}
func (n NotifyWatcher) AddFiles() error {
return addFiles(n)
}
func (n NotifyWatcher) Watch(jobs chan<- string) {
for {
select {
case ev := <-n.watcher.Events:
if ev.Op&fsnotify.Remove == fsnotify.Remove || ev.Op&fsnotify.Write == fsnotify.Write || ev.Op&fsnotify.Create == fsnotify.Create {
// Assume it is a directory and track it.
if directoryShouldBeTracked(n.cfg, ev.Name) {
n.watcher.Add(ev.Name)
}
if pathMatches(n.cfg, ev.Name) {
jobs <- ev.Name
}
}
case err := <-n.watcher.Errors:
if v, ok := err.(*os.SyscallError); ok {
if v.Err == syscall.EINTR {
continue
}
log.Fatal("watcher.Error: SyscallError:", v)
}
log.Fatal("watcher.Error:", err)
}
}
}
func (n NotifyWatcher) add(path string) error {
return n.watcher.Add(path)
}
func (n NotifyWatcher) getConfig() *WatcherConfig {
return n.cfg
}
type PollingWatcher struct {
watcher *pollingWatcher.Watcher
cfg *WatcherConfig
}
func (p PollingWatcher) Close() error {
p.watcher.Close()
return nil
}
func (p PollingWatcher) AddFiles() error {
p.watcher.AddFilterHook(pollingWatcher.RegexFilterHook(p.cfg.pattern, false))
return addFiles(p)
}
func (p PollingWatcher) Watch(jobs chan<- string) {
// Start the watching process.
go func() {
if err := p.watcher.Start(time.Duration(p.cfg.flagPollingInterval) * time.Millisecond); err != nil {
log.Fatalln(err)
}
}()
for {
select {
case event := <-p.watcher.Event:
if p.cfg.flagVerbose {
// Print the event's info.
fmt.Println(event)
}
if pathMatches(p.cfg, event.Path) {
jobs <- event.String()
}
case err := <-p.watcher.Error:
if err == pollingWatcher.ErrWatchedFileDeleted {
continue
}
log.Fatalln(err)
case <-p.watcher.Closed:
return
}
}
}
func (p PollingWatcher) add(path string) error {
return p.watcher.Add(path)
}
func (p PollingWatcher) getConfig() *WatcherConfig {
return p.cfg
}
func NewWatcher(cfg *WatcherConfig) (FileWatcher, error) {
if cfg == nil {
err := errors.New("no config specified")
return nil, err
}
if cfg.flagPolling {
w := pollingWatcher.New()
return PollingWatcher{
watcher: w,
cfg: cfg,
}, nil
} else {
w, err := fsnotify.NewWatcher()
if err != nil {
return nil, err
}
return NotifyWatcher{
watcher: w,
cfg: cfg,
}, nil
}
}
func addFiles(fw FileWatcher) error {
cfg := fw.getConfig()
for _, flagDirectory := range cfg.flagDirectories {
if cfg.flagRecursive == true {
err := filepath.WalkDir(flagDirectory, func(path string, entry os.DirEntry, err error) error {
if err == nil && entry.IsDir() {
if cfg.flagExcludedDirs.Matches(path) {
return filepath.SkipDir
} else {
if cfg.flagVerbose {
log.Printf("Watching directory '%s' for changes.\n", path)
}
return fw.add(path)
}
}
return err
})
if err != nil {
return fmt.Errorf("filepath.Walk(): %v", err)
}
if err := fw.add(flagDirectory); err != nil {
return fmt.Errorf("FileWatcher.Add(): %v", err)
}
} else {
if err := fw.add(flagDirectory); err != nil {
return fmt.Errorf("FileWatcher.AddFiles(): %v", err)
}
}
}
return nil
}