2018-07-14 00:01:27 +02:00
|
|
|
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.
|
2019-08-02 17:21:33 +02:00
|
|
|
func (f *NDJSON) Format(failures <-chan lint.Failure, config lint.Config) (string, error) {
|
2018-07-14 00:01:27 +02:00
|
|
|
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
|
|
|
|
}
|