This repository has been archived by the owner on Feb 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
79 lines (59 loc) · 1.46 KB
/
main.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
package main
import (
"github.com/fsnotify/fsnotify"
"github.com/sirupsen/logrus"
"panicroom/alerter"
"panicroom/watcher"
"time"
)
func main() {
config, err := readConfig()
if err != nil {
logrus.Fatal("Unable to load config:", err)
}
alerters := make(map[string]alerter.Alerter)
for _, a := range config.Alerters {
al, err := alerter.New(a.Type, a.Configuration)
if err != nil {
logrus.Fatal("Unable to initialize alerter", a.Name, ":", err)
}
alerters[a.Name] = al
}
nFiles := uint64(0)
for _, wc := range config.Watchers {
w, err := watcher.New(wc.Paths, wc.Excludes)
if err != nil {
logrus.Fatal("Unable to set up watcher", wc.Name, ":", err)
}
nFiles += uint64(len(w.FilePaths))
// TODO: Refactor this to fan-out watcher events chan into Alerter events chans.
go func() {
for {
e := <-w.Watcher.Events
if e.Op == fsnotify.Create {
nFiles++
}
event := alerter.Event{
WatcherName: wc.Name,
Path: e.Name,
Operation: e.Op,
T: time.Now(),
}
for _, an := range wc.Alerters {
al := alerters[an]
if al == nil {
logrus.Errorln("Alerter ", an, " does not exist. Cannot alert")
continue
}
if err = al.Alert(event); err != nil {
logrus.WithError(err).Errorln("Failed to push alert to", an, ":", err)
}
}
}
}()
}
logrus.Infoln("Started watching ", nFiles, "files for changes.")
for {
time.Sleep(time.Second)
}
}