Skip to content

Commit

Permalink
Enable exhaustruct linter
Browse files Browse the repository at this point in the history
  • Loading branch information
prymitive committed Jan 3, 2025
1 parent f8ebbcd commit bde8ee0
Show file tree
Hide file tree
Showing 54 changed files with 214 additions and 209 deletions.
25 changes: 25 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,14 @@ linters:
- exptostd
- nilnesserr
- usetesting
- exhaustruct

issues:
max-same-issues: 0
exclude-rules:
- path: ^.*_test\.go$
linters:
- exhaustruct

linters-settings:
goimports:
Expand All @@ -40,3 +45,23 @@ linters-settings:
enable-all: true
nakedret:
max-func-lines: 0
exhaustruct:
exclude:
- net/http\..+
- crypto/tls\..+
- encoding/xml\..+
- .+/checks\.badMatcher
- .+/checks\.Problem
- .+/checks\.exprProblem
- .+/checks\..+Settings
- .+/config\.Match
- .+/discovery\.Entry
- .+/parser\.Rule
- .+/parser\.ParseError
- .+/parser\.VectorSelector
- .+/promapi\.MetricTimeRange
- github.com/prometheus/.+\..+
- github.com/urfave/cli/.+
- gitlab.com/gitlab-org/api/.+
- github.com/google/go-github/.+
- github.com/hashicorp/hcl/.+
27 changes: 1 addition & 26 deletions cmd/pint/ci.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,45 +285,20 @@ func logSeverityCounters(src map[checks.Severity]int) (attrs []any) {
}

func detectCI(cfg *config.CI) *config.CI {
var isNil, isDirty bool

if cfg == nil {
isNil = true
cfg = &config.CI{}
}

if bb := os.Getenv("GITHUB_BASE_REF"); bb != "" {
isDirty = true
cfg.BaseBranch = bb
slog.Debug("got base branch from GITHUB_BASE_REF env variable", slog.String("branch", bb))
}

if isNil && !isDirty {
return nil
}
return cfg
}

func detectRepository(cfg *config.Repository) *config.Repository {
var isNil, isDirty bool

if cfg == nil {
isNil = true
cfg = &config.Repository{}
}

if os.Getenv("GITHUB_ACTION") != "" {
isDirty = true
cfg.GitHub = detectGithubActions(cfg.GitHub)
}

if cfg != nil && cfg.GitHub != nil && cfg.GitHub.MaxComments == 0 {
cfg.GitHub.MaxComments = 50
}

if isNil && !isDirty {
return nil
}
return cfg
}

