feat: implement stats per linter with a flag (#4341)
Co-authored-by: Fernandez Ludovic <ldez@users.noreply.github.com>
This commit is contained in:
parent
7bc19270fc
commit
b3ffe708b4
@ -84,6 +84,10 @@ run:
|
|||||||
# Default: use Go version from the go.mod file, fallback on the env var `GOVERSION`, fallback on 1.17
|
# Default: use Go version from the go.mod file, fallback on the env var `GOVERSION`, fallback on 1.17
|
||||||
go: '1.19'
|
go: '1.19'
|
||||||
|
|
||||||
|
# Show statistics per linter.
|
||||||
|
# Default: false
|
||||||
|
show-stats: true
|
||||||
|
|
||||||
|
|
||||||
# output configuration options
|
# output configuration options
|
||||||
output:
|
output:
|
||||||
|
@ -8,12 +8,14 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"runtime"
|
"runtime"
|
||||||
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/fatih/color"
|
"github.com/fatih/color"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"github.com/spf13/pflag"
|
"github.com/spf13/pflag"
|
||||||
|
"golang.org/x/exp/maps"
|
||||||
|
|
||||||
"github.com/golangci/golangci-lint/pkg/config"
|
"github.com/golangci/golangci-lint/pkg/config"
|
||||||
"github.com/golangci/golangci-lint/pkg/exitcodes"
|
"github.com/golangci/golangci-lint/pkg/exitcodes"
|
||||||
@ -125,6 +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. " +
|
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."
|
"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.AllowSerialRunners, "allow-serial-runners", false, wh(allowSerialDesc))
|
||||||
|
fs.BoolVar(&rc.ShowStats, "show-stats", false, wh("Show statistics per linter"))
|
||||||
|
|
||||||
// Linters settings config
|
// Linters settings config
|
||||||
lsc := &cfg.LintersSettings
|
lsc := &cfg.LintersSettings
|
||||||
@ -408,6 +411,8 @@ func (e *Executor) runAndPrint(ctx context.Context, args []string) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
e.printStats(issues)
|
||||||
|
|
||||||
e.setExitCodeIfIssuesFound(issues)
|
e.setExitCodeIfIssuesFound(issues)
|
||||||
|
|
||||||
e.fileCache.PrintStats(e.log)
|
e.fileCache.PrintStats(e.log)
|
||||||
@ -489,6 +494,31 @@ func (e *Executor) createPrinter(format string, w io.Writer) (printers.Printer,
|
|||||||
return p, nil
|
return p, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (e *Executor) printStats(issues []result.Issue) {
|
||||||
|
if !e.cfg.Run.ShowStats {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(issues) == 0 {
|
||||||
|
e.runCmd.Println("0 issues.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
stats := map[string]int{}
|
||||||
|
for idx := range issues {
|
||||||
|
stats[issues[idx].FromLinter]++
|
||||||
|
}
|
||||||
|
|
||||||
|
e.runCmd.Printf("%d issues:\n", len(issues))
|
||||||
|
|
||||||
|
keys := maps.Keys(stats)
|
||||||
|
sort.Strings(keys)
|
||||||
|
|
||||||
|
for _, key := range keys {
|
||||||
|
e.runCmd.Printf("* %s: %d\n", key, stats[key])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// executeRun executes the 'run' CLI command, which runs the linters.
|
// executeRun executes the 'run' CLI command, which runs the linters.
|
||||||
func (e *Executor) executeRun(_ *cobra.Command, args []string) {
|
func (e *Executor) executeRun(_ *cobra.Command, args []string) {
|
||||||
needTrackResources := e.cfg.Run.IsVerbose || e.cfg.Run.PrintResourcesUsage
|
needTrackResources := e.cfg.Run.IsVerbose || e.cfg.Run.PrintResourcesUsage
|
||||||
|
@ -37,4 +37,6 @@ type Run struct {
|
|||||||
|
|
||||||
AllowParallelRunners bool `mapstructure:"allow-parallel-runners"`
|
AllowParallelRunners bool `mapstructure:"allow-parallel-runners"`
|
||||||
AllowSerialRunners bool `mapstructure:"allow-serial-runners"`
|
AllowSerialRunners bool `mapstructure:"allow-serial-runners"`
|
||||||
|
|
||||||
|
ShowStats bool `mapstructure:"show-stats"`
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user