Skip to content

Commit f0fdea0

Browse files
authored
dev: the printer just needs Output configuration (#4479)
1 parent 05f27ab commit f0fdea0

File tree

4 files changed

+53
-56
lines changed

4 files changed

+53
-56
lines changed

pkg/commands/run.go

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import (
3535
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis/load"
3636
"github.com/golangci/golangci-lint/pkg/goutil"
3737
"github.com/golangci/golangci-lint/pkg/lint"
38+
"github.com/golangci/golangci-lint/pkg/lint/linter"
3839
"github.com/golangci/golangci-lint/pkg/lint/lintersdb"
3940
"github.com/golangci/golangci-lint/pkg/logutils"
4041
"github.com/golangci/golangci-lint/pkg/packages"
@@ -186,7 +187,7 @@ func (c *runCommand) preRunE(_ *cobra.Command, _ []string) error {
186187

187188
c.dbManager = dbManager
188189

189-
printer, err := printers.NewPrinter(c.log, c.cfg, c.reportData)
190+
printer, err := printers.NewPrinter(c.log, &c.cfg.Output, c.reportData)
190191
if err != nil {
191192
return err
192193
}
@@ -327,11 +328,24 @@ func (c *runCommand) runAndPrint(ctx context.Context, args []string) error {
327328
}()
328329
}
329330

331+
enabledLintersMap, err := c.dbManager.GetEnabledLintersMap()
332+
if err != nil {
333+
return err
334+
}
335+
336+
c.printDeprecatedLinterMessages(enabledLintersMap)
337+
330338
issues, err := c.runAnalysis(ctx, args)
331339
if err != nil {
332340
return err // XXX: don't lose type
333341
}
334342

343+
// Fills linters information for the JSON printer.
344+
for _, lc := range c.dbManager.GetAllSupportedLinterConfigs() {
345+
isEnabled := enabledLintersMap[lc.Name()] != nil
346+
c.reportData.AddLinter(lc.Name(), isEnabled, lc.EnabledByDefault)
347+
}
348+
335349
err = c.printer.Print(issues)
336350
if err != nil {
337351
return err
@@ -355,16 +369,6 @@ func (c *runCommand) runAnalysis(ctx context.Context, args []string) ([]result.I
355369
return nil, err
356370
}
357371

358-
enabledLintersMap, err := c.dbManager.GetEnabledLintersMap()
359-
if err != nil {
360-
return nil, err
361-
}
362-
363-
for _, lc := range c.dbManager.GetAllSupportedLinterConfigs() {
364-
isEnabled := enabledLintersMap[lc.Name()] != nil
365-
c.reportData.AddLinter(lc.Name(), isEnabled, lc.EnabledByDefault)
366-
}
367-
368372
lintCtx, err := c.contextLoader.Load(ctx, c.log.Child(logutils.DebugKeyLintersContext), lintersToRun)
369373
if err != nil {
370374
return nil, fmt.Errorf("context loading failed: %w", err)
@@ -397,6 +401,25 @@ func (c *runCommand) setExitCodeIfIssuesFound(issues []result.Issue) {
397401
}
398402
}
399403

404+
func (c *runCommand) printDeprecatedLinterMessages(enabledLinters map[string]*linter.Config) {
405+
if c.cfg.InternalCmdTest {
406+
return
407+
}
408+
409+
for name, lc := range enabledLinters {
410+
if !lc.IsDeprecated() {
411+
continue
412+
}
413+
414+
var extra string
415+
if lc.Deprecation.Replacement != "" {
416+
extra = fmt.Sprintf("Replaced by %s.", lc.Deprecation.Replacement)
417+
}
418+
419+
c.log.Warnf("The linter '%s' is deprecated (since %s) due to: %s %s", name, lc.Deprecation.Since, lc.Deprecation.Message, extra)
420+
}
421+
}
422+
400423
func (c *runCommand) printStats(issues []result.Issue) {
401424
if c.cfg.Run.ShowStats {
402425
c.log.Warnf("The configuration option `run.show-stats` is deprecated, please use `output.show-stats`")

pkg/lint/runner.go

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -59,22 +59,6 @@ func NewRunner(log logutils.Log, cfg *config.Config, goenv *goutil.Env,
5959
return nil, fmt.Errorf("failed to get enabled linters: %w", err)
6060
}
6161

62-
// print deprecated messages
63-
if !cfg.InternalCmdTest {
64-
for name, lc := range enabledLinters {
65-
if !lc.IsDeprecated() {
66-
continue
67-
}
68-
69-
var extra string
70-
if lc.Deprecation.Replacement != "" {
71-
extra = fmt.Sprintf("Replaced by %s.", lc.Deprecation.Replacement)
72-
}
73-
74-
log.Warnf("The linter '%s' is deprecated (since %s) due to: %s %s", name, lc.Deprecation.Since, lc.Deprecation.Message, extra)
75-
}
76-
}
77-
7862
return &Runner{
7963
Processors: []processors.Processor{
8064
processors.NewCgo(goenv),

pkg/printers/printer.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ type issuePrinter interface {
2121

2222
// Printer prints issues
2323
type Printer struct {
24-
cfg *config.Config
24+
cfg *config.Output
2525
reportData *report.Data
2626

2727
log logutils.Log
@@ -31,7 +31,7 @@ type Printer struct {
3131
}
3232

3333
// NewPrinter creates a new Printer.
34-
func NewPrinter(log logutils.Log, cfg *config.Config, reportData *report.Data) (*Printer, error) {
34+
func NewPrinter(log logutils.Log, cfg *config.Output, reportData *report.Data) (*Printer, error) {
3535
if log == nil {
3636
return nil, errors.New("missing log argument in constructor")
3737
}
@@ -53,7 +53,7 @@ func NewPrinter(log logutils.Log, cfg *config.Config, reportData *report.Data) (
5353

5454
// Print prints issues based on the formats defined
5555
func (c *Printer) Print(issues []result.Issue) error {
56-
formats := strings.Split(c.cfg.Output.Format, ",")
56+
formats := strings.Split(c.cfg.Format, ",")
5757

5858
for _, item := range formats {
5959
format, path, _ := strings.Cut(item, ":")
@@ -114,11 +114,11 @@ func (c *Printer) createPrinter(format string, w io.Writer) (issuePrinter, error
114114
case config.OutFormatJSON:
115115
p = NewJSON(c.reportData, w)
116116
case config.OutFormatColoredLineNumber, config.OutFormatLineNumber:
117-
p = NewText(c.cfg.Output.PrintIssuedLine,
118-
format == config.OutFormatColoredLineNumber, c.cfg.Output.PrintLinterName,
117+
p = NewText(c.cfg.PrintIssuedLine,
118+
format == config.OutFormatColoredLineNumber, c.cfg.PrintLinterName,
119119
c.log.Child(logutils.DebugKeyTextPrinter), w)
120120
case config.OutFormatTab, config.OutFormatColoredTab:
121-
p = NewTab(c.cfg.Output.PrintLinterName,
121+
p = NewTab(c.cfg.PrintLinterName,
122122
format == config.OutFormatColoredTab,
123123
c.log.Child(logutils.DebugKeyTabPrinter), w)
124124
case config.OutFormatCheckstyle:

pkg/printers/printer_test.go

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -37,24 +37,20 @@ func TestPrinter_Print_stdout(t *testing.T) {
3737

3838
testCases := []struct {
3939
desc string
40-
cfg *config.Config
40+
cfg *config.Output
4141
expected string
4242
}{
4343
{
4444
desc: "stdout (implicit)",
45-
cfg: &config.Config{
46-
Output: config.Output{
47-
Format: "line-number",
48-
},
45+
cfg: &config.Output{
46+
Format: "line-number",
4947
},
5048
expected: "golden-line-number.txt",
5149
},
5250
{
5351
desc: "stdout (explicit)",
54-
cfg: &config.Config{
55-
Output: config.Output{
56-
Format: "line-number:stdout",
57-
},
52+
cfg: &config.Output{
53+
Format: "line-number:stdout",
5854
},
5955
expected: "golden-line-number.txt",
6056
},
@@ -95,10 +91,8 @@ func TestPrinter_Print_stderr(t *testing.T) {
9591
data := &report.Data{}
9692
unmarshalFile(t, "in-report-data.json", data)
9793

98-
cfg := &config.Config{
99-
Output: config.Output{
100-
Format: "line-number:stderr",
101-
},
94+
cfg := &config.Output{
95+
Format: "line-number:stderr",
10296
}
10397

10498
p, err := NewPrinter(logger, cfg, data)
@@ -131,10 +125,8 @@ func TestPrinter_Print_file(t *testing.T) {
131125

132126
outputPath := filepath.Join(t.TempDir(), "report.txt")
133127

134-
cfg := &config.Config{
135-
Output: config.Output{
136-
Format: "line-number:" + outputPath,
137-
},
128+
cfg := &config.Output{
129+
Format: "line-number:" + outputPath,
138130
}
139131

140132
p, err := NewPrinter(logger, cfg, data)
@@ -172,12 +164,10 @@ func TestPrinter_Print_multiple(t *testing.T) {
172164

173165
outputPath := filepath.Join(t.TempDir(), "github-actions.txt")
174166

175-
cfg := &config.Config{
176-
Output: config.Output{
177-
Format: "github-actions:" + outputPath +
178-
",json" +
179-
",line-number:stderr",
180-
},
167+
cfg := &config.Output{
168+
Format: "github-actions:" + outputPath +
169+
",json" +
170+
",line-number:stderr",
181171
}
182172

183173
p, err := NewPrinter(logger, cfg, data)

0 commit comments

Comments
 (0)