-
Notifications
You must be signed in to change notification settings - Fork 0
/
path.go
269 lines (246 loc) · 5.39 KB
/
path.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
258
259
260
261
262
263
264
265
266
267
268
269
package fresher
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/fsnotify/fsnotify"
"github.com/goccy/go-yaml"
)
type Extensions []string
func (e Extensions) IsIncludeSameExt(fileName string) bool {
for _, ext := range e {
if fmt.Sprintf(".%s", ext) == filepath.Ext(fileName) {
return true
}
}
return false
}
type GlobalExclude []string
func (g GlobalExclude) IsExclude(path string) (bool, error) {
for _, d := range g {
ok, err := filepath.Match(d, path)
if err != nil {
return false, err
}
if ok {
return true, nil
}
}
return false, nil
}
type WatcherConfig struct {
Name string `yaml:"name"`
Excludes []string `yaml:"exclude"`
Includes []string `yaml:"include"`
}
func (r *WatcherConfig) UnmarshalYAML(b []byte) error {
s := struct {
Name string `yaml:"name"`
Excludes []string `yaml:"exclude"`
Includes []string `yaml:"include"`
}{}
if err := yaml.Unmarshal(b, &s); err != nil {
var name string
if err := yaml.Unmarshal(b, &name); err != nil {
return err
}
r.Name = name
return nil
}
r.Name = s.Name
r.Excludes = s.Excludes
r.Includes = s.Includes
return nil
}
func (r *WatcherConfig) IsExclude(path string) (bool, error) {
for _, f := range r.Excludes {
ok, err := filepath.Match(f, path)
if err != nil {
return false, err
}
if ok {
return true, nil
}
}
return false, nil
}
func (r *WatcherConfig) IsInclude(path string) (bool, error) {
if len(r.Includes) == 0 {
return true, nil
}
for _, f := range r.Includes {
ok, err := filepath.Match(f, path)
if err != nil {
return false, err
}
if ok {
return true, nil
}
}
return false, nil
}
func (r *WatcherConfig) ShouldWatchFile(path string, opt *Option) (bool, error) {
shouldWatch, err := r.shouldWatch(path, opt)
if err != nil {
return false, err
}
if !shouldWatch {
return false, nil
}
if !opt.exts.IsIncludeSameExt(path) {
return false, nil
}
return true, nil
}
func (r *WatcherConfig) shouldWatch(path string, opt *Option) (bool, error) {
path = filepath.Base(path)
isGlobalExclude, err := opt.globalExclude.IsExclude(path)
if err != nil {
return false, nil
}
if isGlobalExclude {
return false, nil
}
isExclude, err := r.IsExclude(path)
if err != nil {
return false, nil
}
if isExclude {
return false, nil
}
isInclude, err := r.IsInclude(path)
if err != nil {
return false, nil
}
if !isInclude {
return false, nil
}
return true, nil
}
func (r *WatcherConfig) SkipDir(dirName string, opt *Option) (bool, error) {
shouldWatch, err := r.shouldWatch(dirName, opt)
if err != nil {
return false, err
}
return !shouldWatch, nil
}
func (r *WatcherConfig) WalkWithDirName(watcher *fsnotify.Watcher, opt *Option, dirName string) (*WatcherPath, error) {
watcherPath := NewWatcherPath(opt.configs, opt)
if _, err := os.Stat(filepath.Join(dirName, r.Name)); err != nil {
if os.IsNotExist(err) {
log.IgnoreFile(filepath.Join(dirName, r.Name))
return watcherPath, nil
}
}
global := opt.globalExclude
isExclude, err := global.IsExclude(r.Name)
if err != nil {
return nil, err
}
if isExclude {
return watcherPath, nil
}
if err := filepath.Walk(filepath.Join(dirName, r.Name), func(path string, info os.FileInfo, err error) error {
if err != nil && err != filepath.SkipDir {
return err
}
if info.IsDir() {
if info.Name() == r.Name {
if err := watcher.Add(path); err != nil {
return err
}
return nil
}
skipDir, err := r.SkipDir(path, opt)
if err != nil {
return err
}
if !skipDir {
if err := watcher.Add(path); err != nil {
return err
}
return nil
}
return filepath.SkipDir
}
shouldWatch, err := r.ShouldWatchFile(path, opt)
if err != nil {
return err
}
if !shouldWatch {
log.IgnoreFile(path)
watcherPath.ignores[path] = struct{}{}
return nil
}
log.WatchFile(path)
watcherPath.watches[path] = struct{}{}
return nil
}); err != nil {
return nil, err
}
return watcherPath, nil
}
func (r *WatcherConfig) Walk(watcher *fsnotify.Watcher, opt *Option) (*WatcherPath, error) {
watcherPath, err := r.WalkWithDirName(watcher, opt, ".")
if err != nil {
return nil, err
}
return watcherPath, nil
}
type WatcherPath struct {
ignores map[string]struct{}
watches map[string]struct{}
wcs []*WatcherConfig
opt *Option
}
func NewWatcherPath(wcs []*WatcherConfig, option *Option) *WatcherPath {
return &WatcherPath{
ignores: map[string]struct{}{},
watches: map[string]struct{}{},
wcs: wcs,
opt: option,
}
}
func (w *WatcherPath) Merge(wp *WatcherPath) {
for ignore := range wp.ignores {
w.ignores[ignore] = struct{}{}
}
for watch := range wp.watches {
w.watches[watch] = struct{}{}
}
}
var (
skipToAddErr = fmt.Errorf("does not need to add file")
)
func (w *WatcherPath) AddIfNeeds(path string, watcher *fsnotify.Watcher) error {
if strings.Contains(path, "tmp___") {
return nil
}
if strings.Contains(path, "old___") {
return nil
}
if _, exists := w.ignores[path]; exists {
return skipToAddErr
}
if _, exists := w.watches[path]; exists {
return skipToAddErr
}
for _, wc := range w.wcs {
shouldWatch, err := wc.ShouldWatchFile(path, w.opt)
if err != nil {
return err
}
if shouldWatch {
log.WatchFile(path)
if err := watcher.Add(path); err != nil {
return err
}
w.watches[path] = struct{}{}
return nil
}
}
log.IgnoreFile(path)
w.ignores[path] = struct{}{}
return skipToAddErr
}