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)
| 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) { |
| 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 |
no test coverage detected