Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd: Require config file exists for caddy validate #5614

Merged
merged 10 commits into from
Aug 9, 2023
20 changes: 20 additions & 0 deletions cmd/commandfuncs.go
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,26 @@ func cmdValidateConfig(fl Flags) (int, error) {
}
}

// if no input file was specified, try a default
// Caddyfile if the Caddyfile adapter is plugged in
if validateCmdConfigFlag == "" && caddyconfig.GetAdapter("caddyfile") != nil {
_, err := os.Stat("Caddyfile")
if err == nil {
// default Caddyfile exists
validateCmdConfigFlag = "Caddyfile"
validateCmdAdapterFlag = "caddyfile"
caddy.Log().Info("using adjacent Caddyfile")
} else if !os.IsNotExist(err) {
// default Caddyfile exists, but error accessing it
return caddy.ExitCodeFailedStartup, fmt.Errorf("accessing default Caddyfile: %v", err)
}
}

if validateCmdConfigFlag == "" {
return caddy.ExitCodeFailedStartup,
fmt.Errorf("input file required when there is no Caddyfile in current directory (use --config flag)")
}

input, _, err := LoadConfig(validateCmdConfigFlag, validateCmdAdapterFlag)
if err != nil {
return caddy.ExitCodeFailedStartup, err
Expand Down