-
Notifications
You must be signed in to change notification settings - Fork 0
/
observer.go
167 lines (137 loc) · 3.08 KB
/
observer.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
package observer
import (
"fmt"
"log/slog"
"os"
"slices"
"strings"
"time"
"github.com/fsnotify/fsnotify"
"github.com/maranix/monitor/fsutil"
"github.com/maranix/monitor/runner"
)
type Observer struct {
watcher *fsnotify.Watcher
observable string
command string
}
func Create(p string, c string) (*Observer, error) {
path, err := fsutil.AbsPath(p)
if err != nil {
return nil, err
}
watcher, err := fsnotify.NewWatcher()
if err != nil {
return nil, err
}
obs := &Observer{
watcher: watcher,
observable: path,
command: c,
}
return obs, nil
}
func (obs *Observer) Observe() {
slog.Info("Starting observer")
dir, err := fsutil.IsDirectory(obs.observable)
if err != nil {
slog.Error(err.Error())
os.Exit(1)
}
if dir {
obs.watchDirEvents()
} else {
files := []string{obs.observable}
obs.watchFileEvents(files)
}
}
func (obs *Observer) add(p string) {
err := obs.watcher.Add(p)
if err != nil {
slog.Error(fmt.Sprintf("%q: %s", p, err))
os.Exit(1)
}
}
func (obs *Observer) watchDirEvents() {
subdirs, err := fsutil.GetSubDirPaths(obs.observable)
if err != nil {
slog.Error(err.Error())
os.Exit(1)
}
for _, dir := range subdirs {
if !strings.Contains(dir, "/.") {
obs.add(dir)
}
}
slog.Info(fmt.Sprintf("Watching %d dirs", len(obs.watcher.WatchList())))
slog.Info(fmt.Sprintf("WatchList %v", obs.watcher.WatchList()))
defer obs.watcher.Close()
// Start listening for events.
go func() {
var timer *time.Timer
for {
select {
case event, ok := <-obs.watcher.Events:
if timer != nil {
timer.Stop()
}
if !ok || event.Op == fsnotify.Chmod {
continue
}
timer = time.AfterFunc(time.Second*1, func() {
slog.Info(fmt.Sprintf("event: %q, modified :%q", event, event.Name))
slog.Info(fmt.Sprintf("executing command %s", obs.command))
runner.Run(obs.command)
})
case err, ok := <-obs.watcher.Errors:
if !ok {
slog.Error("error:", err)
return
}
}
}
}()
<-make(chan struct{})
}
func (obs *Observer) watchFileEvents(files []string) {
filemap := make(map[string]string)
dirs := make([]string, 0)
for _, f := range files {
name := fsutil.GetFileFromPath(f)
dir := fsutil.GetParentDir(f)
filemap[name] = f
if !slices.Contains(dirs, dir) {
dirs = append(dirs, dir)
obs.add(dir)
}
}
slog.Info(fmt.Sprintf("Watching %d files", len(obs.watcher.WatchList())))
slog.Info(fmt.Sprintf("WatchList %v", obs.watcher.WatchList()))
defer obs.watcher.Close()
// Start listening for events.
go func() {
var timer *time.Timer
for {
select {
case event, ok := <-obs.watcher.Events:
if timer != nil {
timer.Stop()
}
if !ok || event.Op == fsnotify.Chmod {
continue
}
timer = time.AfterFunc(time.Second*1, func() {
slog.Info(fmt.Sprintf("event: %q, modified :%q", event, event.Name))
slog.Info(fmt.Sprintf("executing command %s", obs.command))
runner.Run(obs.command)
})
case err, ok := <-obs.watcher.Errors:
if !ok {
slog.Error("error:", err)
return
}
}
}
}()
<-make(chan struct{})
}