From c0b24dc7195fef3a3da12d487ce582e487c3844f Mon Sep 17 00:00:00 2001 From: Fernandez Ludovic Date: Sun, 10 Mar 2024 16:25:01 +0100 Subject: [PATCH 1/5] chore: the printer just needs Output configuration --- pkg/commands/run.go | 2 +- pkg/printers/printer.go | 12 ++++++------ pkg/printers/printer_test.go | 36 +++++++++++++----------------------- 3 files changed, 20 insertions(+), 30 deletions(-) diff --git a/pkg/commands/run.go b/pkg/commands/run.go index bcf4c51c437c..af160349a065 100644 --- a/pkg/commands/run.go +++ b/pkg/commands/run.go @@ -186,7 +186,7 @@ func (c *runCommand) preRunE(_ *cobra.Command, _ []string) error { c.dbManager = dbManager - printer, err := printers.NewPrinter(c.log, c.cfg, c.reportData) + printer, err := printers.NewPrinter(c.log, &c.cfg.Output, c.reportData) if err != nil { return err } diff --git a/pkg/printers/printer.go b/pkg/printers/printer.go index 97e2b5d3209d..a42fda7ec847 100644 --- a/pkg/printers/printer.go +++ b/pkg/printers/printer.go @@ -21,7 +21,7 @@ type issuePrinter interface { // Printer prints issues type Printer struct { - cfg *config.Config + cfg *config.Output reportData *report.Data log logutils.Log @@ -31,7 +31,7 @@ type Printer struct { } // NewPrinter creates a new Printer. -func NewPrinter(log logutils.Log, cfg *config.Config, reportData *report.Data) (*Printer, error) { +func NewPrinter(log logutils.Log, cfg *config.Output, reportData *report.Data) (*Printer, error) { if log == nil { return nil, errors.New("missing log argument in constructor") } @@ -53,7 +53,7 @@ func NewPrinter(log logutils.Log, cfg *config.Config, reportData *report.Data) ( // Print prints issues based on the formats defined func (c *Printer) Print(issues []result.Issue) error { - formats := strings.Split(c.cfg.Output.Format, ",") + formats := strings.Split(c.cfg.Format, ",") for _, item := range formats { format, path, _ := strings.Cut(item, ":") @@ -114,11 +114,11 @@ func (c *Printer) createPrinter(format string, w io.Writer) (issuePrinter, error case config.OutFormatJSON: p = NewJSON(c.reportData, w) case config.OutFormatColoredLineNumber, config.OutFormatLineNumber: - p = NewText(c.cfg.Output.PrintIssuedLine, - format == config.OutFormatColoredLineNumber, c.cfg.Output.PrintLinterName, + p = NewText(c.cfg.PrintIssuedLine, + format == config.OutFormatColoredLineNumber, c.cfg.PrintLinterName, c.log.Child(logutils.DebugKeyTextPrinter), w) case config.OutFormatTab, config.OutFormatColoredTab: - p = NewTab(c.cfg.Output.PrintLinterName, + p = NewTab(c.cfg.PrintLinterName, format == config.OutFormatColoredTab, c.log.Child(logutils.DebugKeyTabPrinter), w) case config.OutFormatCheckstyle: diff --git a/pkg/printers/printer_test.go b/pkg/printers/printer_test.go index 2bc50feb9010..9170e30a0587 100644 --- a/pkg/printers/printer_test.go +++ b/pkg/printers/printer_test.go @@ -37,24 +37,20 @@ func TestPrinter_Print_stdout(t *testing.T) { testCases := []struct { desc string - cfg *config.Config + cfg *config.Output expected string }{ { desc: "stdout (implicit)", - cfg: &config.Config{ - Output: config.Output{ - Format: "line-number", - }, + cfg: &config.Output{ + Format: "line-number", }, expected: "golden-line-number.txt", }, { desc: "stdout (explicit)", - cfg: &config.Config{ - Output: config.Output{ - Format: "line-number:stdout", - }, + cfg: &config.Output{ + Format: "line-number:stdout", }, expected: "golden-line-number.txt", }, @@ -95,10 +91,8 @@ func TestPrinter_Print_stderr(t *testing.T) { data := &report.Data{} unmarshalFile(t, "in-report-data.json", data) - cfg := &config.Config{ - Output: config.Output{ - Format: "line-number:stderr", - }, + cfg := &config.Output{ + Format: "line-number:stderr", } p, err := NewPrinter(logger, cfg, data) @@ -131,10 +125,8 @@ func TestPrinter_Print_file(t *testing.T) { outputPath := filepath.Join(t.TempDir(), "report.txt") - cfg := &config.Config{ - Output: config.Output{ - Format: "line-number:" + outputPath, - }, + cfg := &config.Output{ + Format: "line-number:" + outputPath, } p, err := NewPrinter(logger, cfg, data) @@ -172,12 +164,10 @@ func TestPrinter_Print_multiple(t *testing.T) { outputPath := filepath.Join(t.TempDir(), "github-actions.txt") - cfg := &config.Config{ - Output: config.Output{ - Format: "github-actions:" + outputPath + - ",json" + - ",line-number:stderr", - }, + cfg := &config.Output{ + Format: "github-actions:" + outputPath + + ",json" + + ",line-number:stderr", } p, err := NewPrinter(logger, cfg, data) From 9deb5da0c750c9a9349e14efb073e905d785b4c8 Mon Sep 17 00:00:00 2001 From: Fernandez Ludovic Date: Sun, 10 Mar 2024 16:41:32 +0100 Subject: [PATCH 2/5] chore: move report data linter filling --- pkg/commands/run.go | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/pkg/commands/run.go b/pkg/commands/run.go index af160349a065..96962ede9057 100644 --- a/pkg/commands/run.go +++ b/pkg/commands/run.go @@ -327,6 +327,17 @@ func (c *runCommand) runAndPrint(ctx context.Context, args []string) error { }() } + enabledLintersMap, err := c.dbManager.GetEnabledLintersMap() + if err != nil { + return err + } + + // Fills linters information for the JSON printer. + for _, lc := range c.dbManager.GetAllSupportedLinterConfigs() { + isEnabled := enabledLintersMap[lc.Name()] != nil + c.reportData.AddLinter(lc.Name(), isEnabled, lc.EnabledByDefault) + } + issues, err := c.runAnalysis(ctx, args) if err != nil { return err // XXX: don't lose type @@ -355,16 +366,6 @@ func (c *runCommand) runAnalysis(ctx context.Context, args []string) ([]result.I return nil, err } - enabledLintersMap, err := c.dbManager.GetEnabledLintersMap() - if err != nil { - return nil, err - } - - for _, lc := range c.dbManager.GetAllSupportedLinterConfigs() { - isEnabled := enabledLintersMap[lc.Name()] != nil - c.reportData.AddLinter(lc.Name(), isEnabled, lc.EnabledByDefault) - } - lintCtx, err := c.contextLoader.Load(ctx, c.log.Child(logutils.DebugKeyLintersContext), lintersToRun) if err != nil { return nil, fmt.Errorf("context loading failed: %w", err) From c64f16d382f1730d636045f2bddaea41e0ce416b Mon Sep 17 00:00:00 2001 From: Fernandez Ludovic Date: Sun, 10 Mar 2024 16:50:14 +0100 Subject: [PATCH 3/5] chore: move print deprecated linters --- pkg/commands/run.go | 22 ++++++++++++++++++++++ pkg/lint/runner.go | 16 ---------------- 2 files changed, 22 insertions(+), 16 deletions(-) diff --git a/pkg/commands/run.go b/pkg/commands/run.go index 96962ede9057..2d6186bad6b6 100644 --- a/pkg/commands/run.go +++ b/pkg/commands/run.go @@ -20,6 +20,7 @@ import ( "github.com/fatih/color" "github.com/gofrs/flock" + "github.com/golangci/golangci-lint/pkg/lint/linter" "github.com/spf13/cobra" "github.com/spf13/pflag" "github.com/spf13/viper" @@ -338,6 +339,8 @@ func (c *runCommand) runAndPrint(ctx context.Context, args []string) error { c.reportData.AddLinter(lc.Name(), isEnabled, lc.EnabledByDefault) } + c.printDeprecatedLinterMessages(enabledLintersMap) + issues, err := c.runAnalysis(ctx, args) if err != nil { return err // XXX: don't lose type @@ -398,6 +401,25 @@ func (c *runCommand) setExitCodeIfIssuesFound(issues []result.Issue) { } } +func (c *runCommand) printDeprecatedLinterMessages(enabledLinters map[string]*linter.Config) { + if c.cfg.InternalCmdTest { + return + } + + for name, lc := range enabledLinters { + if !lc.IsDeprecated() { + continue + } + + var extra string + if lc.Deprecation.Replacement != "" { + extra = fmt.Sprintf("Replaced by %s.", lc.Deprecation.Replacement) + } + + c.log.Warnf("The linter '%s' is deprecated (since %s) due to: %s %s", name, lc.Deprecation.Since, lc.Deprecation.Message, extra) + } +} + func (c *runCommand) printStats(issues []result.Issue) { if c.cfg.Run.ShowStats { c.log.Warnf("The configuration option `run.show-stats` is deprecated, please use `output.show-stats`") diff --git a/pkg/lint/runner.go b/pkg/lint/runner.go index 2362042cc665..78be9382dac4 100644 --- a/pkg/lint/runner.go +++ b/pkg/lint/runner.go @@ -59,22 +59,6 @@ func NewRunner(log logutils.Log, cfg *config.Config, goenv *goutil.Env, return nil, fmt.Errorf("failed to get enabled linters: %w", err) } - // print deprecated messages - if !cfg.InternalCmdTest { - for name, lc := range enabledLinters { - if !lc.IsDeprecated() { - continue - } - - var extra string - if lc.Deprecation.Replacement != "" { - extra = fmt.Sprintf("Replaced by %s.", lc.Deprecation.Replacement) - } - - log.Warnf("The linter '%s' is deprecated (since %s) due to: %s %s", name, lc.Deprecation.Since, lc.Deprecation.Message, extra) - } - } - return &Runner{ Processors: []processors.Processor{ processors.NewCgo(goenv), From 0fb08739205834020405a3ad3c60d214c1e63b7f Mon Sep 17 00:00:00 2001 From: Fernandez Ludovic Date: Sun, 10 Mar 2024 16:58:47 +0100 Subject: [PATCH 4/5] chore: organize imports --- 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 2d6186bad6b6..f966faa5bf1b 100644 --- a/pkg/commands/run.go +++ b/pkg/commands/run.go @@ -20,7 +20,6 @@ import ( "github.com/fatih/color" "github.com/gofrs/flock" - "github.com/golangci/golangci-lint/pkg/lint/linter" "github.com/spf13/cobra" "github.com/spf13/pflag" "github.com/spf13/viper" @@ -36,6 +35,7 @@ import ( "github.com/golangci/golangci-lint/pkg/golinters/goanalysis/load" "github.com/golangci/golangci-lint/pkg/goutil" "github.com/golangci/golangci-lint/pkg/lint" + "github.com/golangci/golangci-lint/pkg/lint/linter" "github.com/golangci/golangci-lint/pkg/lint/lintersdb" "github.com/golangci/golangci-lint/pkg/logutils" "github.com/golangci/golangci-lint/pkg/packages" From e91794f1d85fde99941decb81be30062750241e4 Mon Sep 17 00:00:00 2001 From: Fernandez Ludovic Date: Sun, 10 Mar 2024 17:35:07 +0100 Subject: [PATCH 5/5] chore: move report data linter filling --- pkg/commands/run.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/pkg/commands/run.go b/pkg/commands/run.go index f966faa5bf1b..0e1be529660d 100644 --- a/pkg/commands/run.go +++ b/pkg/commands/run.go @@ -333,12 +333,6 @@ func (c *runCommand) runAndPrint(ctx context.Context, args []string) error { return err } - // Fills linters information for the JSON printer. - for _, lc := range c.dbManager.GetAllSupportedLinterConfigs() { - isEnabled := enabledLintersMap[lc.Name()] != nil - c.reportData.AddLinter(lc.Name(), isEnabled, lc.EnabledByDefault) - } - c.printDeprecatedLinterMessages(enabledLintersMap) issues, err := c.runAnalysis(ctx, args) @@ -346,6 +340,12 @@ func (c *runCommand) runAndPrint(ctx context.Context, args []string) error { return err // XXX: don't lose type } + // Fills linters information for the JSON printer. + for _, lc := range c.dbManager.GetAllSupportedLinterConfigs() { + isEnabled := enabledLintersMap[lc.Name()] != nil + c.reportData.AddLinter(lc.Name(), isEnabled, lc.EnabledByDefault) + } + err = c.printer.Print(issues) if err != nil { return err