diff --git a/README.md b/README.md index 65bab85..cb13f7a 100644 --- a/README.md +++ b/README.md @@ -91,6 +91,7 @@ go get -u github.com/mgechev/revive - `-formatter [NAME]` - formatter to be used for the output. The currently available formatters are: - `default` - will output the failures the same way that `golint` does. - `json` - outputs the failures in JSON format. + - `ndjson` - outputs the failures as stream in newline delimited JSON (NDJSON) format. - `friendly` - outputs the failures when found. Shows summary of all the failures. - `stylish` - formats the failures in a table. Keep in mind that it doesn't stream the output so it might be perceived as slower compared to others. diff --git a/config.go b/config.go index 730e3e1..7eb491b 100644 --- a/config.go +++ b/config.go @@ -61,6 +61,7 @@ var allFormatters = []lint.Formatter{ &formatter.Stylish{}, &formatter.Friendly{}, &formatter.JSON{}, + &formatter.NDJSON{}, &formatter.Default{}, } diff --git a/formatter/ndjson.go b/formatter/ndjson.go new file mode 100644 index 0000000..7b5ba61 --- /dev/null +++ b/formatter/ndjson.go @@ -0,0 +1,34 @@ +package formatter + +import ( + "encoding/json" + "os" + + "github.com/mgechev/revive/lint" +) + +// NDJSON is an implementation of the Formatter interface +// which formats the errors to NDJSON stream. +type NDJSON struct { + Metadata lint.FormatterMetadata +} + +// Name returns the name of the formatter +func (f *NDJSON) Name() string { + return "ndjson" +} + +// Format formats the failures gotten from the lint. +func (f *NDJSON) Format(failures <-chan lint.Failure, config lint.RulesConfig) (string, error) { + enc := json.NewEncoder(os.Stdout) + for failure := range failures { + obj := jsonObject{} + obj.Severity = severity(config, failure) + obj.Failure = failure + err := enc.Encode(obj) + if err != nil { + return "", err + } + } + return "", nil +}