-
Notifications
You must be signed in to change notification settings - Fork 0
/
watcher.go
65 lines (59 loc) · 1.52 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
54
55
56
57
58
59
60
61
62
63
64
65
package dlclist
import (
"context"
"github.com/fsnotify/fsnotify"
"github.com/maestre3d/gtavd/config"
"github.com/maestre3d/gtavd/logging"
)
func Watch(ctx context.Context) {
w, err := fsnotify.NewWatcher()
if err != nil {
logging.Error().Err(err).Msg("gtavd: failed to start dlclist-watcher module")
return
}
defer func() {
if err = w.Close(); err != nil {
logging.Error().Err(err).Msg("gtavd-dlclist-watcher: failed to close os watcher")
}
}()
for {
if w != nil {
modsPath := config.DefaultConfig.Addon.Dir
removeWatcher(w, modsPath)
if err = w.Add(modsPath); err != nil {
logging.Error().
Err(err).
Str("path", modsPath).
Msg("gtavd: failed to start dlclist file system watcher")
} else {
go listenFsEvents(w, modsPath)
}
}
select {
case <-config.OnConfigChange:
continue
case <-ctx.Done():
return
}
}
}
func removeWatcher(w *fsnotify.Watcher, path string) {
_ = w.Remove(path)
}
func listenFsEvents(w *fsnotify.Watcher, filePath string) {
writeAddonsFromWatcher()
logging.Info().
Str("path", filePath).
Msg("gtavd-dlclist-watcher: listening to dlclist directory")
for e := range w.Events {
logging.Debug().Str("event", e.String()).Msg("gtavd-dlclist-watcher: received fs event")
writeAddonsFromWatcher()
}
}
func writeAddonsFromWatcher() {
if err := WriteAddons(); err != nil {
logging.Error().Err(err).Msg("gtavd-dlclist-watcher: failed to write addons list")
} else {
logging.Info().Msg("gtavd-dlclist-watcher: dlclist.xml file was written")
}
}