From e3614c481b77024f47017fb357db2f31f0f43194 Mon Sep 17 00:00:00 2001 From: Mostafa Moradian Date: Thu, 1 Feb 2024 13:15:19 +0100 Subject: [PATCH 1/5] Implement stats per linter with a flag --- pkg/commands/run.go | 17 +++++++++++++++++ pkg/config/run.go | 2 ++ 2 files changed, 19 insertions(+) diff --git a/pkg/commands/run.go b/pkg/commands/run.go index a082d7adfc91..4e2cdeeef13a 100644 --- a/pkg/commands/run.go +++ b/pkg/commands/run.go @@ -125,6 +125,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")) // Linters settings config lsc := &cfg.LintersSettings @@ -408,6 +409,8 @@ func (e *Executor) runAndPrint(ctx context.Context, args []string) error { } } + e.printStats(issues) + e.setExitCodeIfIssuesFound(issues) e.fileCache.PrintStats(e.log) @@ -489,6 +492,20 @@ func (e *Executor) createPrinter(format string, w io.Writer) (printers.Printer, return p, nil } +func (e *Executor) printStats(issues []result.Issue) { + if e.cfg.Run.ShowStatsPerLinter { + e.runCmd.Println("Stats per linter:") + stats := map[string]int{} + for idx := range issues { + stats[issues[idx].FromLinter]++ + } + + for linter, count := range stats { + e.runCmd.Printf(" %s: %d\n", linter, count) + } + } +} + // executeRun executes the 'run' CLI command, which runs the linters. func (e *Executor) executeRun(_ *cobra.Command, args []string) { needTrackResources := e.cfg.Run.IsVerbose || e.cfg.Run.PrintResourcesUsage diff --git a/pkg/config/run.go b/pkg/config/run.go index ff812d0a2583..3bdc93880b6a 100644 --- a/pkg/config/run.go +++ b/pkg/config/run.go @@ -37,4 +37,6 @@ type Run struct { AllowParallelRunners bool `mapstructure:"allow-parallel-runners"` AllowSerialRunners bool `mapstructure:"allow-serial-runners"` + + ShowStatsPerLinter bool `mapstructure:"show-stats-per-linter"` } From b19d51c04fee1d2451944ca7c8604a51a36d9944 Mon Sep 17 00:00:00 2001 From: Mostafa Moradian Date: Thu, 1 Feb 2024 13:46:40 +0100 Subject: [PATCH 2/5] Print no issue if there are none --- pkg/commands/run.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/pkg/commands/run.go b/pkg/commands/run.go index 4e2cdeeef13a..d31739959d13 100644 --- a/pkg/commands/run.go +++ b/pkg/commands/run.go @@ -494,15 +494,17 @@ func (e *Executor) createPrinter(format string, w io.Writer) (printers.Printer, func (e *Executor) printStats(issues []result.Issue) { if e.cfg.Run.ShowStatsPerLinter { - e.runCmd.Println("Stats per linter:") 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") + } } } From 2151d8750b0aa718592ceefbce8ec01d82ff8548 Mon Sep 17 00:00:00 2001 From: Fernandez Ludovic Date: Fri, 2 Feb 2024 16:01:12 +0100 Subject: [PATCH 3/5] 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"` } From d0701fe797e6307d923e62f3666ada787005d327 Mon Sep 17 00:00:00 2001 From: Fernandez Ludovic Date: Fri, 2 Feb 2024 16:14:30 +0100 Subject: [PATCH 4/5] review: replace report by issue --- pkg/commands/run.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/commands/run.go b/pkg/commands/run.go index 1a8cfd8c2026..4ba5a995da4e 100644 --- a/pkg/commands/run.go +++ b/pkg/commands/run.go @@ -500,7 +500,7 @@ func (e *Executor) printStats(issues []result.Issue) { } if len(issues) == 0 { - e.runCmd.Println("0 report.") + e.runCmd.Println("0 issue.") return } @@ -509,7 +509,7 @@ func (e *Executor) printStats(issues []result.Issue) { stats[issues[idx].FromLinter]++ } - e.runCmd.Printf("%d reports:\n", len(issues)) + e.runCmd.Printf("%d issues:\n", len(issues)) keys := maps.Keys(stats) sort.Strings(keys) From cc6b97b8f26f8ea42dee0cc8b1725fae5702c44e Mon Sep 17 00:00:00 2001 From: Ludovic Fernandez Date: Fri, 2 Feb 2024 20:22:00 +0100 Subject: [PATCH 5/5] Update pkg/commands/run.go Co-authored-by: Anton Telyshev --- pkg/commands/run.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/commands/run.go b/pkg/commands/run.go index 4ba5a995da4e..933096ada434 100644 --- a/pkg/commands/run.go +++ b/pkg/commands/run.go @@ -500,7 +500,7 @@ func (e *Executor) printStats(issues []result.Issue) { } if len(issues) == 0 { - e.runCmd.Println("0 issue.") + e.runCmd.Println("0 issues.") return }