Support .env files if it exists

This commit is contained in:
Bernhard Froehlich
2022-08-15 13:35:12 +00:00
parent ee8a5dd989
commit cff075dd8b
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

@@ -194,7 +194,18 @@ func setupTimeouts() {
} }
func ConfigLoad() { func ConfigLoad() {
// configuration parsing // 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:], if err := ff.Parse(flagset, os.Args[1:],
ff.WithEnvVarPrefix("smtprelay"), ff.WithEnvVarPrefix("smtprelay"),
ff.WithConfigFileFlag("config"), ff.WithConfigFileFlag("config"),
@@ -203,6 +214,7 @@ func ConfigLoad() {
fmt.Fprintf(os.Stderr, "error: %v\n", err) fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1) os.Exit(1)
} }
}
// Set up logging as soon as possible // Set up logging as soon as possible
setupLogger() setupLogger()