/
main.go
107 lines (93 loc) · 2.11 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
package main
import (
"flag"
"fmt"
"os"
"os/signal"
"github.com/gophergala2016/Pulse/LogPulse/api"
"github.com/gophergala2016/Pulse/LogPulse/config"
"github.com/gophergala2016/Pulse/LogPulse/email"
"github.com/gophergala2016/Pulse/LogPulse/file"
"github.com/gophergala2016/Pulse/pulse"
)
var (
runAPI bool
outputFile string
buffStrings []string
logList []string
)
func init() {
flag.BoolVar(&runAPI, "api", false, "Turn on API mode")
flag.Parse()
defer func() {
if r := recover(); r != nil {
fmt.Println(r)
os.Exit(0)
}
}()
cfg, err := config.Load()
if err != nil {
panic(fmt.Errorf("main.init: Could not load the config.\n %v", err))
}
logList = cfg.LogList
outputFile = cfg.OutputFile
}
func main() {
defer func() {
if r := recover(); r != nil {
fmt.Println(r)
os.Exit(0)
}
}()
if len(flag.Args()) == 0 && !runAPI {
if len(logList) == 0 {
panic(fmt.Errorf("main.main: Must supply a list of log files in the config"))
}
startPulse(logList)
} else if runAPI {
startAPI()
} else {
startPulse(flag.Args())
}
}
func startAPI() {
api.Start()
}
func startPulse(filenames []string) {
checkList(filenames)
stdIn := make(chan string)
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
// On keyboard interrup cleanup the program
go func() {
for _ = range c {
fmt.Println("Exiting for Keyboard Interupt")
os.Exit(0)
}
}()
pulse.Run(stdIn, email.Send)
for _, filename := range filenames {
line := make(chan string)
file.Read(filename, line)
for l := range line {
stdIn <- l
}
}
close(stdIn)
}
func checkList(filenames []string) {
for i, filename := range filenames {
if _, err := os.Stat(filename); os.IsNotExist(err) {
panic(fmt.Errorf("main.checkList: %s", err))
}
if len(filename) > 3 && filename[len(filename)-3:len(filename)] == ".gz" {
if err := file.UnGZip(filename); err != nil {
panic(fmt.Errorf("main.checkList: %s", err))
}
if _, err := os.Stat(filename[:len(filename)-3]); os.IsNotExist(err) {
panic(fmt.Errorf("main.checkList: %s", err))
}
filenames[i] = filename[:len(filename)-3]
}
}
}