@@ -12,13 +12,16 @@ import (
12
12
"github.com/alecthomas/kong"
13
13
kongyaml "github.com/alecthomas/kong-yaml"
14
14
"github.com/labstack/gommon/bytes"
15
+ "github.com/rs/zerolog/log"
15
16
)
16
17
17
18
type Config struct {
18
19
DatabasePath string
19
20
AttachmentsDirectory string
21
+ HTTPDisable bool
20
22
HTTPAddress string
21
23
HTTPBodyLimit int
24
+ SMTPDisable bool
22
25
SMTPAddress string
23
26
SMTPMaxMessageBytes int
24
27
Endpoints []endpoints.Endpoint
@@ -127,8 +130,10 @@ func Parse(raw Raw) (Config, error) {
127
130
return Config {
128
131
DatabasePath : databasePath ,
129
132
AttachmentsDirectory : attachmentsDirectory ,
133
+ HTTPDisable : raw .HTTP .Disable ,
130
134
HTTPAddress : raw .HTTP .Host + ":" + strconv .Itoa (raw .HTTP .Port ),
131
135
HTTPBodyLimit : int (maxBytesForEachPayload ),
136
+ SMTPDisable : raw .SMTP .Disable ,
132
137
SMTPAddress : raw .SMTP .Host + ":" + strconv .Itoa (raw .SMTP .Port ),
133
138
SMTPMaxMessageBytes : int (maxBytesForEachPayload ),
134
139
Endpoints : ends ,
@@ -176,16 +181,33 @@ type RawRule struct {
176
181
}
177
182
178
183
func Read (cli CLI ) (Raw , error ) {
184
+ var configFiles []string
185
+ if cli .Config == nil {
186
+ configFile , err := resolve ([]string {
187
+ "config.yaml" ,
188
+ "config.yml" ,
189
+ ".smtpbridge.yaml" ,
190
+ ".smtpbridge.yml" ,
191
+ "~/.smtpbridge.yaml" ,
192
+ "~/.smtpbridge.yml" ,
193
+ })
194
+ if err != nil {
195
+ return Raw {}, err
196
+ }
197
+
198
+ if configFile != "" {
199
+ configFiles = append (configFiles , configFile )
200
+ }
201
+ } else if * cli .Config != "" {
202
+ configFiles = []string {* cli .Config }
203
+ }
204
+
205
+ if len (configFiles ) != 0 {
206
+ log .Info ().Str ("path" , configFiles [0 ]).Msg ("Reading config file" )
207
+ }
208
+
179
209
var raw Raw
180
- parser , err := kong .New (& raw , kong .Configuration (
181
- kongyaml .Loader ,
182
- "config.yaml" ,
183
- "config.yml" ,
184
- ".smtpbridge.yaml" ,
185
- ".smtpbridge.yml" ,
186
- "~/.smtpbridge.yaml" ,
187
- "~/.smtpbridge.yml" ,
188
- ))
210
+ parser , err := kong .New (& raw , kong .Configuration (kongyaml .Loader , configFiles ... ))
189
211
if err != nil {
190
212
return Raw {}, err
191
213
}
@@ -206,19 +228,44 @@ func Read(cli CLI) (Raw, error) {
206
228
raw .DataDirectory = cli .DataDirectory
207
229
}
208
230
231
+ if cli .SMTPDisable != nil {
232
+ raw .SMTP .Disable = bool (* cli .SMTPDisable )
233
+ }
234
+ if cli .SMTPHost != nil {
235
+ raw .SMTP .Host = string (* cli .SMTPHost )
236
+ }
237
+ if cli .SMTPPort != nil {
238
+ raw .SMTP .Port = int (* cli .SMTPPort )
239
+ }
240
+
241
+ if cli .HTTPDisable != nil {
242
+ raw .HTTP .Disable = bool (* cli .HTTPDisable )
243
+ }
244
+ if cli .HTTPHost != nil {
245
+ raw .HTTP .Host = string (* cli .HTTPHost )
246
+ }
247
+ if cli .HTTPPort != nil {
248
+ raw .HTTP .Port = int (* cli .HTTPPort )
249
+ }
250
+
209
251
return raw , nil
210
252
}
211
253
212
254
type CLI struct {
213
- Command string `kong:"-"`
214
- DataDirectory string `name:"data-directory" help:"Path to store data." type:"path" optional:""`
215
- Version struct {} `cmd:"" hidden:""` // HACK: hidden because kong will throw error if a command is not supplied
255
+ Config * string `name:"config" help:"Path to config file." type:"string"`
256
+ DataDirectory string `name:"data-directory" help:"Path to store data." type:"path"`
257
+ SMTPDisable * bool `name:"smtp-disable" help:"Disable SMTP server."`
258
+ SMTPHost * string `name:"smtp-host" help:"SMTP host address to listen on."`
259
+ SMTPPort * uint16 `name:"smtp-port" help:"SMTP port to listen on"`
260
+ HTTPDisable * bool `name:"http-disable" help:"Disable HTTP server."`
261
+ HTTPHost * string `name:"http-host" help:"HTTP host address to listen on."`
262
+ HTTPPort * uint16 `name:"http-port" help:"HTTP port to listen on"`
263
+ Version bool `name:"version" help:"Show version."`
216
264
}
217
265
218
266
func ReadAndParseCLI () CLI {
219
267
cli := CLI {}
220
- ctx := kong .Parse (& cli )
221
- cli .Command = ctx .Command ()
268
+ kong .Parse (& cli )
222
269
223
270
return cli
224
271
}
0 commit comments