Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: lint errors.Join #7845

Merged
merged 2 commits into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading