Skip to content

Commit b3ffe70

Browse files
mostafaldez
andauthored
feat: implement stats per linter with a flag (#4341)
Co-authored-by: Fernandez Ludovic <[email protected]>
1 parent 7bc1927 commit b3ffe70

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

.golangci.reference.yml

+4
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ run:
8484
# Default: use Go version from the go.mod file, fallback on the env var `GOVERSION`, fallback on 1.17
8585
go: '1.19'
8686

87+
# Show statistics per linter.
88+
# Default: false
89+
show-stats: true
90+
8791

8892
# output configuration options
8993
output:

pkg/commands/run.go

+30
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@ import (
88
"log"
99
"os"
1010
"runtime"
11+
"sort"
1112
"strings"
1213
"time"
1314

1415
"github.com/fatih/color"
1516
"github.com/spf13/cobra"
1617
"github.com/spf13/pflag"
18+
"golang.org/x/exp/maps"
1719

1820
"github.com/golangci/golangci-lint/pkg/config"
1921
"github.com/golangci/golangci-lint/pkg/exitcodes"
@@ -125,6 +127,7 @@ func initFlagSet(fs *pflag.FlagSet, cfg *config.Config, m *lintersdb.Manager, is
125127
const allowSerialDesc = "Allow multiple golangci-lint instances running, but serialize them around a lock. " +
126128
"If false (default) - golangci-lint exits with an error if it fails to acquire file lock on start."
127129
fs.BoolVar(&rc.AllowSerialRunners, "allow-serial-runners", false, wh(allowSerialDesc))
130+
fs.BoolVar(&rc.ShowStats, "show-stats", false, wh("Show statistics per linter"))
128131

129132
// Linters settings config
130133
lsc := &cfg.LintersSettings
@@ -408,6 +411,8 @@ func (e *Executor) runAndPrint(ctx context.Context, args []string) error {
408411
}
409412
}
410413

414+
e.printStats(issues)
415+
411416
e.setExitCodeIfIssuesFound(issues)
412417

413418
e.fileCache.PrintStats(e.log)
@@ -489,6 +494,31 @@ func (e *Executor) createPrinter(format string, w io.Writer) (printers.Printer,
489494
return p, nil
490495
}
491496

497+
func (e *Executor) printStats(issues []result.Issue) {
498+
if !e.cfg.Run.ShowStats {
499+
return
500+
}
501+
502+
if len(issues) == 0 {
503+
e.runCmd.Println("0 issues.")
504+
return
505+
}
506+
507+
stats := map[string]int{}
508+
for idx := range issues {
509+
stats[issues[idx].FromLinter]++
510+
}
511+
512+
e.runCmd.Printf("%d issues:\n", len(issues))
513+
514+
keys := maps.Keys(stats)
515+
sort.Strings(keys)
516+
517+
for _, key := range keys {
518+
e.runCmd.Printf("* %s: %d\n", key, stats[key])
519+
}
520+
}
521+
492522
// executeRun executes the 'run' CLI command, which runs the linters.
493523
func (e *Executor) executeRun(_ *cobra.Command, args []string) {
494524
needTrackResources := e.cfg.Run.IsVerbose || e.cfg.Run.PrintResourcesUsage

pkg/config/run.go

+2
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,6 @@ type Run struct {
3737

3838
AllowParallelRunners bool `mapstructure:"allow-parallel-runners"`
3939
AllowSerialRunners bool `mapstructure:"allow-serial-runners"`
40+
41+
ShowStats bool `mapstructure:"show-stats"`
4042
}

0 commit comments

Comments
 (0)