-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconfig.go
More file actions
77 lines (64 loc) · 2.12 KB
/
config.go
File metadata and controls
77 lines (64 loc) · 2.12 KB
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
package main
import (
"flag"
"fmt"
"slices"
"time"
"github.com/BurntSushi/toml"
)
type Config struct {
LogLevel int `toml:"log_level"`
CronJob string `toml:"cron_job"`
// Deprecated. UseCron is deprecated, if the CronJob field is empty,
// This well known run as CronJob.
UseCron bool `toml:"use_cron"`
// WaitTime is based on second.
WaitTime int `toml:"wait_time"`
// DeleteDurationPeriod will be use for delete cache automatically.
DeleteDurationPeriod time.Duration `toml:"delete_duration_period"`
Clients map[string]Clients `toml:"clients"`
SMTP map[string]SMTP `toml:"smtp"`
}
type SMTP struct {
Mail string `toml:"mail"`
Address string `toml:"host"`
Port string `toml:"port"`
From string `toml:"from"`
Username string `toml:"username"`
Password string `toml:"password"`
AuthMethod smtpAuthMethod `toml:"auth_method"`
Identity string `toml:"identity"`
SkipTLS bool `toml:"skip_tls"`
}
type Clients struct {
SMTP string `toml:"smtp"`
BillID string `toml:"bill_id"`
BillIDs []string `toml:"bill_ids"`
AuthToken string `toml:"auth_token"`
Recipients []string `toml:"recipients"`
}
type smtpAuthMethod string
const (
smtpAuthMethodPlain smtpAuthMethod = "plain"
smtpAuthMethodMD5 smtpAuthMethod = "cram-md5"
smtpAuthMethodCustom smtpAuthMethod = "custom"
)
var smtpAuthMethodValues = []smtpAuthMethod{smtpAuthMethodPlain, smtpAuthMethodMD5, smtpAuthMethodCustom}
func ParseConfig() (*Config, error) {
var configFilePath string
flag.StringVar(&configFilePath, "file", "config.toml", "config file(toml formatted)")
flag.Parse()
config := new(Config)
if _, err := toml.DecodeFile(configFilePath, config); err != nil {
return nil, err
}
for _, smtp := range config.SMTP {
if !slices.Contains(smtpAuthMethodValues, smtp.AuthMethod) {
return nil, fmt.Errorf("invalid smtp auth, should be exactly one of %v", smtpAuthMethodValues)
}
}
if config.DeleteDurationPeriod == 0 {
config.DeleteDurationPeriod = time.Hour * 24 * 7
}
return config, nil
}