Repository: caddyserver/caddy (language: go)

Issue:
caddy validate --envfile
Is there a way to use caddy validate CLI and pass it --envfile? I get this error whenever I try to run it…

```
Error: loading tls app module: provision tls: provisioning automation policy 0: loading TLS automation management module: position 0: loading module 'acme': provision tls.issuance.acme: expanding email address '{env.EMAIL}': evaluated placeholder {env.EMAIL} is empty
```


Discussion / hints:
There's no `--envfile` for the `validate` command currently, but it could certainly be added.
I suppose we should check for `--envfile` flag in [`cmdValidateConfig`](https://github.com/caddyserver/caddy/blob/master/cmd/commandfuncs.go#L506), and if it exists then load the envs from file. Right?
```go
runCmdLoadEnvfileFlag := fl.String("envfile")

if runCmdLoadEnvfileFlag != "" {
	if err := loadEnvFromFile(runCmdLoadEnvfileFlag); err != nil {
		return caddy.ExitCodeFailedStartup,
			fmt.Errorf("loading additional environment variables: %v", err)
	}
}
```

I'd like to work on this. Would you please assign it to me?
Yep, that's the idea.

No need to assign the issue, feel free to open a PR!

Fix patch:
diff --git a/cmd/commandfuncs.go b/cmd/commandfuncs.go
index c5264ed41e4..09accd0d311 100644
--- a/cmd/commandfuncs.go
+++ b/cmd/commandfuncs.go
@@ -506,6 +506,15 @@ func cmdAdaptConfig(fl Flags) (int, error) {
 func cmdValidateConfig(fl Flags) (int, error) {
 	validateCmdConfigFlag := fl.String("config")
 	validateCmdAdapterFlag := fl.String("adapter")
+	runCmdLoadEnvfileFlag := fl.String("envfile")
+
+	// load all additional envs as soon as possible
+	if runCmdLoadEnvfileFlag != "" {
+		if err := loadEnvFromFile(runCmdLoadEnvfileFlag); err != nil {
+			return caddy.ExitCodeFailedStartup,
+				fmt.Errorf("loading additional environment variables: %v", err)
+		}
+	}
 
 	input, _, err := LoadConfig(validateCmdConfigFlag, validateCmdAdapterFlag)
 	if err != nil {
diff --git a/cmd/commands.go b/cmd/commands.go
index 83d3c99a1a8..9216b898a2d 100644
--- a/cmd/commands.go
+++ b/cmd/commands.go
@@ -287,16 +287,20 @@ zero exit status will be returned.`,
 	RegisterCommand(Command{
 		Name:  "validate",
 		Func:  cmdValidateConfig,
-		Usage: "--config <path> [--adapter <name>]",
+		Usage: "--config <path> [--adapter <name>] [--envfile <path>]",
 		Short: "Tests whether a configuration file is valid",
 		Long: `
 Loads and provisions the provided config, but does not start running it.
 This reveals any errors with the configuration through the loading and
-provisioning stages.`,
+provisioning stages.
+
+If --envfile is specified, an environment file with environment variables in
+the KEY=VALUE format will be loaded into the Caddy process.`,
 		Flags: func() *flag.FlagSet {
 			fs := flag.NewFlagSet("validate", flag.ExitOnError)
 			fs.String("config", "", "Input configuration file")
 			fs.String("adapter", "", "Name of config adapter")
+			fs.String("envfile", "", "Environment file to load")
 			return fs
 		}(),
 	})


Test patch: