-
Notifications
You must be signed in to change notification settings - Fork 6
/
petze.go
81 lines (69 loc) · 1.79 KB
/
petze.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
package main
import (
"flag"
"fmt"
"github.com/foomo/petze/mail"
"github.com/foomo/petze/sms"
"github.com/foomo/petze/watch"
"os"
"github.com/foomo/petze/config"
"github.com/foomo/petze/service"
"github.com/foomo/petze/slack"
log "github.com/sirupsen/logrus"
)
var flagJsonOutput bool
// Version is set during build via ldflags
var Version string
func main() {
flag.Usage = usage
flag.BoolVar(&flagJsonOutput, "json-output", false, "specifies if the logging format is json or not")
flag.Parse()
initializeLogger()
if len(flag.Args()) == 0 {
log.Fatal("please pass the configuration directory as a first argument")
}
// add version to user agent
watch.SetUserAgentVersion(Version)
fmt.Println("petze", Version, "starting")
configurationDirectory := flag.Args()[0]
if stat, err := os.Stat(configurationDirectory); err == nil && stat.IsDir() {
runServer(configurationDirectory)
} else {
log.Fatal("specified configuration directory does not exist or is not a directory")
}
}
func runServer(configurationDirectory string) {
serverConfig, err := config.LoadServer(configurationDirectory)
if err != nil {
log.Fatal(err)
}
if serverConfig.SMTP != nil {
// init mailer
mail.InitMailer(
serverConfig.SMTP.Server,
serverConfig.SMTP.User,
serverConfig.SMTP.Pass,
serverConfig.SMTP.From,
serverConfig.SMTP.Port,
serverConfig.SMTP.To,
)
}
// init slackbot
if serverConfig.Slack != "" {
slack.InitSlackBot(serverConfig.Slack)
}
// init SMS
if serverConfig.Sms != nil {
sms.InitSMS(serverConfig.Sms)
}
log.Info(service.Run(serverConfig, configurationDirectory))
}
func usage() {
log.Printf("Usage: %s configuration-directory \n", os.Args[0])
flag.PrintDefaults()
}
func initializeLogger() {
if flagJsonOutput {
log.SetFormatter(&log.JSONFormatter{})
}
}