Support .env files if it exists

This commit is contained in:
Bernhard Froehlich
2022-08-15 13:35:12 +00:00
parent ee8a5dd989
commit 813bd9ebe7
2 changed files with 23 additions and 11 deletions

View File

@@ -22,7 +22,7 @@ device which produces mail.
## Main features ## 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) * Supports SMTPS/TLS (465), STARTTLS (587) and unencrypted SMTP (25)
* Checks for sender, receiver, client IP * Checks for sender, receiver, client IP
* Authentication support with file (LOGIN, PLAIN) * Authentication support with file (LOGIN, PLAIN)

View File

@@ -47,8 +47,8 @@ var (
remotesStr = flagset.String("remotes", "", "Outgoing SMTP servers") remotesStr = flagset.String("remotes", "", "Outgoing SMTP servers")
// additional flags // additional flags
_ = flagset.String("config", "", "Path to config file (ini format)") _ = flagset.String("config", "", "Path to config file (ini format)")
versionInfo = flagset.Bool("version", false, "Show version information") versionInfo = flagset.Bool("version", false, "Show version information")
// internal // internal
listenAddrs = []protoAddr{} listenAddrs = []protoAddr{}
@@ -194,14 +194,26 @@ func setupTimeouts() {
} }
func ConfigLoad() { func ConfigLoad() {
// configuration parsing // use .env file if it exists
if err := ff.Parse(flagset, os.Args[1:], if _, err := os.Stat(".env"); err == nil {
ff.WithEnvVarPrefix("smtprelay"), if err := ff.Parse(flagset, os.Args[1:],
ff.WithConfigFileFlag("config"), ff.WithEnvVarPrefix("smtprelay"),
ff.WithConfigFileParser(IniParser), ff.WithConfigFile(".env"),
); err != nil { ff.WithConfigFileParser(ff.EnvParser),
fmt.Fprintf(os.Stderr, "error: %v\n", err) ); err != nil {
os.Exit(1) 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 // Set up logging as soon as possible