dev: the printer just needs Output configuration (#4479)
This commit is contained in:
parent
05f27abc01
commit
f0fdea006f
@ -35,6 +35,7 @@ import (
|
|||||||
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis/load"
|
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis/load"
|
||||||
"github.com/golangci/golangci-lint/pkg/goutil"
|
"github.com/golangci/golangci-lint/pkg/goutil"
|
||||||
"github.com/golangci/golangci-lint/pkg/lint"
|
"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/lint/lintersdb"
|
||||||
"github.com/golangci/golangci-lint/pkg/logutils"
|
"github.com/golangci/golangci-lint/pkg/logutils"
|
||||||
"github.com/golangci/golangci-lint/pkg/packages"
|
"github.com/golangci/golangci-lint/pkg/packages"
|
||||||
@ -186,7 +187,7 @@ func (c *runCommand) preRunE(_ *cobra.Command, _ []string) error {
|
|||||||
|
|
||||||
c.dbManager = dbManager
|
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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -327,11 +328,24 @@ func (c *runCommand) runAndPrint(ctx context.Context, args []string) error {
|
|||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
enabledLintersMap, err := c.dbManager.GetEnabledLintersMap()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
c.printDeprecatedLinterMessages(enabledLintersMap)
|
||||||
|
|
||||||
issues, err := c.runAnalysis(ctx, args)
|
issues, err := c.runAnalysis(ctx, args)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err // XXX: don't lose type
|
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)
|
err = c.printer.Print(issues)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@ -355,16 +369,6 @@ func (c *runCommand) runAnalysis(ctx context.Context, args []string) ([]result.I
|
|||||||
return nil, err
|
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)
|
lintCtx, err := c.contextLoader.Load(ctx, c.log.Child(logutils.DebugKeyLintersContext), lintersToRun)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("context loading failed: %w", err)
|
return nil, fmt.Errorf("context loading failed: %w", err)
|
||||||
@ -397,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) {
|
func (c *runCommand) printStats(issues []result.Issue) {
|
||||||
if c.cfg.Run.ShowStats {
|
if c.cfg.Run.ShowStats {
|
||||||
c.log.Warnf("The configuration option `run.show-stats` is deprecated, please use `output.show-stats`")
|
c.log.Warnf("The configuration option `run.show-stats` is deprecated, please use `output.show-stats`")
|
||||||
|
@ -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)
|
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{
|
return &Runner{
|
||||||
Processors: []processors.Processor{
|
Processors: []processors.Processor{
|
||||||
processors.NewCgo(goenv),
|
processors.NewCgo(goenv),
|
||||||
|
@ -21,7 +21,7 @@ type issuePrinter interface {
|
|||||||
|
|
||||||
// Printer prints issues
|
// Printer prints issues
|
||||||
type Printer struct {
|
type Printer struct {
|
||||||
cfg *config.Config
|
cfg *config.Output
|
||||||
reportData *report.Data
|
reportData *report.Data
|
||||||
|
|
||||||
log logutils.Log
|
log logutils.Log
|
||||||
@ -31,7 +31,7 @@ type Printer struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// NewPrinter creates a new Printer.
|
// 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 {
|
if log == nil {
|
||||||
return nil, errors.New("missing log argument in constructor")
|
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
|
// Print prints issues based on the formats defined
|
||||||
func (c *Printer) Print(issues []result.Issue) error {
|
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 {
|
for _, item := range formats {
|
||||||
format, path, _ := strings.Cut(item, ":")
|
format, path, _ := strings.Cut(item, ":")
|
||||||
@ -114,11 +114,11 @@ func (c *Printer) createPrinter(format string, w io.Writer) (issuePrinter, error
|
|||||||
case config.OutFormatJSON:
|
case config.OutFormatJSON:
|
||||||
p = NewJSON(c.reportData, w)
|
p = NewJSON(c.reportData, w)
|
||||||
case config.OutFormatColoredLineNumber, config.OutFormatLineNumber:
|
case config.OutFormatColoredLineNumber, config.OutFormatLineNumber:
|
||||||
p = NewText(c.cfg.Output.PrintIssuedLine,
|
p = NewText(c.cfg.PrintIssuedLine,
|
||||||
format == config.OutFormatColoredLineNumber, c.cfg.Output.PrintLinterName,
|
format == config.OutFormatColoredLineNumber, c.cfg.PrintLinterName,
|
||||||
c.log.Child(logutils.DebugKeyTextPrinter), w)
|
c.log.Child(logutils.DebugKeyTextPrinter), w)
|
||||||
case config.OutFormatTab, config.OutFormatColoredTab:
|
case config.OutFormatTab, config.OutFormatColoredTab:
|
||||||
p = NewTab(c.cfg.Output.PrintLinterName,
|
p = NewTab(c.cfg.PrintLinterName,
|
||||||
format == config.OutFormatColoredTab,
|
format == config.OutFormatColoredTab,
|
||||||
c.log.Child(logutils.DebugKeyTabPrinter), w)
|
c.log.Child(logutils.DebugKeyTabPrinter), w)
|
||||||
case config.OutFormatCheckstyle:
|
case config.OutFormatCheckstyle:
|
||||||
|
@ -37,25 +37,21 @@ func TestPrinter_Print_stdout(t *testing.T) {
|
|||||||
|
|
||||||
testCases := []struct {
|
testCases := []struct {
|
||||||
desc string
|
desc string
|
||||||
cfg *config.Config
|
cfg *config.Output
|
||||||
expected string
|
expected string
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
desc: "stdout (implicit)",
|
desc: "stdout (implicit)",
|
||||||
cfg: &config.Config{
|
cfg: &config.Output{
|
||||||
Output: config.Output{
|
|
||||||
Format: "line-number",
|
Format: "line-number",
|
||||||
},
|
},
|
||||||
},
|
|
||||||
expected: "golden-line-number.txt",
|
expected: "golden-line-number.txt",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
desc: "stdout (explicit)",
|
desc: "stdout (explicit)",
|
||||||
cfg: &config.Config{
|
cfg: &config.Output{
|
||||||
Output: config.Output{
|
|
||||||
Format: "line-number:stdout",
|
Format: "line-number:stdout",
|
||||||
},
|
},
|
||||||
},
|
|
||||||
expected: "golden-line-number.txt",
|
expected: "golden-line-number.txt",
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@ -95,10 +91,8 @@ func TestPrinter_Print_stderr(t *testing.T) {
|
|||||||
data := &report.Data{}
|
data := &report.Data{}
|
||||||
unmarshalFile(t, "in-report-data.json", data)
|
unmarshalFile(t, "in-report-data.json", data)
|
||||||
|
|
||||||
cfg := &config.Config{
|
cfg := &config.Output{
|
||||||
Output: config.Output{
|
|
||||||
Format: "line-number:stderr",
|
Format: "line-number:stderr",
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
p, err := NewPrinter(logger, cfg, data)
|
p, err := NewPrinter(logger, cfg, data)
|
||||||
@ -131,10 +125,8 @@ func TestPrinter_Print_file(t *testing.T) {
|
|||||||
|
|
||||||
outputPath := filepath.Join(t.TempDir(), "report.txt")
|
outputPath := filepath.Join(t.TempDir(), "report.txt")
|
||||||
|
|
||||||
cfg := &config.Config{
|
cfg := &config.Output{
|
||||||
Output: config.Output{
|
|
||||||
Format: "line-number:" + outputPath,
|
Format: "line-number:" + outputPath,
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
p, err := NewPrinter(logger, cfg, data)
|
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")
|
outputPath := filepath.Join(t.TempDir(), "github-actions.txt")
|
||||||
|
|
||||||
cfg := &config.Config{
|
cfg := &config.Output{
|
||||||
Output: config.Output{
|
|
||||||
Format: "github-actions:" + outputPath +
|
Format: "github-actions:" + outputPath +
|
||||||
",json" +
|
",json" +
|
||||||
",line-number:stderr",
|
",line-number:stderr",
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
p, err := NewPrinter(logger, cfg, data)
|
p, err := NewPrinter(logger, cfg, data)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user