Skip to content

Commit

Permalink
feat: allow running only a specific linter without modifying the file…
Browse files Browse the repository at this point in the history
… configuration (#4438)
  • Loading branch information
ldez authored Mar 6, 2024
1 parent 92c0558 commit 803970f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
3 changes: 3 additions & 0 deletions pkg/commands/flagsets.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ func setupLintersFlagSet(v *viper.Viper, fs *pflag.FlagSet) {
fs.StringSliceP("presets", "p", nil,
color.GreenString(fmt.Sprintf("Enable presets (%s) of linters. Run 'golangci-lint help linters' to see "+
"them. This option implies option --disable-all", strings.Join(lintersdb.AllPresets(), "|"))))

fs.StringSlice("enable-only", nil,
color.GreenString("Override linters configuration section to only run the specific linter(s)")) // Flags only.
}

func setupRunFlagSet(v *viper.Viper, fs *pflag.FlagSet) {
Expand Down
28 changes: 25 additions & 3 deletions pkg/config/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,27 @@ func (l *Loader) Load() error {

l.handleGoVersion()

err = l.handleEnableOnlyOption()
if err != nil {
return err
}

return nil
}

func (l *Loader) handleEnableOnlyOption() error {
only, err := l.fs.GetStringSlice("enable-only")
if err != nil {
return err
}

if len(only) > 0 {
l.cfg.Linters = Linters{
Enable: only,
DisableAll: true,
}
}

return nil
}

Expand All @@ -73,13 +94,14 @@ func (l *Loader) handleGoVersion() {

l.cfg.LintersSettings.ParallelTest.Go = l.cfg.Run.Go

trimmedGoVersion := trimGoVersion(l.cfg.Run.Go)

l.cfg.LintersSettings.Gocritic.Go = trimmedGoVersion
if l.cfg.LintersSettings.Gofumpt.LangVersion == "" {
l.cfg.LintersSettings.Gofumpt.LangVersion = l.cfg.Run.Go
}

trimmedGoVersion := trimGoVersion(l.cfg.Run.Go)

l.cfg.LintersSettings.Gocritic.Go = trimmedGoVersion

// staticcheck related linters.
if l.cfg.LintersSettings.Staticcheck.GoVersion == "" {
l.cfg.LintersSettings.Staticcheck.GoVersion = trimmedGoVersion
Expand Down

0 comments on commit 803970f

Please sign in to comment.