mirror of
https://github.com/anchore/syft.git
synced 2025-11-18 00:43:20 +01:00
50 lines
1.4 KiB
Go
50 lines
1.4 KiB
Go
/*
|
|
Package golang provides a concrete Cataloger implementation for go.mod files.
|
|
*/
|
|
package golang
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/anchore/syft/syft/file"
|
|
|
|
"github.com/anchore/syft/internal"
|
|
|
|
"github.com/anchore/syft/internal/log"
|
|
"github.com/anchore/syft/syft/artifact"
|
|
"github.com/anchore/syft/syft/pkg"
|
|
)
|
|
|
|
type Cataloger struct{}
|
|
|
|
// NewGoModuleBinaryCataloger returns a new Golang cataloger object.
|
|
func NewGoModuleBinaryCataloger() *Cataloger {
|
|
return &Cataloger{}
|
|
}
|
|
|
|
// Catalog is given an object to resolve file references and content, this function returns any discovered Packages after analyzing rpm db installation.
|
|
func (c *Cataloger) Catalog(resolver file.Resolver) ([]pkg.Package, []artifact.Relationship, error) {
|
|
var pkgs []pkg.Package
|
|
|
|
fileMatches, err := resolver.FilesByMIMEType(internal.ExecutableMIMETypeSet.List()...)
|
|
if err != nil {
|
|
return pkgs, nil, fmt.Errorf("failed to find bin by mime types: %w", err)
|
|
}
|
|
|
|
for _, location := range fileMatches {
|
|
r, err := resolver.FileContentsByLocation(location)
|
|
if err != nil {
|
|
return pkgs, nil, fmt.Errorf("failed to resolve file contents by location=%q: %w", location.RealPath, err)
|
|
}
|
|
|
|
goPkgs, err := parseGoBin(location, r, openExe)
|
|
if err != nil {
|
|
log.Warnf("could not parse possible go binary at %q: %+v", location.RealPath, err)
|
|
}
|
|
|
|
internal.CloseAndLogError(r, location.RealPath)
|
|
pkgs = append(pkgs, goPkgs...)
|
|
}
|
|
|
|
return pkgs, nil, nil
|
|
}
|