Skip to content

Commit 5dc876c

Browse files
committed
pretty printing issues
1 parent 073ad51 commit 5dc876c

29 files changed

+226
-112
lines changed

internal/commands/run.go

Lines changed: 11 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,19 @@ package commands
22

33
import (
44
"context"
5-
"encoding/json"
65
"fmt"
76
"go/build"
87
"log"
9-
"os"
108
"strings"
11-
"syscall"
129
"time"
1310

14-
"github.com/fatih/color"
1511
"github.com/golangci/go-tools/ssa"
1612
"github.com/golangci/go-tools/ssa/ssautil"
1713
"github.com/golangci/golangci-lint/pkg"
1814
"github.com/golangci/golangci-lint/pkg/config"
1915
"github.com/golangci/golangci-lint/pkg/fsutils"
2016
"github.com/golangci/golangci-lint/pkg/golinters"
17+
"github.com/golangci/golangci-lint/pkg/printers"
2118
"github.com/golangci/golangci-lint/pkg/result"
2219
"github.com/golangci/golangci-lint/pkg/result/processors"
2320
"github.com/sirupsen/logrus"
@@ -39,6 +36,8 @@ func (e *Executor) initRun() {
3936
runCmd.Flags().StringVar(&rc.OutFormat, "out-format",
4037
config.OutFormatColoredLineNumber,
4138
fmt.Sprintf("Format of output: %s", strings.Join(config.OutFormats, "|")))
39+
runCmd.Flags().BoolVar(&rc.PrintIssuedLine, "print-issued-lines", true, "Print lines of code with issue")
40+
4241
runCmd.Flags().IntVar(&rc.ExitCodeIfIssuesFound, "issues-exit-code",
4342
1, "Exit code when issues were found")
4443
runCmd.Flags().StringSliceVar(&rc.BuildTags, "build-tags", []string{}, "Build tags (not all linters support them)")
@@ -227,9 +226,15 @@ func (e *Executor) executeRun(cmd *cobra.Command, args []string) {
227226
return err
228227
}
229228

230-
gotAnyIssues, err := outputIssues(e.cfg.Run.OutFormat, issues)
229+
var p printers.Printer
230+
if e.cfg.Run.OutFormat == config.OutFormatJSON {
231+
p = printers.NewJSON()
232+
} else {
233+
p = printers.NewText(e.cfg.Run.PrintIssuedLine, e.cfg.Run.OutFormat == config.OutFormatColoredLineNumber)
234+
}
235+
gotAnyIssues, err := p.Print(issues)
231236
if err != nil {
232-
return fmt.Errorf("can't output %d issues: %s", len(issues), err)
237+
return fmt.Errorf("can't print %d issues: %s", len(issues), err)
233238
}
234239

235240
if gotAnyIssues {
@@ -247,43 +252,3 @@ func (e *Executor) executeRun(cmd *cobra.Command, args []string) {
247252
}
248253
}
249254
}
250-
251-
func outputIssues(format string, issues chan result.Issue) (bool, error) {
252-
stdout := os.NewFile(uintptr(syscall.Stdout), "/dev/stdout") // was set to /dev/null
253-
if format == config.OutFormatLineNumber || format == config.OutFormatColoredLineNumber {
254-
gotAnyIssue := false
255-
for i := range issues {
256-
gotAnyIssue = true
257-
text := i.Text
258-
if format == config.OutFormatColoredLineNumber {
259-
text = color.RedString(text)
260-
}
261-
fmt.Fprintf(stdout, "%s:%d: %s\n", i.File, i.LineNumber, text)
262-
}
263-
264-
if !gotAnyIssue {
265-
outStr := "Congrats! No issues were found."
266-
if format == config.OutFormatColoredLineNumber {
267-
outStr = color.GreenString(outStr)
268-
}
269-
fmt.Fprintln(stdout, outStr)
270-
}
271-
272-
return gotAnyIssue, nil
273-
}
274-
275-
if format == config.OutFormatJSON {
276-
var allIssues []result.Issue
277-
for i := range issues {
278-
allIssues = append(allIssues, i)
279-
}
280-
outputJSON, err := json.Marshal(allIssues)
281-
if err != nil {
282-
return false, err
283-
}
284-
fmt.Fprint(stdout, string(outputJSON))
285-
return len(allIssues) != 0, nil
286-
}
287-
288-
return false, fmt.Errorf("unknown output format %q", format)
289-
}

pkg/config/config.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ type Run struct {
2727

2828
BuildTags []string
2929

30-
OutFormat string
30+
OutFormat string
31+
PrintIssuedLine bool
32+
3133
ExitCodeIfIssuesFound int
3234

3335
Errcheck struct {

pkg/golinters/deadcode.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ func (d Deadcode) Run(ctx context.Context, lintCtx *Context) ([]result.Issue, er
2323
var res []result.Issue
2424
for _, i := range issues {
2525
res = append(res, result.Issue{
26-
File: i.Pos.Filename,
27-
LineNumber: i.Pos.Line,
26+
Pos: i.Pos,
2827
Text: fmt.Sprintf("%s is unused", formatCode(i.UnusedIdentName, lintCtx.RunCfg())),
2928
FromLinter: d.Name(),
3029
})

pkg/golinters/dupl.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package golinters
33
import (
44
"context"
55
"fmt"
6+
"go/token"
67

78
"github.com/golangci/golangci-lint/pkg/result"
89
duplAPI "github.com/mibk/dupl"
@@ -27,8 +28,14 @@ func (d Dupl) Run(ctx context.Context, lintCtx *Context) ([]result.Issue, error)
2728
i.From.LineStart(), i.From.LineEnd(),
2829
formatCode(dupl, lintCtx.RunCfg()))
2930
res = append(res, result.Issue{
30-
File: i.From.Filename(),
31-
LineNumber: i.From.LineStart(),
31+
Pos: token.Position{
32+
Filename: i.From.Filename(),
33+
Line: i.From.LineStart(),
34+
},
35+
LineRange: result.Range{
36+
From: i.From.LineStart(),
37+
To: i.From.LineEnd(),
38+
},
3239
Text: text,
3340
FromLinter: d.Name(),
3441
})

pkg/golinters/errcheck.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@ func (e Errcheck) Run(ctx context.Context, lintCtx *Context) ([]result.Issue, er
3737
res = append(res, result.Issue{
3838
FromLinter: e.Name(),
3939
Text: text,
40-
LineNumber: i.Pos.Line,
41-
File: i.Pos.Filename,
40+
Pos: i.Pos,
4241
})
4342
}
4443

pkg/golinters/gas.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package golinters
33
import (
44
"context"
55
"fmt"
6+
"go/token"
67
"io/ioutil"
78
"log"
89
"strconv"
@@ -33,8 +34,10 @@ func (lint Gas) Run(ctx context.Context, lintCtx *Context) ([]result.Issue, erro
3334
text := fmt.Sprintf("%s: %s", i.RuleID, i.What) // TODO: use severity and confidence
3435
line, _ := strconv.Atoi(i.Line)
3536
res = append(res, result.Issue{
36-
File: i.File,
37-
LineNumber: line,
37+
Pos: token.Position{
38+
Filename: i.File,
39+
Line: line,
40+
},
3841
Text: text,
3942
FromLinter: lint.Name(),
4043
})

pkg/golinters/goconst.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@ func (lint Goconst) Run(ctx context.Context, lintCtx *Context) ([]result.Issue,
3333
textEnd = fmt.Sprintf(", but such constant %s already exists", formatCode(i.MatchingConst, lintCtx.RunCfg()))
3434
}
3535
res = append(res, result.Issue{
36-
File: i.Pos.Filename,
37-
LineNumber: i.Pos.Line,
36+
Pos: i.Pos,
3837
Text: textBegin + textEnd,
3938
FromLinter: lint.Name(),
4039
})

pkg/golinters/gocyclo.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ func (g Gocyclo) Run(ctx context.Context, lintCtx *Context) ([]result.Issue, err
2424
}
2525

2626
res = append(res, result.Issue{
27-
File: s.Pos.Filename,
28-
LineNumber: s.Pos.Line,
27+
Pos: s.Pos,
2928
Text: fmt.Sprintf("cyclomatic complexity %d of func %s is high (> %d)",
3029
s.Complexity, formatCode(s.FuncName, lintCtx.RunCfg()), lintCtx.RunCfg().Gocyclo.MinComplexity),
3130
FromLinter: g.Name(),

pkg/golinters/gofmt.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"context"
66
"fmt"
7+
"go/token"
78

89
gofmtAPI "github.com/golangci/gofmt/gofmt"
910
goimportsAPI "github.com/golangci/gofmt/goimports"
@@ -79,9 +80,11 @@ func (g Gofmt) extractIssuesFromPatch(patch string) ([]result.Issue, error) {
7980
}
8081
i := result.Issue{
8182
FromLinter: g.Name(),
82-
File: d.NewName,
83-
LineNumber: deletedLine,
84-
Text: text,
83+
Pos: token.Position{
84+
Filename: d.NewName,
85+
Line: deletedLine,
86+
},
87+
Text: text,
8588
}
8689
issues = append(issues, i)
8790
}

pkg/golinters/golint.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,8 @@ func lintFiles(minConfidence float64, filenames ...string) ([]result.Issue, erro
8989
for _, p := range ps {
9090
if p.Confidence >= minConfidence {
9191
issues = append(issues, result.Issue{
92-
File: p.Position.Filename,
93-
LineNumber: p.Position.Line,
94-
Text: p.Text,
92+
Pos: p.Position,
93+
Text: p.Text,
9594
})
9695
// TODO: use p.Link and p.Category
9796
}

0 commit comments

Comments
 (0)