Skip to content

Commit

Permalink
Merge pull request cloudflare#456 from cloudflare/shadow
Browse files Browse the repository at this point in the history
Enable shadow checks in govet
  • Loading branch information
prymitive authored Nov 25, 2022
2 parents c867c9a + 445de05 commit 39a7f27
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 21 deletions.
2 changes: 2 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,7 @@ linters-settings:
local-prefixes: github.com/cloudflare/pint
gofumpt:
extra-rules: true
govet:
check-shadowing: true
nakedret:
max-func-lines: 0
12 changes: 6 additions & 6 deletions cmd/pint/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,15 @@ func actionWatch(c *cli.Context) error {
pidfile := c.String(pidfileFlag)
if pidfile != "" {
pid := os.Getpid()
err := os.WriteFile(pidfile, []byte(fmt.Sprintf("%d\n", pid)), 0o644)
err = os.WriteFile(pidfile, []byte(fmt.Sprintf("%d\n", pid)), 0o644)
if err != nil {
return err
}
log.Info().Str("path", pidfile).Msg("Pidfile created")
defer func() {
err := os.RemoveAll(pidfile)
if err != nil {
log.Error().Err(err).Str("path", pidfile).Msg("Failed to remove pidfile")
pidErr := os.RemoveAll(pidfile)
if pidErr != nil {
log.Error().Err(pidErr).Str("path", pidfile).Msg("Failed to remove pidfile")
}
log.Info().Str("path", pidfile).Msg("Pidfile removed")
}()
Expand Down Expand Up @@ -133,8 +133,8 @@ func actionWatch(c *cli.Context) error {
WriteTimeout: time.Second * 30,
}
go func() {
if err := server.ListenAndServe(); !errors.Is(err, http.ErrServerClosed) {
log.Error().Err(err).Str("listen", listen).Msg("HTTP server returned an error")
if httpErr := server.ListenAndServe(); !errors.Is(httpErr, http.ErrServerClosed) {
log.Error().Err(httpErr).Str("listen", listen).Msg("HTTP server returned an error")
}
}()
log.Info().Str("address", listen).Msg("Started HTTP server")
Expand Down
3 changes: 1 addition & 2 deletions internal/checks/alerts_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -482,8 +482,7 @@ func checkMetricLabels(msg, name, text string, metricLabels []string, excludeLab
}
if found == excludeLabels {
if _, ok := done[v[1]]; !ok {
msg := fmt.Sprintf(msg, v[1])
msgs = append(msgs, msg)
msgs = append(msgs, fmt.Sprintf(msg, v[1]))
done[v[1]] = struct{}{}
}
}
Expand Down
4 changes: 2 additions & 2 deletions internal/checks/rule_link.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,10 @@ func (c RuleLinkCheck) Check(ctx context.Context, path string, rule parser.Rule,
log.Debug().Stringer("link", u).Str("uri", uri).Msg("Link URI rewritten by rule")
}

ctx, cancel := context.WithTimeout(ctx, c.timeout)
rctx, cancel := context.WithTimeout(ctx, c.timeout)
defer cancel()

req, _ := http.NewRequestWithContext(ctx, http.MethodGet, uri, nil)
req, _ := http.NewRequestWithContext(rctx, http.MethodGet, uri, nil)

for k, v := range c.headers {
req.Header.Set(k, v)
Expand Down
2 changes: 1 addition & 1 deletion internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ func Load(path string, failOnMissing bool) (cfg Config, err error) {
Rules: []Rule{},
}

if _, err := os.Stat(path); err == nil || failOnMissing {
if _, err = os.Stat(path); err == nil || failOnMissing {
log.Info().Str("path", path).Msg("Loading configuration file")
err = hclsimple.DecodeFile(path, nil, &cfg)
if err != nil {
Expand Down
10 changes: 6 additions & 4 deletions internal/discovery/git_branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func (f GitBranchFinder) Find() (entries []Entry, err error) {
}

pathCommits := map[string]map[string]struct{}{}
var commit string
var commit, msg string

for _, line := range strings.Split(string(out), "\n") {
parts := strings.Split(removeRedundantSpaces(line), " ")
Expand All @@ -71,7 +71,7 @@ func (f GitBranchFinder) Find() (entries []Entry, err error) {
continue
}

msg, err := git.CommitMessage(f.gitCmd, commit)
msg, err = git.CommitMessage(f.gitCmd, commit)
if err != nil {
return nil, fmt.Errorf("failed to get commit message for %s: %w", commit, err)
}
Expand Down Expand Up @@ -106,8 +106,10 @@ func (f GitBranchFinder) Find() (entries []Entry, err error) {
}
}

var lbs git.LineBlames
var els []Entry
for path, commits := range pathCommits {
lbs, err := git.Blame(path, f.gitCmd)
lbs, err = git.Blame(path, f.gitCmd)
if err != nil {
return nil, fmt.Errorf("failed to run git blame for %s: %w", path, err)
}
Expand All @@ -121,7 +123,7 @@ func (f GitBranchFinder) Find() (entries []Entry, err error) {
allowedLines = append(allowedLines, lb.Line)
}

els, err := readFile(path, !matchesAny(f.relaxed, path))
els, err = readFile(path, !matchesAny(f.relaxed, path))
if err != nil {
return nil, err
}
Expand Down
3 changes: 2 additions & 1 deletion internal/parser/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,8 @@ func (r Rule) GetComment(comment ...string) (s Comment, ok bool) {
comments = r.AlertingRule.Comments()
}
for _, c := range comments {
if val, ok := GetLastComment(c, comment...); ok {
var val Comment
if val, ok = GetLastComment(c, comment...); ok {
return val, ok
}
}
Expand Down
12 changes: 7 additions & 5 deletions internal/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,31 +52,33 @@ func parseNode(content []byte, node *yaml.Node, offset int) (rules []Rule, err e
return
}

var rl []Rule
var rule Rule
for _, root := range node.Content {
// nolint: exhaustive
switch root.Kind {
case yaml.SequenceNode:
for _, n := range root.Content {
ret, err := parseNode(content, n, offset)
rl, err = parseNode(content, n, offset)
if err != nil {
return nil, err
}
rules = append(rules, ret...)
rules = append(rules, rl...)
}
case yaml.MappingNode:
rule, isEmpty, err := parseRule(content, root, offset)
rule, isEmpty, err = parseRule(content, root, offset)
if err != nil {
return nil, err
}
if !isEmpty {
rules = append(rules, rule)
} else {
for _, n := range root.Content {
ret, err := parseNode(content, n, offset)
rl, err = parseNode(content, n, offset)
if err != nil {
return nil, err
}
rules = append(rules, ret...)
rules = append(rules, rl...)
}
}
case yaml.ScalarNode:
Expand Down

0 comments on commit 39a7f27

Please sign in to comment.