Skip to content

Commit fb72793

Browse files
authored
cmd: Reject multiple configs for fmt command (#6717)
1 parent efd9251 commit fb72793

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

cmd/commandfuncs.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -560,10 +560,15 @@ func cmdValidateConfig(fl Flags) (int, error) {
560560

561561
func cmdFmt(fl Flags) (int, error) {
562562
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 == "" {
564568
configFile = "Caddyfile"
569+
} else if configFile == "" {
570+
configFile = configFlag
565571
}
566-
567572
// as a special case, read from stdin if the file name is "-"
568573
if configFile == "-" {
569574
input, err := io.ReadAll(os.Stdin)

cmd/commands.go

+1
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,7 @@ When reading from stdin, the --overwrite flag has no effect: the result
388388
is always printed to stdout.
389389
`,
390390
CobraFunc: func(cmd *cobra.Command) {
391+
cmd.Flags().StringP("config", "c", "", "Configuration file")
391392
cmd.Flags().BoolP("overwrite", "w", false, "Overwrite the input file with the results")
392393
cmd.Flags().BoolP("diff", "d", false, "Print the differences between the input file and the formatted output")
393394
cmd.RunE = WrapCommandFuncForCobra(cmdFmt)

0 commit comments

Comments
 (0)