diff options
author | Aziz Rmadi <[email protected]> | 2024-12-04 06:43:52 -0600 |
---|---|---|
committer | GitHub <[email protected]> | 2024-12-04 05:43:52 -0700 |
commit | fb72793269d419b2b37b5f1db8141c63818be514 (patch) | |
tree | 3b557839a5ddf955e00ece3c94ef389db796153e | |
parent | efd9251ad38a4fd9f7d900445400ac3c8e564c28 (diff) | |
download | caddy-fb72793269d419b2b37b5f1db8141c63818be514.tar.gz caddy-fb72793269d419b2b37b5f1db8141c63818be514.zip |
cmd: Reject multiple configs for fmt command (#6717)
-rw-r--r-- | cmd/commandfuncs.go | 9 | ||||
-rw-r--r-- | cmd/commands.go | 1 |
2 files changed, 8 insertions, 2 deletions
diff --git a/cmd/commandfuncs.go b/cmd/commandfuncs.go index a5c357cd7..2adf95bb3 100644 --- a/cmd/commandfuncs.go +++ b/cmd/commandfuncs.go @@ -560,10 +560,15 @@ func cmdValidateConfig(fl Flags) (int, error) { func cmdFmt(fl Flags) (int, error) { configFile := fl.Arg(0) - if configFile == "" { + configFlag := fl.String("config") + if (len(fl.Args()) > 1) || (configFlag != "" && configFile != "") { + return caddy.ExitCodeFailedStartup, fmt.Errorf("fmt does not support multiple files %s %s", configFlag, strings.Join(fl.Args(), " ")) + } + if configFile == "" && configFlag == "" { configFile = "Caddyfile" + } else if configFile == "" { + configFile = configFlag } - // as a special case, read from stdin if the file name is "-" if configFile == "-" { input, err := io.ReadAll(os.Stdin) diff --git a/cmd/commands.go b/cmd/commands.go index ebb57690c..259dd358f 100644 --- a/cmd/commands.go +++ b/cmd/commands.go @@ -388,6 +388,7 @@ When reading from stdin, the --overwrite flag has no effect: the result is always printed to stdout. `, CobraFunc: func(cmd *cobra.Command) { + cmd.Flags().StringP("config", "c", "", "Configuration file") cmd.Flags().BoolP("overwrite", "w", false, "Overwrite the input file with the results") cmd.Flags().BoolP("diff", "d", false, "Print the differences between the input file and the formatted output") cmd.RunE = WrapCommandFuncForCobra(cmdFmt) |