Skip to content

Commit

Permalink
feat: cli args
Browse files Browse the repository at this point in the history
  • Loading branch information
ItsNotGoodName committed Jul 21, 2023
1 parent 1a8fd46 commit 7d691d4
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 22 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ smtpbridge
### Show Version

```
smtpbridge version
smtpbridge --version
```

## Supported Endpoints
Expand Down
75 changes: 61 additions & 14 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@ import (
"github.com/alecthomas/kong"
kongyaml "github.com/alecthomas/kong-yaml"
"github.com/labstack/gommon/bytes"
"github.com/rs/zerolog/log"
)

type Config struct {
DatabasePath string
AttachmentsDirectory string
HTTPDisable bool
HTTPAddress string
HTTPBodyLimit int
SMTPDisable bool
SMTPAddress string
SMTPMaxMessageBytes int
Endpoints []endpoints.Endpoint
Expand Down Expand Up @@ -127,8 +130,10 @@ func Parse(raw Raw) (Config, error) {
return Config{
DatabasePath: databasePath,
AttachmentsDirectory: attachmentsDirectory,
HTTPDisable: raw.HTTP.Disable,
HTTPAddress: raw.HTTP.Host + ":" + strconv.Itoa(raw.HTTP.Port),
HTTPBodyLimit: int(maxBytesForEachPayload),
SMTPDisable: raw.SMTP.Disable,
SMTPAddress: raw.SMTP.Host + ":" + strconv.Itoa(raw.SMTP.Port),
SMTPMaxMessageBytes: int(maxBytesForEachPayload),
Endpoints: ends,
Expand Down Expand Up @@ -176,16 +181,33 @@ type RawRule struct {
}

func Read(cli CLI) (Raw, error) {
var configFiles []string
if cli.Config == nil {
configFile, err := resolve([]string{
"config.yaml",
"config.yml",
".smtpbridge.yaml",
".smtpbridge.yml",
"~/.smtpbridge.yaml",
"~/.smtpbridge.yml",
})
if err != nil {
return Raw{}, err
}

if configFile != "" {
configFiles = append(configFiles, configFile)
}
} else if *cli.Config != "" {
configFiles = []string{*cli.Config}
}

if len(configFiles) != 0 {
log.Info().Str("path", configFiles[0]).Msg("Reading config file")
}

var raw Raw
parser, err := kong.New(&raw, kong.Configuration(
kongyaml.Loader,
"config.yaml",
"config.yml",
".smtpbridge.yaml",
".smtpbridge.yml",
"~/.smtpbridge.yaml",
"~/.smtpbridge.yml",
))
parser, err := kong.New(&raw, kong.Configuration(kongyaml.Loader, configFiles...))
if err != nil {
return Raw{}, err
}
Expand All @@ -206,19 +228,44 @@ func Read(cli CLI) (Raw, error) {
raw.DataDirectory = cli.DataDirectory
}

if cli.SMTPDisable != nil {
raw.SMTP.Disable = bool(*cli.SMTPDisable)
}
if cli.SMTPHost != nil {
raw.SMTP.Host = string(*cli.SMTPHost)
}
if cli.SMTPPort != nil {
raw.SMTP.Port = int(*cli.SMTPPort)
}

if cli.HTTPDisable != nil {
raw.HTTP.Disable = bool(*cli.HTTPDisable)
}
if cli.HTTPHost != nil {
raw.HTTP.Host = string(*cli.HTTPHost)
}
if cli.HTTPPort != nil {
raw.HTTP.Port = int(*cli.HTTPPort)
}

return raw, nil
}

type CLI struct {
Command string `kong:"-"`
DataDirectory string `name:"data-directory" help:"Path to store data." type:"path" optional:""`
Version struct{} `cmd:"" hidden:""` // HACK: hidden because kong will throw error if a command is not supplied
Config *string `name:"config" help:"Path to config file." type:"string"`
DataDirectory string `name:"data-directory" help:"Path to store data." type:"path"`
SMTPDisable *bool `name:"smtp-disable" help:"Disable SMTP server."`
SMTPHost *string `name:"smtp-host" help:"SMTP host address to listen on."`
SMTPPort *uint16 `name:"smtp-port" help:"SMTP port to listen on"`
HTTPDisable *bool `name:"http-disable" help:"Disable HTTP server."`
HTTPHost *string `name:"http-host" help:"HTTP host address to listen on."`
HTTPPort *uint16 `name:"http-port" help:"HTTP port to listen on"`
Version bool `name:"version" help:"Show version."`
}

func ReadAndParseCLI() CLI {
cli := CLI{}
ctx := kong.Parse(&cli)
cli.Command = ctx.Command()
kong.Parse(&cli)

return cli
}
25 changes: 25 additions & 0 deletions config/resolve.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package config

import (
"os"

"github.com/alecthomas/kong"
)

func resolve(paths []string) (string, error) {
for _, path := range paths {
f, err := os.Open(kong.ExpandPath(path))
if err != nil {
if os.IsNotExist(err) || os.IsPermission(err) {
continue
}

return "", err
}
f.Close()

return path, nil
}

return "", nil
}
19 changes: 12 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
func main() {
cli := config.ReadAndParseCLI()

if cli.Command == "version" {
if cli.Version {
fmt.Println(version)
return
}
Expand Down Expand Up @@ -65,17 +65,22 @@ func run(ctx context.Context, shutdown context.CancelFunc, cli config.CLI) <-cha
procs.GardenerStart(ctx, app, cfg.RetentionPolicy)
procs.VacuumStart(ctx, app)

var backgrounds []background.Background

// SMTP
smtp := smtp.New(app, shutdown, cfg.SMTPAddress, cfg.SMTPMaxMessageBytes)
if !cfg.SMTPDisable {
smtp := smtp.New(app, shutdown, cfg.SMTPAddress, cfg.SMTPMaxMessageBytes)
backgrounds = append(backgrounds, smtp)
}

// HTTP
http := http.New(app, shutdown, cfg.HTTPAddress, cfg.HTTPBodyLimit, cfg.RetentionPolicy)
if !cfg.HTTPDisable {
http := http.New(app, shutdown, cfg.HTTPAddress, cfg.HTTPBodyLimit, cfg.RetentionPolicy)
backgrounds = append(backgrounds, http)
}

// Start
return background.Run(ctx,
smtp,
http,
)
return background.Run(ctx, backgrounds...)
}

var (
Expand Down

0 comments on commit 7d691d4

Please sign in to comment.