MCPcopy
hub / github.com/caddyserver/caddy / FormattingDifference

Function FormattingDifference

caddyconfig/caddyfile/adapter.go:69–93  ·  view source on GitHub ↗

FormattingDifference returns a warning and true if the formatted version is any different from the input; empty warning and false otherwise. TODO: also perform this check on imported files

(filename string, body []byte)

Source from the content-addressed store, hash-verified

67// is any different from the input; empty warning and false otherwise.
68// TODO: also perform this check on imported files
69func FormattingDifference(filename string, body []byte) (caddyconfig.Warning, bool) {
70 // replace windows-style newlines to normalize comparison
71 normalizedBody := bytes.ReplaceAll(body, []byte("\r\n"), []byte("\n"))
72
73 formatted := Format(normalizedBody)
74 if bytes.Equal(formatted, normalizedBody) {
75 return caddyconfig.Warning{}, false
76 }
77
78 // find where the difference is
79 line := 1
80 for i, ch := range normalizedBody {
81 if i >= len(formatted) || ch != formatted[i] {
82 break
83 }
84 if ch == '\n' {
85 line++
86 }
87 }
88 return caddyconfig.Warning{
89 File: filename,
90 Line: line,
91 Message: "Caddyfile input is not formatted; run 'caddy fmt --overwrite' to fix inconsistencies",
92 }, true
93}
94
95// Unmarshaler is a type that can unmarshal Caddyfile tokens to
96// set itself up for a JSON encoding. The goal of an unmarshaler

Callers 2

cmdFmtFunction · 0.92
AdaptMethod · 0.85

Calls 3

FormatFunction · 0.85
ReplaceAllMethod · 0.80
EqualMethod · 0.45

Tested by

no test coverage detected