diff options
author | Matthew Holt <mholt@users.noreply.github.com> | 2020-02-04 10:48:02 -0700 |
---|---|---|
committer | Matthew Holt <mholt@users.noreply.github.com> | 2020-02-04 10:48:02 -0700 |
commit | 3592e593990d8ae1ca6aec0f5a6d5a0cfa83cf4c (patch) | |
tree | 1e0423896ba3e68eb2585fc3ae2b3499f108b05c | |
parent | f74fed3f544651c1870d285011598d4fef5e205b (diff) |
cmd: adapt: Make --config flag optional when Caddyfile exists
-rw-r--r-- | cmd/commandfuncs.go | 22 |
1 files changed, 20 insertions, 2 deletions
diff --git a/cmd/commandfuncs.go b/cmd/commandfuncs.go index 8d52258..df3e498 100644 --- a/cmd/commandfuncs.go +++ b/cmd/commandfuncs.go @@ -413,9 +413,27 @@ func cmdAdaptConfig(fl Flags) (int, error) { adaptCmdPrettyFlag := fl.Bool("pretty") adaptCmdValidateFlag := fl.Bool("validate") - if adaptCmdAdapterFlag == "" || adaptCmdInputFlag == "" { + // if no input file was specified, try a default + // Caddyfile if the Caddyfile adapter is plugged in + if adaptCmdInputFlag == "" && caddyconfig.GetAdapter("caddyfile") != nil { + _, err := os.Stat("Caddyfile") + if err == nil { + // default Caddyfile exists + adaptCmdInputFlag = "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 adaptCmdInputFlag == "" { + return caddy.ExitCodeFailedStartup, + fmt.Errorf("input file required when there is no Caddyfile in current directory (use --config flag)") + } + if adaptCmdAdapterFlag == "" { return caddy.ExitCodeFailedStartup, - fmt.Errorf("--adapter and --config flags are required") + fmt.Errorf("adapter name is required (use --adapt flag or leave unspecified for default)") } cfgAdapter := caddyconfig.GetAdapter(adaptCmdAdapterFlag) |