Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS
Mirrored from https://github.com/Slazee/caddy.git
Synced Nov 13, 06:45 UTC
cmd: Add `--envfile` flag to `validate` command (#5350)
Fixes https://github.com/caddyserver/caddy/issues/5346
Amis Shokoohi committed
Feb 1, 2023 at 00:57 UTC
94b8d56096b2581d6739b057655e7b895c8fd3bb
2 files changed
+15
-2
cmd/commandfuncs.go
+9
@@ -506,6 +506,15 @@ func cmdAdaptConfig(fl Flags) (int, error) {
506
func cmdValidateConfig(fl Flags) (int, error) {
507
validateCmdConfigFlag := fl.String("config")
508
validateCmdAdapterFlag := fl.String("adapter")
509
+ runCmdLoadEnvfileFlag := fl.String("envfile")
510
+
511
+ // load all additional envs as soon as possible
512
+ if runCmdLoadEnvfileFlag != "" {
513
+ if err := loadEnvFromFile(runCmdLoadEnvfileFlag); err != nil {
514
+ return caddy.ExitCodeFailedStartup,
515
+ fmt.Errorf("loading additional environment variables: %v", err)
516
+ }
517
+ }
518
519
input, _, err := LoadConfig(validateCmdConfigFlag, validateCmdAdapterFlag)
520
if err != nil {
cmd/commands.go
+6
-2
@@ -287,16 +287,20 @@ zero exit status will be returned.`,
287
RegisterCommand(Command{
288
Name: "validate",
289
Func: cmdValidateConfig,
290
- Usage: "--config <path> [--adapter <name>]",
290
+ Usage: "--config <path> [--adapter <name>] [--envfile <path>]",
291
Short: "Tests whether a configuration file is valid",
292
Long: `
293
Loads and provisions the provided config, but does not start running it.
294
This reveals any errors with the configuration through the loading and
295
-provisioning stages.`,
295
+provisioning stages.
296
+
297
+If --envfile is specified, an environment file with environment variables in
298
+the KEY=VALUE format will be loaded into the Caddy process.`,
299
Flags: func() *flag.FlagSet {
300
fs := flag.NewFlagSet("validate", flag.ExitOnError)
301
fs.String("config", "", "Input configuration file")
302
fs.String("adapter", "", "Name of config adapter")
303
+ fs.String("envfile", "", "Environment file to load")
304
return fs
305
}(),
306
})