chore: lint errors.Join (#7845)

Signed-off-by: knqyf263 <knqyf263@gmail.com>
This commit is contained in:
Teppei Fukuda
2024-10-31 16:08:47 +04:00
committed by GitHub
parent 5e78b6c12f
commit 7632625be2
5 changed files with 27 additions and 16 deletions

View File

@@ -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.")
}

View File

@@ -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"
@@ -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)

View File

@@ -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"
@@ -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...)
@@ -618,7 +619,7 @@ func (b *Block) ExpandBlock() error {
b.injectBlock(block)
}
return errors.Join(errs...)
return errs
}
func (b *Block) expandDynamic() ([]*Block, error) {
@@ -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) {
@@ -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
}
@@ -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)
@@ -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) {

View File

@@ -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"
@@ -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)
}
}()

View File

@@ -11,6 +11,7 @@ import (
"time"
"github.com/hashicorp/go-getter"
"github.com/hashicorp/go-multierror"
"github.com/samber/lo"
"golang.org/x/xerrors"
@@ -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