/
parser.go
85 lines (78 loc) · 2.05 KB
/
parser.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
package main
import (
"fmt"
"github.com/vaughan0/go-ini"
)
type Config struct {
relay SMTPRelay
apiHost, contactsFile, masterCA, apiCert, apiKey string
}
type Parser struct {
}
func (p *Parser) Parse(path string) ([]Contact, error) {
file, err := ini.LoadFile(path)
if err != nil {
return nil, err
}
var contacts []Contact
for name, section := range file {
for key, value := range section {
switch key {
case "email":
var newContact EmailContact
newContact.Address = value
newContact.Name = name
contacts = append(contacts, newContact)
default:
log.Infof("Ignoring unknown notifier `%s`", key)
}
}
}
return contacts, err
}
func (p *Parser) ParseConfig(path string) (config Config, err error) {
file, err := ini.LoadFile(path)
if err != nil {
return
}
// Notifier section
notifier, ok := file["notifier"]
if !ok {
err = fmt.Errorf("Missing 'notifier' section in config file")
return
}
config.apiHost, ok = notifier["api_host"]
if !ok {
err = fmt.Errorf("Missing 'api_host' veriable in 'notifier' section in config file")
return
}
config.apiKey, _ = notifier["api_key"]
config.apiCert, _ = notifier["api_cert"]
if tmp := config.apiCert + config.apiKey; tmp != "" && (config.apiCert == "" || config.apiKey == "") {
err = fmt.Errorf("Both, `api_key` and `api_cert` have to be defined in the 'notifier' section in config file")
return
}
config.masterCA, ok = notifier["master_ca"]
config.contactsFile, ok = notifier["contacts"]
if !ok {
err = fmt.Errorf("Missing 'contacts' veriable in 'notifier' section in config file")
return
}
// SMTP section
smtp, ok := file["smtp"]
if !ok {
err = fmt.Errorf("Missing 'smtp' section in config file")
return
}
config.relay.MailFrom, ok = smtp["mail_from"]
if !ok {
err = fmt.Errorf("Missing 'mail_from' veriable in 'smtp' section in config file")
return
}
config.relay.Hostname, ok = smtp["relay_host"]
if !ok {
err = fmt.Errorf("Missing 'relay_host' veriable in 'smtp' section in config file")
return
}
return
}