Expand All @@ -342,7 +317,7 @@ func detectGithubActions(gh *config.GitHub) *config.GitHub {

if gh == nil {
isNil = true
gh = &config.GitHub{Timeout: time.Minute.String()}
gh = &config.GitHub{Timeout: time.Minute.String()} // nolint:exhaustruct
}

if repo := os.Getenv("GITHUB_REPOSITORY"); repo != "" {
Expand Down
2 changes: 2 additions & 0 deletions cmd/pint/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ func verifyOwners(entries []discovery.Entry, allowedOwners []*regexp.Regexp) (re
Path: entry.Path,
ModifiedLines: entry.ModifiedLines,
Rule: entry.Rule,
Owner: "",
Problem: checks.Problem{
Lines: entry.Rule.Lines,
Reporter: discovery.RuleOwnerComment,
Expand All @@ -211,6 +212,7 @@ func verifyOwners(entries []discovery.Entry, allowedOwners []*regexp.Regexp) (re
Path: entry.Path,
ModifiedLines: entry.ModifiedLines,
Rule: entry.Rule,
Owner: "",
Problem: checks.Problem{
Lines: entry.Rule.Lines,
Reporter: discovery.RuleOwnerComment,
Expand Down
2 changes: 1 addition & 1 deletion cmd/pint/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func checkRules(ctx context.Context, workers int, isOffline bool, gen *config.Pr
checkList := cfg.GetChecksForEntry(ctx, gen, entry)
for _, check := range checkList {
checkIterationChecks.Inc()
if check.Meta().IsOnline {
if check.Meta().Online {
onlineChecksCount.Inc()
} else {
offlineChecksCount.Inc()
Expand Down
1 change: 0 additions & 1 deletion cmd/pint/tests/0083_github_action.txt
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,3 @@ groups:

-- src/.pint.hcl --
repository {}

11 changes: 9 additions & 2 deletions cmd/pint/tests/0122_lint_owner_allowed.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ cmp stderr stderr.txt
-- stderr.txt --
level=INFO msg="Loading configuration file" path=.pint.hcl
level=INFO msg="Finding all rules to check" paths=["rules"]
level=INFO msg="Checking Prometheus rules" entries=5 workers=10 online=true
level=INFO msg="Checking Prometheus rules" entries=6 workers=10 online=true
rules/1.yml:4-5 Bug: `rule/owner` comments are required in all files, please add a `# pint file/owner $owner` somewhere in this file and/or `# pint rule/owner $owner` on top of each rule. (rule/owner)
4 | - alert: No Owner
5 | expr: up > 0
Expand All @@ -14,14 +14,18 @@ rules/1.yml:7-8 Bug: This rule is set as owned by `bob` but `bob` doesn't match
7 | - alert: Invalid
8 | expr: up == 0

rules/1.yml:13-14 Bug: This rule is set as owned by `zed` but `zed` doesn't match any of the allowed owner values. (rule/owner)
13 | - alert: Owner Zed
14 | expr: up < 0

rules/2.yml:4-5 Bug: `rule/owner` comments are required in all files, please add a `# pint file/owner $owner` somewhere in this file and/or `# pint rule/owner $owner` on top of each rule. (rule/owner)
4 | - alert: No Owner
5 | expr: up > 0

rules/3.yml:1 Bug: This file is set as owned by `ax` but `ax` doesn't match any of the allowed owner values. (rule/owner)
1 | # pint file/owner ax

level=INFO msg="Problems found" Bug=4
level=INFO msg="Problems found" Bug=5
level=ERROR msg="Fatal error" err="found 1 problem(s) with severity Bug or higher"
-- rules/1.yml --
groups:
Expand All @@ -35,6 +39,9 @@ groups:
# pint rule/owner alice
- alert: Owner Alice
expr: up > 0
# pint rule/owner zed
- alert: Owner Zed
expr: up < 0

-- rules/2.yml --
groups:
Expand Down
2 changes: 1 addition & 1 deletion cmd/pint/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ type problemCollector struct {
}

func newProblemCollector(cfg config.Config, f pathFinderFunc, minSeverity checks.Severity, maxProblems int) *problemCollector {
return &problemCollector{
return &problemCollector{ // nolint:exhaustruct
finder: f,
cfg: cfg,
fileOwners: map[string]string{},
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/cespare/xxhash/v2 v2.3.0
github.com/gkampitakis/go-snaps v0.5.8
github.com/google/go-cmp v0.6.0
github.com/google/go-github/v63 v63.0.0
github.com/google/go-github/v68 v68.0.0
github.com/hashicorp/hcl/v2 v2.23.0
github.com/klauspost/compress v1.17.11
github.com/neilotoole/slogt v1.1.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ github.com/golang/snappy v0.0.4/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEW
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/go-github/v63 v63.0.0 h1:13xwK/wk9alSokujB9lJkuzdmQuVn2QCPeck76wR3nE=
github.com/google/go-github/v63 v63.0.0/go.mod h1:IqbcrgUmIcEaioWrGYei/09o+ge5vhffGOcxrO0AfmA=
github.com/google/go-github/v68 v68.0.0 h1:ZW57zeNZiXTdQ16qrDiZ0k6XucrxZ2CGmoTvcCyQG6s=
github.com/google/go-github/v68 v68.0.0/go.mod h1:K9HAUBovM2sLwM408A18h+wd9vqdLOEqTUCbnRIcx68=
github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8=
github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
Expand Down
3 changes: 2 additions & 1 deletion internal/checks/alerts_absent.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ func (c AlertsAbsentCheck) Meta() CheckMeta {
discovery.Modified,
discovery.Moved,
},
IsOnline: true,
Online: true,
AlwaysEnabled: false,
}
}

Expand Down
3 changes: 2 additions & 1 deletion internal/checks/alerts_annotation.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ func (c AnnotationCheck) Meta() CheckMeta {
discovery.Modified,
discovery.Moved,
},
IsOnline: false,
Online: false,
AlwaysEnabled: false,
}
}

Expand Down
3 changes: 2 additions & 1 deletion internal/checks/alerts_comparison.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ func (c ComparisonCheck) Meta() CheckMeta {
discovery.Modified,
discovery.Moved,
},
IsOnline: false,
Online: false,
AlwaysEnabled: false,
}
}

Expand Down
3 changes: 2 additions & 1 deletion internal/checks/alerts_count.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ func (c AlertsCheck) Meta() CheckMeta {
discovery.Modified,
discovery.Moved,
},
IsOnline: true,
Online: true,
AlwaysEnabled: false,
}
}

Expand Down
3 changes: 2 additions & 1 deletion internal/checks/alerts_external_labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ func (c AlertsExternalLabelsCheck) Meta() CheckMeta {
discovery.Modified,
discovery.Moved,
},
IsOnline: true,
Online: true,
AlwaysEnabled: false,
}
}

Expand Down
3 changes: 2 additions & 1 deletion internal/checks/alerts_for.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ func (c AlertsForChecksFor) Meta() CheckMeta {
discovery.Modified,
discovery.Moved,
},
IsOnline: false,
Online: false,
AlwaysEnabled: false,
}
}

Expand Down
3 changes: 2 additions & 1 deletion internal/checks/alerts_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ func (c TemplateCheck) Meta() CheckMeta {
discovery.Modified,
discovery.Moved,
},
IsOnline: false,
Online: false,
AlwaysEnabled: false,
}
}

