From fb38e51c8324e6c39c3254316d0ecc079bd13883 Mon Sep 17 00:00:00 2001 From: Denis Isaev Date: Sun, 24 Jun 2018 09:38:51 +0300 Subject: [PATCH] add all warnings and error to json if out-format=json --- pkg/commands/executor.go | 6 +++- pkg/commands/run.go | 52 +++++++++++++++++++++++---------- pkg/logutils/stderr_log.go | 2 +- pkg/printers/json.go | 15 +++++++--- pkg/report/data.go | 26 +++++++++++++++++ pkg/report/log.go | 60 ++++++++++++++++++++++++++++++++++++++ 6 files changed, 139 insertions(+), 22 deletions(-) create mode 100644 pkg/report/data.go create mode 100644 pkg/report/log.go diff --git a/pkg/commands/executor.go b/pkg/commands/executor.go index a62b1c1d..eb274341 100644 --- a/pkg/commands/executor.go +++ b/pkg/commands/executor.go @@ -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() diff --git a/pkg/commands/run.go b/pkg/commands/run.go index 71d8f52b..8fc665e5 100644 --- a/pkg/commands/run.go +++ b/pkg/commands/run.go @@ -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{}) diff --git a/pkg/logutils/stderr_log.go b/pkg/logutils/stderr_log.go index 67b217bc..0cf44488 100644 --- a/pkg/logutils/stderr_log.go +++ b/pkg/logutils/stderr_log.go @@ -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 diff --git a/pkg/printers/json.go b/pkg/printers/json.go index 49c36155..adb665fd 100644 --- a/pkg/printers/json.go +++ b/pkg/printers/json.go @@ -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) diff --git a/pkg/report/data.go b/pkg/report/data.go new file mode 100644 index 00000000..f083fa9f --- /dev/null +++ b/pkg/report/data.go @@ -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, + }) +} diff --git a/pkg/report/log.go b/pkg/report/log.go new file mode 100644 index 00000000..c18007a6 --- /dev/null +++ b/pkg/report/log.go @@ -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) +}