refactor: unify Library and Package structs (#6633)

Signed-off-by: knqyf263 <knqyf263@gmail.com>
Co-authored-by: DmitriyLewen <91113035+DmitriyLewen@users.noreply.github.com>
Co-authored-by: DmitriyLewen <dmitriy.lewen@smartforce.io>
This commit is contained in:
Teppei Fukuda
2024-05-07 16:25:52 +04:00
committed by GitHub
parent 39ebed45f8
commit 3eecfc6b6e
156 changed files with 3901 additions and 3675 deletions

View File

@@ -6,7 +6,7 @@ import (
"golang.org/x/exp/maps"
"github.com/aquasecurity/trivy/pkg/dependency/types"
ftypes "github.com/aquasecurity/trivy/pkg/fanal/types"
)
func UniqueStrings(ss []string) []string {
@@ -22,36 +22,36 @@ func UniqueStrings(ss []string) []string {
return results
}
func UniqueLibraries(libs []types.Library) []types.Library {
if len(libs) == 0 {
func UniquePackages(pkgs []ftypes.Package) []ftypes.Package {
if len(pkgs) == 0 {
return nil
}
unique := make(map[string]types.Library)
for _, lib := range libs {
identifier := fmt.Sprintf("%s@%s", lib.Name, lib.Version)
unique := make(map[string]ftypes.Package)
for _, pkg := range pkgs {
identifier := fmt.Sprintf("%s@%s", pkg.Name, pkg.Version)
if l, ok := unique[identifier]; !ok {
unique[identifier] = lib
unique[identifier] = pkg
} else {
// There are times when we get 2 same libraries as root and dev dependencies.
// There are times when we get 2 same packages as root and dev dependencies.
// https://github.com/aquasecurity/trivy/issues/5532
// In these cases, we need to mark the dependency as a root dependency.
if !lib.Dev {
l.Dev = lib.Dev
if !pkg.Dev {
l.Dev = pkg.Dev
unique[identifier] = l
}
if len(lib.Locations) > 0 {
if len(pkg.Locations) > 0 {
// merge locations
l.Locations = append(l.Locations, lib.Locations...)
l.Locations = append(l.Locations, pkg.Locations...)
sort.Sort(l.Locations)
unique[identifier] = l
}
}
}
libSlice := maps.Values(unique)
sort.Sort(types.Libraries(libSlice))
pkgSlice := maps.Values(unique)
sort.Sort(ftypes.Packages(pkgSlice))
return libSlice
return pkgSlice
}
func MergeMaps(parent, child map[string]string) map[string]string {