Skip to content

Commit

Permalink
Support .env files if it exists
Browse files Browse the repository at this point in the history
  • Loading branch information
decke committed Aug 15, 2022
1 parent ee8a5dd commit 813bd9e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ device which produces mail.

## Main features

* Simple configuration with ini file or environment variables
* Simple configuration with ini file .env file or environment variables
* Supports SMTPS/TLS (465), STARTTLS (587) and unencrypted SMTP (25)
* Checks for sender, receiver, client IP
* Authentication support with file (LOGIN, PLAIN)
Expand Down
32 changes: 22 additions & 10 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ var (
remotesStr = flagset.String("remotes", "", "Outgoing SMTP servers")

// additional flags
_ = flagset.String("config", "", "Path to config file (ini format)")
versionInfo = flagset.Bool("version", false, "Show version information")
_ = flagset.String("config", "", "Path to config file (ini format)")
versionInfo = flagset.Bool("version", false, "Show version information")

// internal
listenAddrs = []protoAddr{}
Expand Down Expand Up @@ -194,14 +194,26 @@ func setupTimeouts() {
}

func ConfigLoad() {
// configuration parsing
if err := ff.Parse(flagset, os.Args[1:],
ff.WithEnvVarPrefix("smtprelay"),
ff.WithConfigFileFlag("config"),
ff.WithConfigFileParser(IniParser),
); err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
// use .env file if it exists
if _, err := os.Stat(".env"); err == nil {
if err := ff.Parse(flagset, os.Args[1:],
ff.WithEnvVarPrefix("smtprelay"),
ff.WithConfigFile(".env"),
ff.WithConfigFileParser(ff.EnvParser),
); err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
} else {
// use env variables and smtprelay.ini file
if err := ff.Parse(flagset, os.Args[1:],
ff.WithEnvVarPrefix("smtprelay"),
ff.WithConfigFileFlag("config"),
ff.WithConfigFileParser(IniParser),
); err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
}

// Set up logging as soon as possible
Expand Down

0 comments on commit 813bd9e

Please sign in to comment.