diff options
author | Matt Holt <mholt@users.noreply.github.com> | 2021-01-04 11:11:36 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-01-04 11:11:36 -0700 |
commit | c8557dc00bd93ce8ecf0bb724856a320e129c71b (patch) | |
tree | 0310586287637c9114228452c6620aa5a84c736a /caddyconfig/caddyfile | |
parent | 1b453dd4fbea2f3a54362fb4c2115bab85cad1b7 (diff) |
caddyfile: Introduce basic linting and fmt check (#3923)
* caddyfile: Introduce basic linting and fmt check
This will help encourage people to keep their Caddyfiles tidy.
* Remove unrelated tests
I am not sure that testing the output of warnings here is quite the
right idea; these tests are just for syntax and parsing success.
Diffstat (limited to 'caddyconfig/caddyfile')
-rw-r--r-- | caddyconfig/caddyfile/adapter.go | 15 |
1 files changed, 11 insertions, 4 deletions
diff --git a/caddyconfig/caddyfile/adapter.go b/caddyconfig/caddyfile/adapter.go index 5b4495e..8d624f1 100644 --- a/caddyconfig/caddyfile/adapter.go +++ b/caddyconfig/caddyfile/adapter.go @@ -15,6 +15,7 @@ package caddyfile import ( + "bytes" "encoding/json" "fmt" @@ -51,11 +52,17 @@ func (a Adapter) Adapt(body []byte, options map[string]interface{}) ([]byte, []c return nil, warnings, err } - marshalFunc := json.Marshal - if options["pretty"] == "true" { - marshalFunc = caddyconfig.JSONIndent + // lint check: see if input was properly formatted; sometimes messy files files parse + // successfully but result in logical errors because the Caddyfile is a bad format + // TODO: also perform this check on imported files + if !bytes.Equal(Format(body), body) { + warnings = append(warnings, caddyconfig.Warning{ + File: filename, + Message: "file is not formatted with 'caddy fmt'", + }) } - result, err := marshalFunc(cfg) + + result, err := json.Marshal(cfg) return result, warnings, err } |