forked from acoshift/goreload
-
Notifications
You must be signed in to change notification settings - Fork 1
/
watcher.go
191 lines (176 loc) · 3.82 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
package main
import (
"fmt"
"io/ioutil"
"os"
"path"
"path/filepath"
"regexp"
"strings"
"time"
"github.com/fsnotify/fsnotify"
"github.com/oxycoder/goreload/internal"
"github.com/radovskyb/watcher"
"github.com/urfave/cli/v2"
)
// a fallback watcher for docker in windows
func kwatch(c *cli.Context, runner internal.Runner, builder internal.Builder) {
w := watcher.New()
defer w.Close()
w.IgnoreHiddenFiles(true)
for _, x := range c.StringSlice("excludeDir") {
w.Ignore(x)
}
filterPattern := fmt.Sprintf(`.*(%s)`, c.String("ext"))
r := regexp.MustCompile(filterPattern)
w.AddFilterHook(watcher.RegexFilterHook(r, false))
if err := w.AddRecursive("."); err != nil {
logger.Fatalln(err)
}
if c.Bool("showWatchedFiles") {
for path, f := range w.WatchedFiles() {
fmt.Printf("%s: %s\n", path, f.Name())
}
}
logWarn("------>")
go func() {
haveModified := false
for {
select {
case event, ok := <-w.Event:
if !ok {
return
}
logInfo("modified file: %s", event.Name())
haveModified = true
case err, ok := <-w.Error:
if !ok {
return
}
logger.Println("error:", err)
case <-w.Closed:
return
case <-time.After(time.Millisecond * c.Duration("delay")):
if haveModified {
runner.Kill()
start(builder, runner, logger, c.Bool("debug"))
haveModified = false
}
}
}
}()
// Start the watching process - it'll check for changes every 100ms.
if err := w.Start(time.Millisecond * 200); err != nil {
logger.Fatalln(err)
}
}
// fsnotify watcher
func fwatcher(c *cli.Context, runner internal.Runner, builder internal.Builder) {
w, err := fsnotify.NewWatcher()
if err != nil {
logger.Fatal(err)
}
defer w.Close()
dirs, err := getAllDir(".", c.StringSlice("excludeDir")...)
if err != nil {
logger.Fatal(err)
}
for _, dir := range dirs {
if c.Bool("showWatchedFiles") {
logInfo("watching dir: %s", dir)
}
w.Add(dir)
}
logWarn("------>")
exts := strings.Split(c.String("ext"), "|")
haveModified := false
for {
select {
case event, ok := <-w.Events:
if !ok {
return
}
// check file extentions
if containExt(path.Ext(event.Name), exts) {
switch event.Op {
case fsnotify.Create:
if isDir(event.Name) {
if c.Bool("showWatchedFiles") {
logSuccess("%s added to watch list", event.Name)
}
w.Add(event.Name)
}
haveModified = true
case fsnotify.Remove:
if isDir(event.Name) {
w.Remove(event.Name)
}
haveModified = true
case fsnotify.Chmod:
default:
logInfo("modified file: %s", event.Name)
haveModified = true
}
}
case <-time.After(time.Millisecond * c.Duration("delay")):
if haveModified {
runner.Kill()
start(builder, runner, logger, c.Bool("debug"))
haveModified = false
}
case err, ok := <-w.Errors:
haveModified = false
logError("error: %s", err)
if !ok {
return
}
}
}
}
func getAllDir(pathname string, excludes ...string) ([]string, error) {
var allDir []string
rd, err := ioutil.ReadDir(pathname)
if err != nil {
logError("read dir fail: %s", err)
return allDir, err
}
allDir = append(allDir, pathname)
for _, fi := range rd {
if !fi.IsDir() {
continue
}
fullDir := pathname + "/" + fi.Name()
skip := false
for _, ex := range excludes {
if strings.Contains(fullDir, ex) {
skip = true
}
}
if skip {
continue
}
subdir, err := getAllDir(fullDir)
if err != nil {
logError("read dir fail: %s", err)
return allDir, err
}
allDir = append(allDir, subdir...)
}
return allDir, nil
}
func isDir(path string) bool {
absPath, _ := filepath.Abs(path)
fi, err := os.Stat(absPath)
if err != nil {
logError(err.Error())
}
return fi.IsDir()
}
func containExt(ext string, exts []string) bool {
for _, a := range exts {
if a == ext {
return true
}
}
return false
}