-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
89 lines (81 loc) · 1.57 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
80
81
82
83
84
85
86
87
88
89
package main
import (
"fmt"
"log"
"os"
"sync"
"github.com/blainemoser/errorLogging/configurations"
"github.com/blainemoser/errorLogging/digest"
"github.com/blainemoser/errorLogging/files"
"github.com/fsnotify/fsnotify"
)
func main() {
watcher := initialise()
defer watcher.Close()
err := files.List.Walk()
if err != nil {
log.Fatal(err)
return
}
go runFileWatcher(watcher)
for {
runQueueWatcher()
}
}
func runFileWatcher(watcher *fsnotify.Watcher) {
for {
select {
// watch for events
case event := <-watcher.Events:
err := digest.AddToQueue(event)
if err != nil {
log.Println(err)
}
case err := <-watcher.Errors:
if err != nil {
log.Printf("error in watcher: %s", err.Error())
}
}
}
}
func runQueueWatcher() {
errChan := make(chan error, 1)
cond := sync.NewCond(&sync.RWMutex{})
go digest.ProcessQueue(errChan, cond)
cond.L.Lock()
for !digest.Q.Ready {
cond.Wait()
}
cond.L.Unlock()
err := <-errChan
if err != nil {
log.Println(err.Error())
}
}
func initConifgs(args []string) {
err := configurations.Initialize(args)
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
}
func initFileWatcher(newWatcher *fsnotify.Watcher, errChan chan error) {
errChan <- files.Initialize(newWatcher)
}
func initialise() *fsnotify.Watcher {
args := os.Args[1:]
initConifgs(args)
digest.Start()
newWatcher, err := fsnotify.NewWatcher()
if err != nil {
log.Fatal(err)
}
errChan := make(chan error, 1)
initFileWatcher(newWatcher, errChan)
err = <-errChan
close(errChan)
if err != nil {
log.Fatal(err)
}
return newWatcher
}