-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
112 lines (91 loc) · 2.12 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package main
import (
"fmt"
"log"
"os"
"path"
"encoding/json"
"io/ioutil"
"golang.org/x/exp/inotify"
)
type Config struct {
ReplaysFolder string
MaxQueueSize int
DebugMode bool
DeleteOldFiles bool
MaxTries int
RequestUrl string
}
type FilePending struct {
Path string
Name string
}
type FileFinished struct {
Path string
Name string
Stats *MatchStats
}
func main() {
if len(os.Args) < 2 {
log.Fatal("Expected a .json config file as argument")
}
conf := readConfig(os.Args[1])
dirName := conf.ReplaysFolder
InitDebugMode(conf.DebugMode)
DebugPrint("watching directory %s\n", dirName)
// set up queue and start the parsing in a goroutine
// I think this is thread-safe
parseQueue := make(chan *FilePending, conf.MaxQueueSize)
finishedQueue := make(chan *FileFinished, conf.MaxQueueSize)
go Parse(parseQueue, finishedQueue)
go ReportAndClean(finishedQueue, conf)
// check the directory for any existing .dem files
dirEntries, err := ioutil.ReadDir(dirName)
if err != nil {
log.Fatal(err)
}
for _, fileInfo := range dirEntries {
handleFile(path.Join(dirName, fileInfo.Name()), parseQueue)
}
// watch the directory for file writes, which should only happen
// when a new file is created
watcher := inotifySetup(dirName)
for {
select {
case ev := <-watcher.Event:
if ev.Mask&inotify.IN_CLOSE_WRITE > 0 {
handleFile(ev.Name, parseQueue)
}
case err := <-watcher.Error:
log.Println("inotify error:", err)
}
}
}
func readConfig(fname string) *Config {
file, _ := os.Open(fname)
decoder := json.NewDecoder(file)
configuration := Config{}
err := decoder.Decode(&configuration)
if err != nil {
fmt.Println("error:", err)
}
return &configuration
}
func inotifySetup(dir string) *inotify.Watcher {
watcher, err := inotify.NewWatcher()
if err != nil {
log.Fatal(err)
}
err = watcher.Watch(dir)
if err != nil {
log.Fatal(err)
}
return watcher
}
func handleFile(fname string, parseQueue chan<- *FilePending) {
basename := path.Base(fname)
ext := path.Ext(fname)
if ext == ".dem" {
parseQueue <- &FilePending{Path: path.Clean(fname), Name: basename}
}
}