-
Notifications
You must be signed in to change notification settings - Fork 1
/
watcher.go
53 lines (44 loc) · 1.08 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
package local
import (
"context"
"fmt"
"os"
"github.com/fsnotify/fsnotify"
"github.com/go-kit/log/level"
)
func (l *LocalStore) startup(ctx context.Context) error {
return l.Load()
}
func (l *LocalStore) watching(ctx context.Context) error {
l.watcher, _ = fsnotify.NewWatcher()
l.watcher.Add(l.cfg.Directory)
for {
select {
case event := <-l.watcher.Events:
if event.Op&fsnotify.Create == fsnotify.Create {
file, err := os.Stat(event.Name)
if err != nil {
level.Warn(l.logger).Log("msg", "failed to read file stat", "file", event.Name)
continue
}
if file.IsDir() {
level.Debug(l.logger).Log("msg", "watched directory change", "dir", event.Name)
continue
}
l.LoadTemplate(event.Name)
}
if event.Op&fsnotify.Remove == fsnotify.Remove {
l.UnloadTemplate(event.Name)
}
if event.Op&fsnotify.Rename == fsnotify.Rename {
l.UnloadTemplate(event.Name)
}
case err := <-l.watcher.Errors:
fmt.Println("error : ", err)
return err
}
}
}
func (l *LocalStore) shutdown(_ error) error {
return l.watcher.Close()
}