feat: allow running only a specific linter without modifying the file configuration (#4438)

This commit is contained in:
Ludovic Fernandez 2024-03-06 20:43:12 +01:00 committed by GitHub
parent 92c05581a3
commit 803970f0ed
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 28 additions and 3 deletions

View File

@ -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) {

View File

@ -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
}
@ -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