-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
63 lines (51 loc) · 1.35 KB
/
config.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"
"os"
"regexp"
"strings"
"github.com/pelletier/go-toml"
)
// Config r
type Config struct {
Notifs NotifConfigs
}
// NotifConfigs represents a notification config for a postal code, a map of
// postal codes ([\d{4}[A-Z]{2}) to phone numbers (E.164).
type NotifConfigs map[string][]string
var (
postalCodeRegExp = regexp.MustCompile(`^\d{4}[A-Z]{2}`)
phoneNumberRegExp = regexp.MustCompile(`^\+?\d{11}`)
)
// LoadConfig loads a config from a TOML file.
func LoadConfig(path string) (Config, error) {
file, err := os.Open(path)
if err != nil {
return Config{}, fmt.Errorf("cannot open config file: %v", err)
}
var cfg Config
err = toml.NewDecoder(file).Decode(&cfg)
if err != nil {
return Config{}, fmt.Errorf("cannot parse config: %v", err)
}
err = cfg.Validate()
if err != nil {
return Config{}, err
}
return cfg, nil
}
// Validate checks the validity of a config.
func (cfg Config) Validate() error {
for postalCode, phoneNumbers := range cfg.Notifs {
if !postalCodeRegExp.Match([]byte(postalCode)) {
return fmt.Errorf("invalid postal code (%v)", postalCode)
}
for i, phoneNumber := range phoneNumbers {
if !phoneNumberRegExp.Match([]byte(phoneNumber)) {
return fmt.Errorf("invalid phone number (%v)", phoneNumber)
}
phoneNumbers[i] = strings.TrimPrefix(phoneNumber, "+")
}
}
return nil
}