Expand Down
2 changes: 1 addition & 1 deletion internal/checks/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ type Problem struct {

type CheckMeta struct {
States []discovery.ChangeType
IsOnline bool
Online bool
AlwaysEnabled bool
}

Expand Down
2 changes: 1 addition & 1 deletion internal/checks/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (c ErrorCheck) Meta() CheckMeta {
discovery.Moved,
discovery.Removed,
},
IsOnline: false,
Online: false,
AlwaysEnabled: true,
}
}
Expand Down
3 changes: 2 additions & 1 deletion internal/checks/labels_conflict.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ func (c LabelsConflictCheck) Meta() CheckMeta {
discovery.Modified,
discovery.Moved,
},
IsOnline: true,
Online: true,
AlwaysEnabled: false,
}
}

Expand Down
3 changes: 2 additions & 1 deletion internal/checks/promql_aggregation.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ func (c AggregationCheck) Meta() CheckMeta {
discovery.Modified,
discovery.Moved,
},
IsOnline: false,
Online: false,
AlwaysEnabled: false,
}
}

Expand Down
3 changes: 2 additions & 1 deletion internal/checks/promql_counter.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ func (c CounterCheck) Meta() CheckMeta {
discovery.Modified,
discovery.Moved,
},
IsOnline: true,
Online: true,
AlwaysEnabled: false,
}
}

Expand Down
3 changes: 2 additions & 1 deletion internal/checks/promql_fragile.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ func (c FragileCheck) Meta() CheckMeta {
discovery.Modified,
discovery.Moved,
},
IsOnline: false,
Online: false,
AlwaysEnabled: false,
}
}

Expand Down
3 changes: 2 additions & 1 deletion internal/checks/promql_range_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ func (c RangeQueryCheck) Meta() CheckMeta {
discovery.Modified,
discovery.Moved,
},
IsOnline: true,
Online: true,
AlwaysEnabled: false,
}
}

Expand Down
5 changes: 3 additions & 2 deletions internal/checks/promql_rate.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ func (c RateCheck) Meta() CheckMeta {
discovery.Modified,
discovery.Moved,
},
IsOnline: true,
Online: true,
AlwaysEnabled: false,
}
}

Expand Down Expand Up @@ -78,7 +79,7 @@ func (c RateCheck) Check(ctx context.Context, _ discovery.Path, rule parser.Rule
return problems
}

done := &completedList{}
done := &completedList{values: nil}
for _, problem := range c.checkNode(ctx, expr.Query, entries, cfg, done) {
problems = append(problems, Problem{
Lines: expr.Value.Lines,
Expand Down
3 changes: 2 additions & 1 deletion internal/checks/promql_regexp.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ func (c RegexpCheck) Meta() CheckMeta {
discovery.Modified,
discovery.Moved,
},
IsOnline: false,
Online: false,
AlwaysEnabled: false,
}
}

Expand Down
7 changes: 5 additions & 2 deletions internal/checks/promql_series.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@ func (c SeriesCheck) Meta() CheckMeta {
discovery.Modified,
discovery.Moved,
},
IsOnline: true,
Online: true,
AlwaysEnabled: false,
}
}

Expand Down Expand Up @@ -245,7 +246,8 @@ func (c SeriesCheck) Check(ctx context.Context, _ discovery.Path, rule parser.Ru
slog.String("name", c.prom.Name()),
slog.String("metric", c.prom.UptimeMetric()),
)
promUptime = &promapi.RangeQueryResult{
promUptime = &promapi.RangeQueryResult{ // nolint:exhaustruct
URI: c.prom.URI(),
Series: promapi.SeriesTimeRanges{
From: params.Start(),
Until: params.End(),
Expand All @@ -258,6 +260,7 @@ func (c SeriesCheck) Check(ctx context.Context, _ discovery.Path, rule parser.Ru
End: params.End(),
},
},
Gaps: nil,
},
}
}
Expand Down
3 changes: 2 additions & 1 deletion internal/checks/promql_syntax.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ func (c SyntaxCheck) Meta() CheckMeta {
discovery.Modified,
discovery.Moved,
},
IsOnline: false,
Online: false,
AlwaysEnabled: false,
}
}

Expand Down
Loading

0 comments on commit bde8ee0

Please sign in to comment.