-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
61 lines (48 loc) · 1.33 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
package main
import (
"fmt"
"io/ioutil"
"strings"
"github.com/pelletier/go-toml"
)
type config struct {
Mail mailConfig `toml:"mail"`
Drive watchConfig `toml:"drive"`
}
type mailConfig struct {
// Human-readable name of the email sernder.
Sender string `toml:"sender"`
// Basic authentication
Username string `toml:"username"`
Password string `toml:"password"`
// Mail Server
Server string `toml:"server"`
Port uint `toml:"port"`
// Who should receive the email.
RecepientList []string `toml:"recepient_list"`
// Language of the email (this does not affect loggine).
Language string `toml:"lang"`
}
type watchConfig struct {
Path string `toml:"path"`
LimitBytes uint64 `toml:"limit_bytes"`
}
func configFromFile(configFile string) (*config, error) {
cfgData, err := ioutil.ReadFile(configFile)
if err != nil {
return nil, fmt.Errorf("cannot read config file %q: %v", configFile, err)
}
cfg := config{}
if err := toml.Unmarshal(cfgData, &cfg); err != nil {
return nil, fmt.Errorf("cannot unmarshal config file %q: %v", configFile, err)
}
return &cfg, nil
}
func (c *config) validate(acceptableLanguages []string) error {
for _, v := range acceptableLanguages {
if c.Mail.Language == v {
return nil
}
}
return fmt.Errorf("lang must be one of: (%s)", strings.Join(acceptableLanguages, ", "))
}