add all warnings and error to json if out-format=json

This commit is contained in:
Denis Isaev 2018-06-24 09:38:51 +03:00 committed by Isaev Denis
parent 93311aee9b
commit fb38e51c83
6 changed files with 139 additions and 22 deletions

View File

@ -3,6 +3,7 @@ package commands
import (
"github.com/golangci/golangci-lint/pkg/config"
"github.com/golangci/golangci-lint/pkg/logutils"
"github.com/golangci/golangci-lint/pkg/report"
"github.com/spf13/cobra"
)
@ -16,6 +17,8 @@ type Executor struct {
version, commit, date string
log logutils.Log
reportData report.Data
}
func NewExecutor(version, commit, date string) *Executor {
@ -24,9 +27,10 @@ func NewExecutor(version, commit, date string) *Executor {
version: version,
commit: commit,
date: date,
log: logutils.NewStderrLog(""),
}
e.log = report.NewLogWrapper(logutils.NewStderrLog(""), &e.reportData)
e.initRoot()
e.initRun()
e.initLinters()

View File

@ -211,6 +211,17 @@ func (e *Executor) runAnalysis(ctx context.Context, args []string) (<-chan resul
return nil, err
}
for _, lc := range lintersdb.GetAllSupportedLinterConfigs() {
isEnabled := false
for _, linter := range linters {
if linter.Linter.Name() == lc.Linter.Name() {
isEnabled = true
break
}
}
e.reportData.AddLinter(lc.Linter.Name(), isEnabled, lc.EnabledByDefault)
}
lintCtx, err := lint.LoadContext(ctx, linters, e.cfg, e.log.Child("load"))
if err != nil {
return nil, err
@ -251,22 +262,9 @@ func (e *Executor) runAndPrint(ctx context.Context, args []string) error {
return err
}
var p printers.Printer
format := e.cfg.Output.Format
switch format {
case config.OutFormatJSON:
p = printers.NewJSON()
case config.OutFormatColoredLineNumber, config.OutFormatLineNumber:
p = printers.NewText(e.cfg.Output.PrintIssuedLine,
format == config.OutFormatColoredLineNumber, e.cfg.Output.PrintLinterName, e.cfg.Run.Silent,
e.log.Child("text_printer"))
case config.OutFormatTab:
p = printers.NewTab(e.cfg.Output.PrintLinterName, e.cfg.Run.Silent,
e.log.Child("tab_printer"))
case config.OutFormatCheckstyle:
p = printers.NewCheckstyle()
default:
return fmt.Errorf("unknown output format %s", format)
p, err := e.createPrinter()
if err != nil {
return err
}
gotAnyIssues, err := p.Print(ctx, issues)
@ -282,6 +280,28 @@ func (e *Executor) runAndPrint(ctx context.Context, args []string) error {
return nil
}
func (e *Executor) createPrinter() (printers.Printer, error) {
var p printers.Printer
format := e.cfg.Output.Format
switch format {
case config.OutFormatJSON:
p = printers.NewJSON(&e.reportData)
case config.OutFormatColoredLineNumber, config.OutFormatLineNumber:
p = printers.NewText(e.cfg.Output.PrintIssuedLine,
format == config.OutFormatColoredLineNumber, e.cfg.Output.PrintLinterName, e.cfg.Run.Silent,
e.log.Child("text_printer"))
case config.OutFormatTab:
p = printers.NewTab(e.cfg.Output.PrintLinterName, e.cfg.Run.Silent,
e.log.Child("tab_printer"))
case config.OutFormatCheckstyle:
p = printers.NewCheckstyle()
default:
return nil, fmt.Errorf("unknown output format %s", format)
}
return p, nil
}
func (e *Executor) executeRun(cmd *cobra.Command, args []string) {
needTrackResources := e.cfg.Run.IsVerbose || e.cfg.Run.PrintResourcesUsage
trackResourcesEndCh := make(chan struct{})

View File

@ -93,7 +93,7 @@ func (sl StderrLog) Debugf(format string, args ...interface{}) {
func (sl StderrLog) Child(name string) Log {
prefix := ""
if sl.name != "" {
prefix = sl.name + ":"
prefix = sl.name + "/"
}
child := sl

View File

@ -6,20 +6,26 @@ import (
"fmt"
"github.com/golangci/golangci-lint/pkg/logutils"
"github.com/golangci/golangci-lint/pkg/report"
"github.com/golangci/golangci-lint/pkg/result"
)
type JSON struct{}
type JSON struct {
rd *report.Data
}
func NewJSON() *JSON {
return &JSON{}
func NewJSON(rd *report.Data) *JSON {
return &JSON{
rd: rd,
}
}
type JSONResult struct {
Issues []result.Issue
Report *report.Data
}
func (JSON) Print(ctx context.Context, issues <-chan result.Issue) (bool, error) {
func (p JSON) Print(ctx context.Context, issues <-chan result.Issue) (bool, error) {
allIssues := []result.Issue{}
for i := range issues {
allIssues = append(allIssues, i)
@ -27,6 +33,7 @@ func (JSON) Print(ctx context.Context, issues <-chan result.Issue) (bool, error)
res := JSONResult{
Issues: allIssues,
Report: p.rd,
}
outputJSON, err := json.Marshal(res)

26
pkg/report/data.go Normal file
View File

@ -0,0 +1,26 @@
package report
type Warning struct {
Tag string `json:",omitempty"`
Text string
}
type LinterData struct {
Name string
Enabled bool `json:",omitempty"`
EnabledByDefault bool `json:",omitempty"`
}
type Data struct {
Warnings []Warning `json:",omitempty"`
Linters []LinterData `json:",omitempty"`
Error string `json:",omitempty"`
}
func (d *Data) AddLinter(name string, enabled, enabledByDefault bool) {
d.Linters = append(d.Linters, LinterData{
Name: name,
Enabled: enabled,
EnabledByDefault: enabledByDefault,
})
}

60
pkg/report/log.go Normal file
View File

@ -0,0 +1,60 @@
package report
import (
"fmt"
"strings"
"github.com/golangci/golangci-lint/pkg/logutils"
)
type LogWrapper struct {
rd *Data
tags []string
origLog logutils.Log
}
func NewLogWrapper(log logutils.Log, reportData *Data) *LogWrapper {
return &LogWrapper{
rd: reportData,
origLog: log,
}
}
func (lw LogWrapper) Fatalf(format string, args ...interface{}) {
lw.origLog.Fatalf(format, args...)
}
func (lw LogWrapper) Errorf(format string, args ...interface{}) {
lw.origLog.Errorf(format, args...)
lw.rd.Error = fmt.Sprintf(format, args...)
}
func (lw LogWrapper) Warnf(format string, args ...interface{}) {
lw.origLog.Warnf(format, args...)
w := Warning{
Tag: strings.Join(lw.tags, "/"),
Text: fmt.Sprintf(format, args...),
}
lw.rd.Warnings = append(lw.rd.Warnings, w)
}
func (lw LogWrapper) Infof(format string, args ...interface{}) {
lw.origLog.Infof(format, args...)
}
func (lw LogWrapper) Child(name string) logutils.Log {
c := lw
c.origLog = lw.origLog.Child(name)
c.tags = append([]string{}, lw.tags...)
c.tags = append(c.tags, name)
return c
}
func (lw LogWrapper) SetLevel(level logutils.LogLevel) {
lw.origLog.SetLevel(level)
}
func (lw LogWrapper) GoString() string {
return fmt.Sprintf("lw: %+v, orig log: %#v", lw, lw.origLog)
}