-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
63 lines (53 loc) · 1.42 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
package main
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
flag "github.com/spf13/pflag"
)
var (
configFile string
errorDirectory string
)
func init() {
flag.StringVarP(&configFile, "config", "c", "config.yml", "name of the config file")
flag.StringVarP(&errorDirectory, "error-reports", "e", "", "directory to write the error reports to")
}
func main() {
flag.Parse()
config, err := NewConfig(configFile)
exitOnErr(err)
checker, err := NewChecker(config.Checks)
exitOnErr(err)
results := checker.Run()
for _, result := range results {
fmt.Printf("%d,%s,%t,%v\n", result.Timestamp.UnixNano(), result.Name, result.OK(), result.RTT)
if !result.OK() && errorDirectory != "" {
writeErrorReport(result, errorDirectory)
}
}
}
func writeErrorReport(result CheckResult, path string) error {
os.MkdirAll(path, os.ModePerm)
filename := filepath.Join(path, fmt.Sprintf("%s-%d-error.report", result.Name, result.Timestamp.UnixNano()))
data := []byte(result.String())
return ioutil.WriteFile(filename, data, 0600)
}
// exitOnErr takes an arbitary number of errors and prints those to stderr
// if they are not nil. If any non-nil errors where passed the program will
// be exited.
func exitOnErr(errs ...error) {
errNotNil := false
for _, err := range errs {
if err == nil {
continue
}
errNotNil = true
fmt.Fprintf(os.Stderr, "ERROR: %s", err.Error())
}
if errNotNil {
fmt.Print("\n")
os.Exit(-1)
}
}