Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lint: Advertise regal fix command #1016

Merged
merged 1 commit into from
Aug 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions pkg/reporter/reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ import (
"github.com/owenrumney/go-sarif/v2/sarif"

"github.com/styrainc/regal/internal/novelty"
"github.com/styrainc/regal/internal/util"
"github.com/styrainc/regal/pkg/fixer"
"github.com/styrainc/regal/pkg/fixer/fixes"
"github.com/styrainc/regal/pkg/report"
)

Expand Down Expand Up @@ -149,6 +152,38 @@ func (tr PrettyReporter) Publish(_ context.Context, r report.Report) error {
}

_, err := fmt.Fprint(tr.out, table+footer+"\n")
if err != nil {
return fmt.Errorf("failed to write report: %w", err)
}

f := fixer.NewFixer()
f.RegisterFixes(fixes.NewDefaultFixes()...)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'll have to consider what to do here if we make the new directory-package-mismatch rule custom and not "default"... but let's just make a note on that for now.


fixableViolations := make(map[string]struct{})
fixableViolationsCount := 0

for _, violation := range r.Violations {
if fix, ok := f.GetFixForName(violation.Title); ok {
if _, ok := fixableViolations[fix.Name()]; !ok {
fixableViolations[fix.Name()] = struct{}{}
}

fixableViolationsCount++
}
}

if fixableViolationsCount > 0 {
_, err = fmt.Fprintf(
tr.out,
`
Hint: %d/%d violations can be automatically fixed (%s)
Run regal fix --help for more details.
`,
fixableViolationsCount,
r.Summary.NumViolations,
strings.Join(util.Keys(fixableViolations), ", "),
)
}

return err
}
Expand Down