add all warnings and error to json if out-format=json
This commit is contained in:
parent
93311aee9b
commit
fb38e51c83
@ -3,6 +3,7 @@ package commands
|
|||||||
import (
|
import (
|
||||||
"github.com/golangci/golangci-lint/pkg/config"
|
"github.com/golangci/golangci-lint/pkg/config"
|
||||||
"github.com/golangci/golangci-lint/pkg/logutils"
|
"github.com/golangci/golangci-lint/pkg/logutils"
|
||||||
|
"github.com/golangci/golangci-lint/pkg/report"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -16,6 +17,8 @@ type Executor struct {
|
|||||||
version, commit, date string
|
version, commit, date string
|
||||||
|
|
||||||
log logutils.Log
|
log logutils.Log
|
||||||
|
|
||||||
|
reportData report.Data
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewExecutor(version, commit, date string) *Executor {
|
func NewExecutor(version, commit, date string) *Executor {
|
||||||
@ -24,9 +27,10 @@ func NewExecutor(version, commit, date string) *Executor {
|
|||||||
version: version,
|
version: version,
|
||||||
commit: commit,
|
commit: commit,
|
||||||
date: date,
|
date: date,
|
||||||
log: logutils.NewStderrLog(""),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
e.log = report.NewLogWrapper(logutils.NewStderrLog(""), &e.reportData)
|
||||||
|
|
||||||
e.initRoot()
|
e.initRoot()
|
||||||
e.initRun()
|
e.initRun()
|
||||||
e.initLinters()
|
e.initLinters()
|
||||||
|
@ -211,6 +211,17 @@ func (e *Executor) runAnalysis(ctx context.Context, args []string) (<-chan resul
|
|||||||
return nil, err
|
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"))
|
lintCtx, err := lint.LoadContext(ctx, linters, e.cfg, e.log.Child("load"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -251,22 +262,9 @@ func (e *Executor) runAndPrint(ctx context.Context, args []string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
var p printers.Printer
|
p, err := e.createPrinter()
|
||||||
format := e.cfg.Output.Format
|
if err != nil {
|
||||||
switch format {
|
return err
|
||||||
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)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
gotAnyIssues, err := p.Print(ctx, issues)
|
gotAnyIssues, err := p.Print(ctx, issues)
|
||||||
@ -282,6 +280,28 @@ func (e *Executor) runAndPrint(ctx context.Context, args []string) error {
|
|||||||
return nil
|
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) {
|
func (e *Executor) executeRun(cmd *cobra.Command, args []string) {
|
||||||
needTrackResources := e.cfg.Run.IsVerbose || e.cfg.Run.PrintResourcesUsage
|
needTrackResources := e.cfg.Run.IsVerbose || e.cfg.Run.PrintResourcesUsage
|
||||||
trackResourcesEndCh := make(chan struct{})
|
trackResourcesEndCh := make(chan struct{})
|
||||||
|
@ -93,7 +93,7 @@ func (sl StderrLog) Debugf(format string, args ...interface{}) {
|
|||||||
func (sl StderrLog) Child(name string) Log {
|
func (sl StderrLog) Child(name string) Log {
|
||||||
prefix := ""
|
prefix := ""
|
||||||
if sl.name != "" {
|
if sl.name != "" {
|
||||||
prefix = sl.name + ":"
|
prefix = sl.name + "/"
|
||||||
}
|
}
|
||||||
|
|
||||||
child := sl
|
child := sl
|
||||||
|
@ -6,20 +6,26 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/golangci/golangci-lint/pkg/logutils"
|
"github.com/golangci/golangci-lint/pkg/logutils"
|
||||||
|
"github.com/golangci/golangci-lint/pkg/report"
|
||||||
"github.com/golangci/golangci-lint/pkg/result"
|
"github.com/golangci/golangci-lint/pkg/result"
|
||||||
)
|
)
|
||||||
|
|
||||||
type JSON struct{}
|
type JSON struct {
|
||||||
|
rd *report.Data
|
||||||
|
}
|
||||||
|
|
||||||
func NewJSON() *JSON {
|
func NewJSON(rd *report.Data) *JSON {
|
||||||
return &JSON{}
|
return &JSON{
|
||||||
|
rd: rd,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type JSONResult struct {
|
type JSONResult struct {
|
||||||
Issues []result.Issue
|
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{}
|
allIssues := []result.Issue{}
|
||||||
for i := range issues {
|
for i := range issues {
|
||||||
allIssues = append(allIssues, i)
|
allIssues = append(allIssues, i)
|
||||||
@ -27,6 +33,7 @@ func (JSON) Print(ctx context.Context, issues <-chan result.Issue) (bool, error)
|
|||||||
|
|
||||||
res := JSONResult{
|
res := JSONResult{
|
||||||
Issues: allIssues,
|
Issues: allIssues,
|
||||||
|
Report: p.rd,
|
||||||
}
|
}
|
||||||
|
|
||||||
outputJSON, err := json.Marshal(res)
|
outputJSON, err := json.Marshal(res)
|
||||||
|
26
pkg/report/data.go
Normal file
26
pkg/report/data.go
Normal 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
60
pkg/report/log.go
Normal 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)
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user