From 2151d8750b0aa718592ceefbce8ec01d82ff8548 Mon Sep 17 00:00:00 2001 From: Fernandez Ludovic Date: Fri, 2 Feb 2024 16:01:12 +0100 Subject: [PATCH] review --- .golangci.reference.yml | 4 ++++ pkg/commands/run.go | 37 ++++++++++++++++++++++++------------- pkg/config/run.go | 2 +- 3 files changed, 29 insertions(+), 14 deletions(-) diff --git a/.golangci.reference.yml b/.golangci.reference.yml index 8df0419b78e7..f9a8d0feec32 100644 --- a/.golangci.reference.yml +++ b/.golangci.reference.yml @@ -84,6 +84,10 @@ run: # Default: use Go version from the go.mod file, fallback on the env var `GOVERSION`, fallback on 1.17 go: '1.19' + # Show statistics per linter. + # Default: false + show-stats: true + # output configuration options output: diff --git a/pkg/commands/run.go b/pkg/commands/run.go index d31739959d13..1a8cfd8c2026 100644 --- a/pkg/commands/run.go +++ b/pkg/commands/run.go @@ -8,12 +8,14 @@ import ( "log" "os" "runtime" + "sort" "strings" "time" "github.com/fatih/color" "github.com/spf13/cobra" "github.com/spf13/pflag" + "golang.org/x/exp/maps" "github.com/golangci/golangci-lint/pkg/config" "github.com/golangci/golangci-lint/pkg/exitcodes" @@ -125,7 +127,7 @@ func initFlagSet(fs *pflag.FlagSet, cfg *config.Config, m *lintersdb.Manager, is const allowSerialDesc = "Allow multiple golangci-lint instances running, but serialize them around a lock. " + "If false (default) - golangci-lint exits with an error if it fails to acquire file lock on start." fs.BoolVar(&rc.AllowSerialRunners, "allow-serial-runners", false, wh(allowSerialDesc)) - fs.BoolVar(&rc.ShowStatsPerLinter, "show-stats-per-linter", false, wh("Show stats per linter")) + fs.BoolVar(&rc.ShowStats, "show-stats", false, wh("Show statistics per linter")) // Linters settings config lsc := &cfg.LintersSettings @@ -493,18 +495,27 @@ func (e *Executor) createPrinter(format string, w io.Writer) (printers.Printer, } func (e *Executor) printStats(issues []result.Issue) { - if e.cfg.Run.ShowStatsPerLinter { - stats := map[string]int{} - for idx := range issues { - stats[issues[idx].FromLinter]++ - } - e.runCmd.Println("Stats per linter:") - for linter, count := range stats { - e.runCmd.Printf(" %s: %d\n", linter, count) - } - if len(stats) == 0 { - e.runCmd.Println(" no issues") - } + if !e.cfg.Run.ShowStats { + return + } + + if len(issues) == 0 { + e.runCmd.Println("0 report.") + return + } + + stats := map[string]int{} + for idx := range issues { + stats[issues[idx].FromLinter]++ + } + + e.runCmd.Printf("%d reports:\n", len(issues)) + + keys := maps.Keys(stats) + sort.Strings(keys) + + for _, key := range keys { + e.runCmd.Printf("* %s: %d\n", key, stats[key]) } } diff --git a/pkg/config/run.go b/pkg/config/run.go index 3bdc93880b6a..2bb21d9fa224 100644 --- a/pkg/config/run.go +++ b/pkg/config/run.go @@ -38,5 +38,5 @@ type Run struct { AllowParallelRunners bool `mapstructure:"allow-parallel-runners"` AllowSerialRunners bool `mapstructure:"allow-serial-runners"` - ShowStatsPerLinter bool `mapstructure:"show-stats-per-linter"` + ShowStats bool `mapstructure:"show-stats"` }