Skip to content

Commit 7d691d4

Browse files
feat: cli args
1 parent 1a8fd46 commit 7d691d4

File tree

4 files changed

+99
-22
lines changed

4 files changed

+99
-22
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ smtpbridge
2424
### Show Version
2525

2626
```
27-
smtpbridge version
27+
smtpbridge --version
2828
```
2929

3030
## Supported Endpoints

config/config.go

+61-14
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,16 @@ import (
1212
"github.com/alecthomas/kong"
1313
kongyaml "github.com/alecthomas/kong-yaml"
1414
"github.com/labstack/gommon/bytes"
15+
"github.com/rs/zerolog/log"
1516
)
1617

1718
type Config struct {
1819
DatabasePath string
1920
AttachmentsDirectory string
21+
HTTPDisable bool
2022
HTTPAddress string
2123
HTTPBodyLimit int
24+
SMTPDisable bool
2225
SMTPAddress string
2326
SMTPMaxMessageBytes int
2427
Endpoints []endpoints.Endpoint
@@ -127,8 +130,10 @@ func Parse(raw Raw) (Config, error) {
127130
return Config{
128131
DatabasePath: databasePath,
129132
AttachmentsDirectory: attachmentsDirectory,
133+
HTTPDisable: raw.HTTP.Disable,
130134
HTTPAddress: raw.HTTP.Host + ":" + strconv.Itoa(raw.HTTP.Port),
131135
HTTPBodyLimit: int(maxBytesForEachPayload),
136+
SMTPDisable: raw.SMTP.Disable,
132137
SMTPAddress: raw.SMTP.Host + ":" + strconv.Itoa(raw.SMTP.Port),
133138
SMTPMaxMessageBytes: int(maxBytesForEachPayload),
134139
Endpoints: ends,
@@ -176,16 +181,33 @@ type RawRule struct {
176181
}
177182

178183
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+
179209
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...))
189211
if err != nil {
190212
return Raw{}, err
191213
}
@@ -206,19 +228,44 @@ func Read(cli CLI) (Raw, error) {
206228
raw.DataDirectory = cli.DataDirectory
207229
}
208230

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+
209251
return raw, nil
210252
}
211253

212254
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."`
216264
}
217265

218266
func ReadAndParseCLI() CLI {
219267
cli := CLI{}
220-
ctx := kong.Parse(&cli)
221-
cli.Command = ctx.Command()
268+
kong.Parse(&cli)
222269

223270
return cli
224271
}

config/resolve.go

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package config
2+
3+
import (
4+
"os"
5+
6+
"github.com/alecthomas/kong"
7+
)
8+
9+
func resolve(paths []string) (string, error) {
10+
for _, path := range paths {
11+
f, err := os.Open(kong.ExpandPath(path))
12+
if err != nil {
13+
if os.IsNotExist(err) || os.IsPermission(err) {
14+
continue
15+
}
16+
17+
return "", err
18+
}
19+
f.Close()
20+
21+
return path, nil
22+
}
23+
24+
return "", nil
25+
}

main.go

+12-7
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import (
2121
func main() {
2222
cli := config.ReadAndParseCLI()
2323

24-
if cli.Command == "version" {
24+
if cli.Version {
2525
fmt.Println(version)
2626
return
2727
}
@@ -65,17 +65,22 @@ func run(ctx context.Context, shutdown context.CancelFunc, cli config.CLI) <-cha
6565
procs.GardenerStart(ctx, app, cfg.RetentionPolicy)
6666
procs.VacuumStart(ctx, app)
6767

68+
var backgrounds []background.Background
69+
6870
// SMTP
69-
smtp := smtp.New(app, shutdown, cfg.SMTPAddress, cfg.SMTPMaxMessageBytes)
71+
if !cfg.SMTPDisable {
72+
smtp := smtp.New(app, shutdown, cfg.SMTPAddress, cfg.SMTPMaxMessageBytes)
73+
backgrounds = append(backgrounds, smtp)
74+
}
7075

7176
// HTTP
72-
http := http.New(app, shutdown, cfg.HTTPAddress, cfg.HTTPBodyLimit, cfg.RetentionPolicy)
77+
if !cfg.HTTPDisable {
78+
http := http.New(app, shutdown, cfg.HTTPAddress, cfg.HTTPBodyLimit, cfg.RetentionPolicy)
79+
backgrounds = append(backgrounds, http)
80+
}
7381

7482
// Start
75-
return background.Run(ctx,
76-
smtp,
77-
http,
78-
)
83+
return background.Run(ctx, backgrounds...)
7984
}
8085

8186
var (

0 commit comments

Comments
 (0)