-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
158 lines (127 loc) · 3.29 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package cmd
import (
"errors"
"io"
"io/fs"
"os"
"path"
"path/filepath"
"github.com/BurntSushi/toml"
"github.com/adrg/xdg"
"github.com/spf13/pflag"
)
type CredentialConfig struct {
SmtpPass string `json:"smtp_pass"`
ApiKey string `json:"api_key"`
}
type Config struct {
Server *ServerConfig `toml:"server"`
Database *DatabaseConfig `toml:"database"`
Logging *LoggingConfig `toml:"logging"`
}
type ServerConfig struct {
Smtp *SmtpConfig `toml:"smtp"`
Http *HttpConfig `toml:"http"`
}
type SmtpConfig struct {
Listen string `toml:"listen"`
MaxMsgBytes int `toml:"max_message_bytes"`
KeyFile string `toml:"key_file"`
CertFile string `toml:"cert_file"`
}
type HttpConfig struct {
Listen string `toml:"listen"`
KeyFile string `toml:"key_file"`
CertFile string `toml:"cert_file"`
}
type DatabaseConfig struct {
Path string `toml:"path"`
}
type LoggingConfig struct {
Filename string `toml:"filename"`
MaxSize int `toml:"max_size"`
MaxBackups int `toml:"max_backups"`
MaxAge int `toml:"max_age"`
}
func getDefaultPath(inputPath, basePath, file string) string {
if inputPath == "" {
return ""
}
if filepath.IsAbs(inputPath) {
return inputPath
}
return path.Join(basePath, inputPath)
}
func readConfig(flags *pflag.FlagSet) (*Config, error) {
cfgFromArgs := true
cfgFile, err := flags.GetString("config")
if err != nil || cfgFile == "" {
cfgFromArgs = false
defaultCfgFile, err := xdg.ConfigFile("postbox/config.toml")
if err != nil {
return nil, err
} else {
cfgFile = defaultCfgFile
}
}
var cfg Config
f, err := os.Open(cfgFile)
if err == nil {
defer f.Close()
data, err := io.ReadAll(f)
if err != nil {
return nil, err
}
if toml.Unmarshal(data, &cfg) != nil {
return nil, err
}
} else if cfgFromArgs || !errors.Is(err, fs.ErrNotExist) {
return nil, err
}
if cfg.Server == nil {
cfg.Server = &ServerConfig{}
}
if cfg.Server.Smtp == nil {
cfg.Server.Smtp = &SmtpConfig{}
}
if cfg.Server.Smtp.Listen == "" {
cfg.Server.Smtp.Listen = ":8025"
}
dir := filepath.Dir(cfgFile)
cfg.Server.Smtp.KeyFile = getDefaultPath(cfg.Server.Smtp.KeyFile, dir, "key.pem")
cfg.Server.Smtp.CertFile = getDefaultPath(cfg.Server.Smtp.CertFile, dir, "cert.pem")
if cfg.Server.Smtp.MaxMsgBytes == 0 {
cfg.Server.Smtp.MaxMsgBytes = 1024 * 1024 * 10
} else if cfg.Server.Smtp.MaxMsgBytes < 1024 {
return nil, errors.New("server.smtp.max_message_bytes must be >= 1024")
}
if cfg.Server.Http == nil {
cfg.Server.Http = &HttpConfig{}
}
if cfg.Server.Http.Listen == "" {
cfg.Server.Http.Listen = ":8080"
}
cfg.Server.Http.KeyFile = getDefaultPath(cfg.Server.Http.KeyFile, dir, "key.pem")
cfg.Server.Http.CertFile = getDefaultPath(cfg.Server.Http.CertFile, dir, "cert.pem")
if cfg.Database == nil {
cfg.Database = &DatabaseConfig{}
}
if cfg.Database.Path == "" {
if defaultStoragePath, err := xdg.DataFile("postbox"); err != nil {
return nil, err
} else {
cfg.Database.Path = defaultStoragePath
}
}
if cfg.Logging == nil {
cfg.Logging = &LoggingConfig{}
}
if cfg.Logging.Filename != "" && !filepath.IsAbs(cfg.Logging.Filename) {
logPath, err := xdg.DataFile(path.Join("postbox", cfg.Logging.Filename))
if err != nil {
return nil, err
}
cfg.Logging.Filename = logPath
}
return &cfg, nil
}