forked from VojtechVitek/rerun
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwatcher.go
146 lines (121 loc) · 2.63 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
package rerun
import (
"os"
"path/filepath"
"strings"
"time"
"github.com/fsnotify/fsnotify"
)
type Watcher struct {
watcher *fsnotify.Watcher
watch map[string]struct{}
ignore map[string]struct{}
done chan struct{}
}
type ChangeSet struct {
FirstFile string
Files map[string]struct{}
Error error
}
func NewWatcher() (*Watcher, error) {
watcher, err := fsnotify.NewWatcher()
if err != nil {
return nil, err
}
w := &Watcher{
watcher: watcher,
watch: make(map[string]struct{}),
ignore: make(map[string]struct{}),
done: make(chan struct{}, 0),
}
return w, nil
}
func (w *Watcher) Add(paths ...string) {
for _, path := range paths {
w.watch[path] = struct{}{}
//fmt.Printf("Add %v\n", path)
}
}
func (w *Watcher) Ignore(paths ...string) {
for _, path := range paths {
w.ignore[path] = struct{}{}
//fmt.Printf("Ignore %v\n", path)
}
}
func (w *Watcher) Watch(delay time.Duration) <-chan ChangeSet {
// fmt.Println()
// resolve add + ignore paths
for path, _ := range w.watch { //s
filepath.Walk(path, func(path string, info os.FileInfo, err error) error {
if info != nil && !info.IsDir() {
return nil
}
if strings.HasSuffix(path, ".git") {
//fmt.Printf("skip %v\n", path)
return filepath.SkipDir
}
if _, ok := w.ignore[path]; ok {
//fmt.Printf("skip %v\n", path)
return filepath.SkipDir
}
w.watcher.Add(path)
//fmt.Printf("watch %v\n", path)
return nil
})
}
// fmt.Println()
changes := make(chan ChangeSet, 1)
go func() {
for {
change := ChangeSet{
Files: make(map[string]struct{}),
}
timeout := time.NewTimer(1<<63 - 1) // max duration
timeout.Stop()
loop:
for {
select {
case event := <-w.watcher.Events:
// Ignore CHMOD.
if event.Op&fsnotify.Chmod == fsnotify.Chmod {
continue
}
timeout.Reset(delay)
//fmt.Printf("event: %v (%v)\n", event, time.Now()) //
// if event.Op&fsnotify.Write == fsnotify.Write {
// log.Println("modified file:", event.Name)
// }
if len(change.Files) == 0 {
change.FirstFile = event.Name
}
change.Files[event.Name] = struct{}{}
case err := <-w.watcher.Errors:
change.Error = err
changes <- change
timeout.Stop()
break loop
case <-timeout.C:
changes <- change
timeout.Stop()
break loop
case <-w.done:
close(changes)
timeout.Stop()
return
}
}
}
}()
return changes
}
func (w *Watcher) Close() error {
close(w.done)
return w.watcher.Close()
}
func (c *ChangeSet) String() string {
str := ""
for file, _ := range c.Files {
str += "\n" + file
}
return str
}