Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS
Mirrored from https://github.com/Slazee/caddy.git
Synced Nov 13, 06:45 UTC
cmd: `caddy fmt` return code is 1 if not formatted (#5297)
* cmd: Fix caddy fmt if input isn't formatted * Fixes #5294 * return exit 1 with an error message * cmd: Use formattingDifference for caddy fmt * #5294 * expose caddyfile.formattingDifference
Y.Horie committed
Jan 22, 2023 at 13:28 UTC
5805b3ca11492c2673180e707a643b24100a9e4a
2 files changed
+7
-3
caddyconfig/caddyfile/adapter.go
+3
-3
@@ -54,7 +54,7 @@ func (a Adapter) Adapt(body []byte, options map[string]any) ([]byte, []caddyconf
54
55
// lint check: see if input was properly formatted; sometimes messy files files parse
56
// successfully but result in logical errors (the Caddyfile is a bad format, I'm sorry)
57
- if warning, different := formattingDifference(filename, body); different {
57
+ if warning, different := FormattingDifference(filename, body); different {
58
warnings = append(warnings, warning)
59
}
60
@@ -63,10 +63,10 @@ func (a Adapter) Adapt(body []byte, options map[string]any) ([]byte, []caddyconf
63
return result, warnings, err
64
}
65
66
-// formattingDifference returns a warning and true if the formatted version
66
+// FormattingDifference returns a warning and true if the formatted version
67
// is any different from the input; empty warning and false otherwise.
68
// TODO: also perform this check on imported files
69
-func formattingDifference(filename string, body []byte) (caddyconfig.Warning, bool) {
69
+func FormattingDifference(filename string, body []byte) (caddyconfig.Warning, bool) {
70
// replace windows-style newlines to normalize comparison
71
normalizedBody := bytes.Replace(body, []byte("\r\n"), []byte("\n"), -1)
72
cmd/commandfuncs.go
+4
@@ -576,6 +576,10 @@ func cmdFmt(fl Flags) (int, error) {
576
fmt.Print(string(output))
577
}
578
579
+ if warning, diff := caddyfile.FormattingDifference(formatCmdConfigFile, input); diff {
580
+ return caddy.ExitCodeFailedStartup, fmt.Errorf("%s:%d: Caddyfile input is not formatted", warning.File, warning.Line)
581
+ }
582
+
583
return caddy.ExitCodeSuccess, nil
584
}
585