Skip to content

Commit

Permalink
refactor: replace packages
Browse files Browse the repository at this point in the history
Signed-off-by: knqyf263 <knqyf263@gmail.com>
  • Loading branch information
knqyf263 committed Jun 19, 2024
1 parent 28463fb commit cbfa0c3
Show file tree
Hide file tree
Showing 71 changed files with 142 additions and 120 deletions.
4 changes: 1 addition & 3 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@ linters-settings:
main:
list-mode: lax
deny:
- pkg: "io/ioutil"
desc: "Replaced by 'io' and 'os' packages since Go 1.16: https://tip.golang.org/doc/go1.16#ioutil"
# Cannot use gomodguard, which examines go.mod, as "golang.org/x/exp/slices" is not a module and doesn't appear in go.mod.
- pkg: "golang.org/x/exp/slices"
desc: "Use 'slices' instead"
- pkg: "golang.org/x/exp/maps"
desc: "Use 'maps' instead"
desc: "Use 'maps' or 'github.com/samber/lo' instead"
dupl:
threshold: 100
errcheck:
Expand Down
2 changes: 1 addition & 1 deletion pkg/cloud/aws/commands/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package commands
import (
"context"
"errors"
"slices"
"sort"
"strings"

"github.com/aws/aws-sdk-go-v2/service/sts"
"golang.org/x/exp/slices"
"golang.org/x/xerrors"

"github.com/aquasecurity/trivy-aws/pkg/errs"
Expand Down
2 changes: 1 addition & 1 deletion pkg/commands/artifact/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import (
"context"
"errors"
"fmt"
"slices"

"github.com/hashicorp/go-multierror"
"github.com/samber/lo"
"github.com/spf13/viper"
"golang.org/x/exp/slices"
"golang.org/x/xerrors"

"github.com/aquasecurity/go-version/pkg/semver"
Expand Down
4 changes: 2 additions & 2 deletions pkg/compliance/spec/compliance.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"os"
"strings"

"golang.org/x/exp/maps"
"github.com/samber/lo"
"golang.org/x/xerrors"
"gopkg.in/yaml.v3"

Expand Down Expand Up @@ -39,7 +39,7 @@ func (cs *ComplianceSpec) Scanners() (types.Scanners, error) {
scannerTypes[scannerType] = struct{}{}
}
}
return maps.Keys(scannerTypes), nil
return lo.Keys(scannerTypes), nil
}

// CheckIDs return list of compliance check IDs
Expand Down
2 changes: 1 addition & 1 deletion pkg/compliance/spec/mapper.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package spec

import (
"golang.org/x/exp/slices"
"slices"

"github.com/aquasecurity/trivy/pkg/types"
)
Expand Down
2 changes: 1 addition & 1 deletion pkg/dependency/parser/c/conan/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package conan

import (
"io"
"slices"
"strings"

"github.com/liamg/jfather"
"github.com/samber/lo"
"golang.org/x/exp/slices"
"golang.org/x/xerrors"

"github.com/aquasecurity/trivy/pkg/dependency"
Expand Down
3 changes: 1 addition & 2 deletions pkg/dependency/parser/golang/mod/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"strings"

"github.com/samber/lo"
"golang.org/x/exp/maps"
"golang.org/x/mod/modfile"
"golang.org/x/xerrors"

Expand Down Expand Up @@ -148,7 +147,7 @@ func (p *Parser) Parse(r xio.ReadSeekerAt) ([]ftypes.Package, []ftypes.Dependenc
}
}

return maps.Values(pkgs), nil, nil
return lo.Values(pkgs), nil, nil
}

