@marcel91200 / caddy Mirror · read-only / commits / fb727932
Fork 0

Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS

Mirrored from https://github.com/Slazee/caddy.git

Synced Nov 12, 05:45 UTC

cmd: Reject multiple configs for fmt command (#6717)

Aziz Rmadi committed Dec 4, 2024 at 06:43 UTC fb72793269d419b2b37b5f1db8141c63818be514
2 files changed +8 -2
cmd/commandfuncs.go
+7 -2
@@ -560,10 +560,15 @@ func cmdValidateConfig(fl Flags) (int, error) {
560
561 func cmdFmt(fl Flags) (int, error) {
562 configFile := fl.Arg(0)
563 - if configFile == "" {
563 + configFlag := fl.String("config")
564 + if (len(fl.Args()) > 1) || (configFlag != "" && configFile != "") {
565 + return caddy.ExitCodeFailedStartup, fmt.Errorf("fmt does not support multiple files %s %s", configFlag, strings.Join(fl.Args(), " "))
566 + }
567 + if configFile == "" && configFlag == "" {
568 configFile = "Caddyfile"
569 + } else if configFile == "" {
570 + configFile = configFlag
571 }
566 -
572 // as a special case, read from stdin if the file name is "-"
573 if configFile == "-" {
574 input, err := io.ReadAll(os.Stdin)
cmd/commands.go
+1
@@ -388,6 +388,7 @@ When reading from stdin, the --overwrite flag has no effect: the result
388 is always printed to stdout.
389 `,
390 CobraFunc: func(cmd *cobra.Command) {
391 + cmd.Flags().StringP("config", "c", "", "Configuration file")
392 cmd.Flags().BoolP("overwrite", "w", false, "Overwrite the input file with the results")
393 cmd.Flags().BoolP("diff", "d", false, "Print the differences between the input file and the formatted output")
394 cmd.RunE = WrapCommandFuncForCobra(cmdFmt)