-
Notifications
You must be signed in to change notification settings - Fork 0
/
Watch.go
165 lines (144 loc) · 3.73 KB
/
Watch.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
package main
import (
"errors"
"fmt"
"ipsd_vsc/Utils"
"os"
"path/filepath"
"time"
"github.com/fsnotify/fsnotify"
)
type Watch struct {
watch *fsnotify.Watcher
}
func FindInList(str string, strList []string) bool {
if str == "" {
return false
}
if strList == nil {
return false
}
if len(strList) == 0 {
return false
}
for _, s := range strList {
if s == str {
return true
}
}
return false
}
//监控目录
func (w *Watch) watchDir(dir string) {
//通过Walk来遍历目录下的所有子目录
filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
//这里判断是否为目录,只需监控目录即可
//目录下的文件也在监控范围内,不需要我们一个一个加
if info.IsDir() {
path, err := filepath.Abs(path)
if err != nil {
return err
}
err = w.watch.Add(path)
if err != nil {
return err
}
}
return nil
})
go func() {
var updateFileList []string
for {
select {
case ev := <-w.watch.Events:
{
if ev.Op&fsnotify.Create == fsnotify.Create {
fmt.Println("Copy/Generate ", ev.Name)
//这里获取新创建文件的信息,如果是目录,则加入监控中
if FindInList(ev.Name, updateFileList) == false {
updateFileList = append(updateFileList, ev.Name)
}
fi, err := os.Stat(ev.Name)
if err == nil && fi.IsDir() {
w.watch.Add(ev.Name)
}
}
if ev.Op&fsnotify.Write == fsnotify.Write {
if FindInList(ev.Name, updateFileList) == false {
fmt.Println("Updating ", ev.Name)
updateFileList = append(updateFileList, ev.Name)
}
}
if ev.Op&fsnotify.Remove == fsnotify.Remove {
fmt.Println("Deleting ", ev.Name)
}
}
case err := <-w.watch.Errors:
{
fmt.Println("error : ", err)
return
}
}
}
}()
}
func (w *Watch) removeWatch(dir string) {
//通过Walk来遍历目录下的所有子目录
filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
//这里判断是否为目录,只需监控目录即可
//目录下的文件也在监控范围内,不需要我们一个一个加
if info.IsDir() {
path, err := filepath.Abs(path)
if err != nil {
return err
}
err = w.watch.Remove(path)
if err != nil {
return err
}
}
return nil
})
}
func GetOutputFolderPath(siteFolderPath string) (string, error) {
outputFolderPath := filepath.Join(siteFolderPath, "Output")
if Utils.PathIsExist(outputFolderPath) == false {
return "", errors.New("RunMonitor.MonitorCompile.GetOutputFolderPath: output folder path not exist " + outputFolderPath)
}
return filepath.EvalSymlinks(outputFolderPath)
}
func MonitorCompile(siteFolderPath string) *Watch {
outputDir, errGetOutputFolderPath := GetOutputFolderPath(siteFolderPath)
if errGetOutputFolderPath != nil {
var errMsg = "RunMonitor: Cannot find output folder " + outputDir
Utils.Logger.Println(errMsg)
return nil
}
if Utils.PathIsExist(outputDir) == false {
var errMsg = "RunMonitor: Output Folder not exist " + outputDir
Utils.Logger.Println(errMsg)
return nil
}
watch, _ := fsnotify.NewWatcher()
w := Watch{
watch: watch,
}
w.watchDir(outputDir)
fmt.Println(time.Now().Format("2006-01-02 15:04:05"))
return &w
}
func StopMonitor(w *Watch, siteFolderPath string) {
outputDir, errGetOutputFolderPath := GetOutputFolderPath(siteFolderPath)
if errGetOutputFolderPath != nil {
var errMsg = "RunMonitor: Cannot find output folder " + outputDir
Utils.Logger.Println(errMsg)
return
}
if Utils.PathIsExist(outputDir) == false {
var errMsg = "RunMonitor: Output Folder not exist " + outputDir
Utils.Logger.Println(errMsg)
return
}
w.removeWatch(outputDir)
fmt.Println(time.Now().Format("2006-01-02 15:04:05"))
}