// Check if the Go version is less than 1.17
Expand Down
2 changes: 1 addition & 1 deletion pkg/dependency/parser/java/pom/artifact.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import (
"fmt"
"os"
"regexp"
"slices"
"strings"

"github.com/samber/lo"
"golang.org/x/exp/slices"

ftypes "github.com/aquasecurity/trivy/pkg/fanal/types"
"github.com/aquasecurity/trivy/pkg/log"
Expand Down
4 changes: 2 additions & 2 deletions pkg/dependency/parser/julia/manifest/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"sort"

"github.com/BurntSushi/toml"
"golang.org/x/exp/maps"
"github.com/samber/lo"
"golang.org/x/xerrors"

ftypes "github.com/aquasecurity/trivy/pkg/fanal/types"
Expand Down Expand Up @@ -156,7 +156,7 @@ func decodeDependency(man *primitiveManifest, dep primitiveDependency, metadata
var possibleDepsMap map[string]string
err = metadata.PrimitiveDecode(dep.Dependencies, &possibleDepsMap)
if err == nil {
possibleUuids := maps.Values(possibleDepsMap)
possibleUuids := lo.Values(possibleDepsMap)
sort.Strings(possibleUuids)
dep.DependsOn = possibleUuids
return dep, nil
Expand Down
4 changes: 2 additions & 2 deletions pkg/dependency/parser/nodejs/npm/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ package npm
import (
"fmt"
"io"
"maps"
"path"
"slices"
"sort"
"strings"

"github.com/liamg/jfather"
"github.com/samber/lo"
"golang.org/x/exp/maps"
"golang.org/x/xerrors"

"github.com/aquasecurity/trivy/pkg/dependency"
Expand Down Expand Up @@ -186,7 +186,7 @@ func (p *Parser) parseV2(packages map[string]Package) ([]ftypes.Package, []ftype

}

return maps.Values(pkgs), deps
return lo.Values(pkgs), deps
}

// for local package npm uses links. e.g.:
Expand Down
3 changes: 1 addition & 2 deletions pkg/dependency/parser/nodejs/pnpm/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"strings"

"github.com/samber/lo"
"golang.org/x/exp/maps"
"golang.org/x/xerrors"
"gopkg.in/yaml.v3"

Expand Down Expand Up @@ -216,7 +215,7 @@ func (p *Parser) parseV9(lockFile LockFile) ([]ftypes.Package, []ftypes.Dependen
}
}

return maps.Values(resolvedPkgs), maps.Values(resolvedDeps)
return lo.Values(resolvedPkgs), lo.Values(resolvedDeps)
}

// markRootPkgs sets `Dev` to false for non dev dependency.
Expand Down
4 changes: 2 additions & 2 deletions pkg/dependency/parser/php/composer/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"strings"

"github.com/liamg/jfather"
"golang.org/x/exp/maps"
"github.com/samber/lo"
"golang.org/x/xerrors"

"github.com/aquasecurity/trivy/pkg/dependency"
Expand Down Expand Up @@ -98,7 +98,7 @@ func (p *Parser) Parse(r xio.ReadSeekerAt) ([]ftypes.Package, []ftypes.Dependenc
})
}

pkgSlice := maps.Values(pkgs)
pkgSlice := lo.Values(pkgs)
sort.Sort(ftypes.Packages(pkgSlice))
sort.Sort(deps)

Expand Down
4 changes: 2 additions & 2 deletions pkg/dependency/parser/ruby/bundler/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"sort"
"strings"

"golang.org/x/exp/maps"
"github.com/samber/lo"
"golang.org/x/xerrors"

"github.com/aquasecurity/trivy/pkg/dependency"
Expand Down Expand Up @@ -103,7 +103,7 @@ func (p *Parser) Parse(r xio.ReadSeekerAt) ([]ftypes.Package, []ftypes.Dependenc
return nil, nil, xerrors.Errorf("scan error: %w", err)
}

pkgSlice := maps.Values(pkgs)
pkgSlice := lo.Values(pkgs)
sort.Sort(ftypes.Packages(pkgSlice))
return pkgSlice, deps, nil
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/dependency/parser/swift/cocoapods/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"sort"
"strings"

"golang.org/x/exp/maps"
"github.com/samber/lo"
"golang.org/x/xerrors"
"gopkg.in/yaml.v3"

Expand Down Expand Up @@ -86,7 +86,7 @@ func (p *Parser) Parse(r xio.ReadSeekerAt) ([]ftypes.Package, []ftypes.Dependenc
}

sort.Sort(deps)
return utils.UniquePackages(maps.Values(parsedDeps)), deps, nil
return utils.UniquePackages(lo.Values(parsedDeps)), deps, nil
}

func parseDep(dep string) (ftypes.Package, error) {
Expand Down
5 changes: 3 additions & 2 deletions pkg/dependency/parser/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package utils

import (
"fmt"
"maps"
"sort"

"golang.org/x/exp/maps"
"github.com/samber/lo"

ftypes "github.com/aquasecurity/trivy/pkg/fanal/types"
)
Expand Down Expand Up @@ -48,7 +49,7 @@ func UniquePackages(pkgs []ftypes.Package) []ftypes.Package {
}
}
}
pkgSlice := maps.Values(unique)
pkgSlice := lo.Values(unique)
sort.Sort(ftypes.Packages(pkgSlice))

return pkgSlice
Expand Down
6 changes: 3 additions & 3 deletions pkg/detector/ospkg/redhat/redhat.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ package redhat
import (
"context"
"fmt"
"slices"
"sort"
"strings"
"time"

version "github.com/knqyf263/go-rpm-version"
"golang.org/x/exp/maps"
"golang.org/x/exp/slices"
"github.com/samber/lo"
"golang.org/x/xerrors"

dbTypes "github.com/aquasecurity/trivy-db/pkg/types"
Expand Down Expand Up @@ -176,7 +176,7 @@ func (s *Scanner) detect(osVer string, pkg ftypes.Package) ([]types.DetectedVuln
}
}

