Skip to content

Commit 5737c95

Browse files
committed
chore: sepration of concerns
1 parent 63306f5 commit 5737c95

File tree

5 files changed

+43
-21
lines changed

5 files changed

+43
-21
lines changed

pkg/config/config.go

+7-12
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package config
22

33
import (
4-
"errors"
5-
"fmt"
64
"os"
75
"regexp"
86
"strings"
@@ -34,19 +32,16 @@ func (c *Config) GetConfigDir() string {
3432
}
3533

3634
func (c *Config) Validate() error {
37-
for i, rule := range c.Issues.ExcludeRules {
38-
if err := rule.Validate(); err != nil {
39-
return fmt.Errorf("error in exclude rule #%d: %w", i, err)
40-
}
35+
if err := c.Issues.Validate(); err != nil {
36+
return err
4137
}
4238

43-
if len(c.Severity.Rules) > 0 && c.Severity.Default == "" {
44-
return errors.New("can't set severity rule option: no default severity defined")
39+
if err := c.Severity.Validate(); err != nil {
40+
return err
4541
}
46-
for i, rule := range c.Severity.Rules {
47-
if err := rule.Validate(); err != nil {
48-
return fmt.Errorf("error in severity rule #%d: %w", i, err)
49-
}
42+
43+
if err := c.LintersSettings.Validate(); err != nil {
44+
return err
5045
}
5146

5247
return nil

pkg/config/issues.go

+10
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,16 @@ type Issues struct {
122122
NeedFix bool `mapstructure:"fix"`
123123
}
124124

125+
func (i *Issues) Validate() error {
126+
for i, rule := range i.ExcludeRules {
127+
if err := rule.Validate(); err != nil {
128+
return fmt.Errorf("error in exclude rule #%d: %w", i, err)
129+
}
130+
}
131+
132+
return nil
133+
}
134+
125135
type ExcludeRule struct {
126136
BaseRule `mapstructure:",squash"`
127137
}

pkg/config/linters_settings.go

+7-4
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,10 @@ type LintersSettings struct {
291291
Custom map[string]CustomLinterSettings
292292
}
293293

294+
func (s *LintersSettings) Validate() error {
295+
return s.Govet.Validate()
296+
}
297+
294298
type AsasalintSettings struct {
295299
Exclude []string `mapstructure:"exclude"`
296300
UseBuiltinExclusions bool `mapstructure:"use-builtin-exclusions"`
@@ -606,15 +610,14 @@ type GovetSettings struct {
606610
}
607611

608612
func (cfg *GovetSettings) Validate() error {
609-
// TODO(ldez) need to be move into the linter file.
610613
if cfg.EnableAll && cfg.DisableAll {
611-
return errors.New("enable-all and disable-all can't be combined")
614+
return errors.New("govet: enable-all and disable-all can't be combined")
612615
}
613616
if cfg.EnableAll && len(cfg.Enable) != 0 {
614-
return errors.New("enable-all and enable can't be combined")
617+
return errors.New("govet: enable-all and enable can't be combined")
615618
}
616619
if cfg.DisableAll && len(cfg.Disable) != 0 {
617-
return errors.New("disable-all and disable can't be combined")
620+
return errors.New("govet: disable-all and disable can't be combined")
618621
}
619622
return nil
620623
}

pkg/config/severity.go

+19
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
package config
22

3+
import (
4+
"errors"
5+
"fmt"
6+
)
7+
38
const severityRuleMinConditionsCount = 1
49

510
type Severity struct {
@@ -8,6 +13,20 @@ type Severity struct {
813
Rules []SeverityRule `mapstructure:"rules"`
914
}
1015

16+
func (s *Severity) Validate() error {
17+
if len(s.Rules) > 0 && s.Default == "" {
18+
return errors.New("can't set severity rule option: no default severity defined")
19+
}
20+
21+
for i, rule := range s.Rules {
22+
if err := rule.Validate(); err != nil {
23+
return fmt.Errorf("error in severity rule #%d: %w", i, err)
24+
}
25+
}
26+
27+
return nil
28+
}
29+
1130
type SeverityRule struct {
1231
BaseRule `mapstructure:",squash"`
1332
Severity string

pkg/golinters/govet.go

-5
Original file line numberDiff line numberDiff line change
@@ -139,11 +139,6 @@ var (
139139
func NewGovet(settings *config.GovetSettings) *goanalysis.Linter {
140140
var conf map[string]map[string]any
141141
if settings != nil {
142-
err := settings.Validate()
143-
if err != nil {
144-
linterLogger.Fatalf("govet configuration: %v", err)
145-
}
146-
147142
conf = settings.Settings
148143
}
149144

0 commit comments

Comments
 (0)