-
Notifications
You must be signed in to change notification settings - Fork 0
/
watcher.go
67 lines (59 loc) · 1.28 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
package gim
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/fsnotify/fsnotify"
)
func (g *Gim) launchProjectWatcher(tableRoutes map[string]string) error {
watcher, err := fsnotify.NewWatcher()
if err != nil {
return err
}
go func() {
for {
select {
case event, ok := <-watcher.Events:
if !ok {
return
}
if event.Op&fsnotify.Write == fsnotify.Write {
if err := g.regenerateScripts(tableRoutes); err != nil {
fmt.Println(err)
}
if err := g.updateWatcherScope(); err != nil {
fmt.Println(err)
}
}
case err, ok := <-watcher.Errors:
if !ok {
return
}
fmt.Println("error:", err)
}
}
}()
g.watcher = watcher
return g.updateWatcherScope()
}
func (g *Gim) updateWatcherScope() error {
return filepath.Walk(g.Workspace.Path, func(p string, info os.FileInfo, err error) error {
rootLevel1 := ""
// log.Println(p, strings.HasPrefix(p, strings.Trim(g.Workspace.Path, "./")))
if strings.HasPrefix(p, strings.Trim(g.Workspace.Path, "./")) {
chunks := strings.Split(p, "/")
if len(chunks) > 1 {
rootLevel1 = chunks[0]
}
}
// log.Println("rootLevel1", rootLevel1)
if !g.fileInBlackList(rootLevel1) {
err = g.watcher.Add(p)
if err != nil {
return err
}
}
return nil
})
}