vulns := maps.Values(uniqVulns)
vulns := lo.Values(uniqVulns)
sort.Slice(vulns, func(i, j int) bool {
return vulns[i].VulnerabilityID < vulns[j].VulnerabilityID
})
Expand Down
2 changes: 1 addition & 1 deletion pkg/downloader/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package downloader

import (
"context"
"maps"
"os"

getter "github.com/hashicorp/go-getter"
"golang.org/x/exp/maps"
"golang.org/x/xerrors"
)

Expand Down
2 changes: 1 addition & 1 deletion pkg/fanal/analyzer/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import (
"io/fs"
"os"
"regexp"
"slices"
"sort"
"strings"
"sync"

"github.com/samber/lo"
"golang.org/x/exp/slices"
"golang.org/x/sync/semaphore"
"golang.org/x/xerrors"

Expand Down
2 changes: 1 addition & 1 deletion pkg/fanal/analyzer/config_analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package analyzer

import (
"context"
"slices"

v1 "github.com/google/go-containerregistry/pkg/v1"
"golang.org/x/exp/slices"
"golang.org/x/xerrors"

"github.com/aquasecurity/trivy/pkg/fanal/types"
Expand Down
4 changes: 2 additions & 2 deletions pkg/fanal/analyzer/imgconf/apk/apk.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"time"

v1 "github.com/google/go-containerregistry/pkg/v1"
"golang.org/x/exp/maps"
"github.com/samber/lo"
"golang.org/x/xerrors"

"github.com/aquasecurity/trivy/pkg/fanal/analyzer"
Expand Down Expand Up @@ -138,7 +138,7 @@ func (a alpineCmdAnalyzer) parseConfig(apkIndexArchive *apkIndex, config *v1.Con
}
}

return maps.Values(uniqPkgs)
return lo.Values(uniqPkgs)
}

func (a alpineCmdAnalyzer) parseCommand(command string, envs map[string]string) (pkgs []string) {
Expand Down
3 changes: 1 addition & 2 deletions pkg/fanal/analyzer/language/dart/pub/pubspec.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"sort"

"github.com/samber/lo"
"golang.org/x/exp/maps"
"golang.org/x/xerrors"
"gopkg.in/yaml.v3"

Expand Down Expand Up @@ -166,7 +165,7 @@ func parsePubSpecYaml(r io.Reader) (string, []string, error) {

// pubspec.yaml uses version ranges
// save only dependencies names
dependsOn := maps.Keys(spec.Dependencies)
dependsOn := lo.Keys(spec.Dependencies)

return dependency.ID(types.Pub, spec.Name, spec.Version), dependsOn, nil
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/fanal/analyzer/language/dotnet/nuget/nuget.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import (
"io/fs"
"os"
"path/filepath"
"slices"
"sort"

"golang.org/x/exp/slices"
"golang.org/x/xerrors"

"github.com/aquasecurity/trivy/pkg/dependency/parser/nuget/config"
Expand Down
5 changes: 2 additions & 3 deletions pkg/fanal/analyzer/language/golang/mod/mod.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@ import (
"os"
"path/filepath"
"regexp"
"slices"
"unicode"

"github.com/samber/lo"
"golang.org/x/exp/maps"
"golang.org/x/exp/slices"
"golang.org/x/xerrors"

"github.com/aquasecurity/trivy/pkg/dependency/parser/golang/mod"
Expand Down Expand Up @@ -262,7 +261,7 @@ func mergeGoSum(gomod, gosum *types.Application) {
uniq[lib.Name] = lib
}

gomod.Packages = maps.Values(uniq)
gomod.Packages = lo.Values(uniq)
}

func findLicense(dir string, classifierConfidenceLevel float64) ([]string, error) {
Expand Down
5 changes: 2 additions & 3 deletions pkg/fanal/analyzer/language/julia/pkg/pkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@ import (
"io/fs"
"os"
"path/filepath"
"slices"
"sort"

"github.com/BurntSushi/toml"
"github.com/samber/lo"
"golang.org/x/exp/maps"
"golang.org/x/exp/slices"
"golang.org/x/xerrors"

julia "github.com/aquasecurity/trivy/pkg/dependency/parser/julia/manifest"
Expand Down Expand Up @@ -167,7 +166,7 @@ func walkDependencies(directDeps map[string]string, allPackages types.Packages,
walkIndirectDependencies(pkg, pkgsByID, visited)
}

return maps.Values(visited)
return lo.Values(visited)
}

// Marks all indirect dependencies as indirect. Starts from `rootPkg`. Visited deps are added to `visited`.
Expand Down
Loading

0 comments on commit cbfa0c3

Please sign in to comment.