summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAmis Shokoohi <amisshokoohi@gmail.com>2023-02-01 00:57:35 +0330
committerGitHub <noreply@github.com>2023-01-31 16:27:35 -0500
commit94b8d56096b2581d6739b057655e7b895c8fd3bb (patch)
tree8a3b32ec744afe3f174c6ea91c7e6803e0e58009
parent8c0b49bf039bc0b6ab72b44ef0e1e6587c76ad2b (diff)
cmd: Add `--envfile` flag to `validate` command (#5350)
Fixes https://github.com/caddyserver/caddy/issues/5346
-rw-r--r--cmd/commandfuncs.go9
-rw-r--r--cmd/commands.go8
2 files changed, 15 insertions, 2 deletions
diff --git a/cmd/commandfuncs.go b/cmd/commandfuncs.go
index c5264ed..09accd0 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 83d3c99..9216b89 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
}(),
})