cmd: Add --envfile flag to validate command (#5350)

Fixes https://github.com/caddyserver/caddy/issues/5346
This commit is contained in:
Amis Shokoohi 2023-02-01 00:57:35 +03:30 committed by GitHub
parent 8c0b49bf03
commit 94b8d56096
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 2 deletions

View file

@ -506,6 +506,15 @@ func cmdAdaptConfig(fl Flags) (int, error) {
func cmdValidateConfig(fl Flags) (int, error) { func cmdValidateConfig(fl Flags) (int, error) {
validateCmdConfigFlag := fl.String("config") validateCmdConfigFlag := fl.String("config")
validateCmdAdapterFlag := fl.String("adapter") 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) input, _, err := LoadConfig(validateCmdConfigFlag, validateCmdAdapterFlag)
if err != nil { if err != nil {

View file

@ -287,16 +287,20 @@ zero exit status will be returned.`,
RegisterCommand(Command{ RegisterCommand(Command{
Name: "validate", Name: "validate",
Func: cmdValidateConfig, Func: cmdValidateConfig,
Usage: "--config <path> [--adapter <name>]", Usage: "--config <path> [--adapter <name>] [--envfile <path>]",
Short: "Tests whether a configuration file is valid", Short: "Tests whether a configuration file is valid",
Long: ` Long: `
Loads and provisions the provided config, but does not start running it. Loads and provisions the provided config, but does not start running it.
This reveals any errors with the configuration through the loading and 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 { Flags: func() *flag.FlagSet {
fs := flag.NewFlagSet("validate", flag.ExitOnError) fs := flag.NewFlagSet("validate", flag.ExitOnError)
fs.String("config", "", "Input configuration file") fs.String("config", "", "Input configuration file")
fs.String("adapter", "", "Name of config adapter") fs.String("adapter", "", "Name of config adapter")
fs.String("envfile", "", "Environment file to load")
return fs return fs
}(), }(),
}) })