-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.go
63 lines (56 loc) · 1.87 KB
/
file.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
package watcher
import (
"path/filepath"
"github.com/fsnotify/fsnotify"
"github.com/xiaoyang-chen/zapx/log"
)
func WatchFileChange(filePath string, run func(in fsnotify.Event)) {
go func() {
var watcher, err = fsnotify.NewWatcher()
if err != nil {
log.Error("fsnotify.NewWatcher err", log.Error2Field(err))
return
}
defer watcher.Close()
var file = filepath.Clean(filePath)
var fileDir, _ = filepath.Split(file)
if err = watcher.Add(fileDir); err != nil {
log.Error("watcher.Add(fileDir) err", log.String("fileDir", fileDir), log.Error2Field(err))
return
}
var realConfigFile, _ = filepath.EvalSymlinks(filePath)
var currentConfigFile string
var event fsnotify.Event
var isOpen bool
const writeOrCreateMask = fsnotify.Write | fsnotify.Create
for {
select {
case event, isOpen = <-watcher.Events:
if !isOpen { // 'Events' channel is closed
log.Error("watch file change end, event, isOpen = <-watcher.Events, this channel is closed")
return
}
currentConfigFile, _ = filepath.EvalSymlinks(filePath)
// we only care about the file with the following cases:
// 1 - if the file was modified or created
// 2 - if the real path to the file changed (eg: k8s ConfigMap replacement)
if (filepath.Clean(event.Name) == file &&
event.Op&writeOrCreateMask != 0) ||
(currentConfigFile != "" &&
currentConfigFile != realConfigFile) {
realConfigFile = currentConfigFile
run(event)
} else if filepath.Clean(event.Name) == file &&
event.Op&fsnotify.Remove != 0 {
log.Error("watch file change end, file is removed!!!")
return
} /*else {
log.Error("file change but unknown change type")
}*/
case err, isOpen = <-watcher.Errors:
log.Error("watch file change end, err, isOpen = <-watcher.Errors", log.NamedError("err", err), log.Bool("isOpen", isOpen))
return
}
}
}()
}