Repository: caddyserver/caddy (language: go)

Issue:
caddy fmt --overwrite exit status 1
Why does `caddy fmt --overwrite` exit with status 1 after overwriting the config file?
```
Error: ../../Caddyfile:4: Caddyfile input is not formatted
exit status 1
```
Shouldn't it successfully exit [here](https://github.com/caddyserver/caddy/blob/master/cmd/commandfuncs.go#L560)?
```go
if fl.Bool("overwrite") {
	if err := os.WriteFile(formatCmdConfigFile, output, 0600); err != nil {
		return caddy.ExitCodeFailedStartup, fmt.Errorf("overwriting formatted file: %v", err)
	}
	return caddy.ExitCodeSuccess, nil
}
```


Discussion / hints:
Yeah, you're right. That was a recent change from https://github.com/caddyserver/caddy/pull/5297 but the intent was to only change the exit code for the non-overwrite case, I think.

PR is welcome to fix it! Thanks for pointing that out.

Fix patch:
diff --git a/cmd/commandfuncs.go b/cmd/commandfuncs.go
index bc4c22781a0..c5264ed41e4 100644
--- a/cmd/commandfuncs.go
+++ b/cmd/commandfuncs.go
@@ -558,7 +558,10 @@ func cmdFmt(fl Flags) (int, error) {
 		if err := os.WriteFile(formatCmdConfigFile, output, 0600); err != nil {
 			return caddy.ExitCodeFailedStartup, fmt.Errorf("overwriting formatted file: %v", err)
 		}
-	} else if fl.Bool("diff") {
+		return caddy.ExitCodeSuccess, nil
+	}
+
+	if fl.Bool("diff") {
 		diff := difflib.Diff(
 			strings.Split(string(input), "\n"),
 			strings.Split(string(output), "\n"))


Test patch: