Skip to content

Commit

Permalink
chore: lint errors.Join (#7845)
Browse files Browse the repository at this point in the history
Signed-off-by: knqyf263 <[email protected]>
  • Loading branch information
knqyf263 authored Oct 31, 2024
1 parent 5e78b6c commit 7632625
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 16 deletions.
10 changes: 10 additions & 0 deletions misc/lint/rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,13 @@ func initializeMaps(m dsl.Matcher) {
Suggest(`make(map[$key]$value)`).
Report(`replace '$$' with 'make(map[$key]$value)`)
}

// While errors.Join from standard library can combine multiple errors,
// we use hashicorp/go-multierror for more user-friendly error outputs.
func errorsJoin(m dsl.Matcher) {
m.Match(`errors.Join($x...)`).
Report("use github.com/hashicorp/go-multierror.Append instead of errors.Join.")

m.Match(`errors.Join($*args)`).
Report("use github.com/hashicorp/go-multierror.Append instead of errors.Join.")
}
11 changes: 5 additions & 6 deletions pkg/iac/scanners/cloudformation/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ package parser
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"io/fs"
"path/filepath"
"strings"

"github.com/hashicorp/go-multierror"
"github.com/liamg/jfather"
"gopkg.in/yaml.v3"

Expand Down Expand Up @@ -171,18 +171,17 @@ func (p *Parser) parseParams() error {

params := make(Parameters)

var errs []error

var errs error
for _, path := range p.parameterFiles {
if parameters, err := p.parseParametersFile(path); err != nil {
errs = append(errs, err)
errs = multierror.Append(errs, err)
} else {
params.Merge(parameters)
}
}

if len(errs) != 0 {
return errors.Join(errs...)
if errs != nil {
return errs
}

params.Merge(p.parameters)
Expand Down
15 changes: 8 additions & 7 deletions pkg/iac/terraform/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strings"

"github.com/google/uuid"
"github.com/hashicorp/go-multierror"
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/hclsyntax"
"github.com/zclconf/go-cty/cty"
Expand Down Expand Up @@ -600,14 +601,14 @@ func (b *Block) IsNotNil() bool {
func (b *Block) ExpandBlock() error {
var (
expanded []*Block
errs []error
errs error
)

for _, child := range b.childBlocks {
if child.Type() == "dynamic" {
blocks, err := child.expandDynamic()
if err != nil {
errs = append(errs, err)
errs = multierror.Append(errs, err)
continue
}
expanded = append(expanded, blocks...)
Expand All @@ -618,7 +619,7 @@ func (b *Block) ExpandBlock() error {
b.injectBlock(block)
}

return errors.Join(errs...)
return errs
}

func (b *Block) expandDynamic() ([]*Block, error) {
Expand All @@ -638,7 +639,7 @@ func (b *Block) expandDynamic() ([]*Block, error) {

var (
expanded []*Block
errs []error
errs error
)

forEachVal.ForEachElement(func(key, val cty.Value) (stop bool) {
Expand All @@ -648,7 +649,7 @@ func (b *Block) expandDynamic() ([]*Block, error) {

iteratorName, err := b.iteratorName(realBlockType)
if err != nil {
errs = append(errs, err)
errs = multierror.Append(errs, err)
return
}

Expand All @@ -664,7 +665,7 @@ func (b *Block) expandDynamic() ([]*Block, error) {
inherited.hclBlock.Labels = []string{}
inherited.hclBlock.Type = realBlockType
if err := inherited.ExpandBlock(); err != nil {
errs = append(errs, err)
errs = multierror.Append(errs, err)
return
}
expanded = append(expanded, inherited)
Expand All @@ -676,7 +677,7 @@ func (b *Block) expandDynamic() ([]*Block, error) {
b.markExpanded()
}

return expanded, errors.Join(errs...)
return expanded, errs
}

func (b *Block) validateForEach() (cty.Value, error) {
Expand Down
4 changes: 2 additions & 2 deletions pkg/report/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package report

import (
"context"
"errors"
"io"
"strings"

"github.com/hashicorp/go-multierror"
"golang.org/x/xerrors"

cr "github.com/aquasecurity/trivy/pkg/compliance/report"
Expand All @@ -32,7 +32,7 @@ func Write(ctx context.Context, report types.Report, option flag.Options) (err e
}
defer func() {
if cerr := cleanup(); cerr != nil {
err = errors.Join(err, cerr)
err = multierror.Append(err, cerr)
}
}()

Expand Down
3 changes: 2 additions & 1 deletion pkg/vex/repo/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"time"

"github.com/hashicorp/go-getter"
"github.com/hashicorp/go-multierror"
"github.com/samber/lo"
"golang.org/x/xerrors"

Expand Down Expand Up @@ -256,7 +257,7 @@ func (r *Repository) download(ctx context.Context, ver Version, dst string, opts
etag = etags[loc.URL] // Keep the old ETag
// Update last updated time so that Trivy will not try to download the same URL soon
case err != nil:
errs = errors.Join(errs, err)
errs = multierror.Append(errs, err)
continue // Try the next location
default:
// Successfully downloaded
Expand Down

0 comments on commit 7632625

Please sign in to comment.