-
Notifications
You must be signed in to change notification settings - Fork 0
/
listen.go
43 lines (39 loc) · 798 Bytes
/
listen.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
package main
import (
"log/slog"
"os"
"path/filepath"
"github.com/fsnotify/fsnotify"
)
// listen listens to filesystem events and triggers actions/commands
func (w *watcher) listen(workDir string) {
start:
select {
case event, ok := <-w.watcher.Events:
if !ok {
panic("watch error")
}
if w.verbose {
slog.Info("FS Event.", "event", event)
}
eventName, _ := filepath.Rel(workDir, event.Name)
if w.match(event.Op, eventName) {
w.runcmd <- eventName
}
if event.Has(fsnotify.Create) {
stat, err := os.Stat(event.Name)
if err != nil {
goto start
}
if stat.IsDir() {
w.actions <- action{actionAdd, event.Name}
}
}
case err, ok := <-w.watcher.Errors:
if !ok {
panic("watch error")
}
slog.Error("Error.", "error", err)
}
goto start
}