github.com/noqcks/syft@v0.0.0-20230920222752-a9e2c4e288e5/syft/pkg/cataloger/apkdb/package.go (about)

     1  package apkdb
     2  
     3  import (
     4  	"strings"
     5  
     6  	"github.com/anchore/packageurl-go"
     7  	"github.com/anchore/syft/syft/file"
     8  	"github.com/anchore/syft/syft/license"
     9  	"github.com/anchore/syft/syft/linux"
    10  	"github.com/anchore/syft/syft/pkg"
    11  )
    12  
    13  func newPackage(d parsedData, release *linux.Release, dbLocation file.Location) pkg.Package {
    14  	// check if license is a valid spdx expression before splitting
    15  	licenseStrings := []string{d.License}
    16  	_, err := license.ParseExpression(d.License)
    17  	if err != nil {
    18  		// invalid so update to split on space
    19  		licenseStrings = strings.Split(d.License, " ")
    20  	}
    21  
    22  	p := pkg.Package{
    23  		Name:         d.Package,
    24  		Version:      d.Version,
    25  		Locations:    file.NewLocationSet(dbLocation.WithAnnotation(pkg.EvidenceAnnotationKey, pkg.PrimaryEvidenceAnnotation)),
    26  		Licenses:     pkg.NewLicenseSet(pkg.NewLicensesFromLocation(dbLocation, licenseStrings...)...),
    27  		PURL:         packageURL(d.ApkMetadata, release),
    28  		Type:         pkg.ApkPkg,
    29  		MetadataType: pkg.ApkMetadataType,
    30  		Metadata:     d.ApkMetadata,
    31  	}
    32  
    33  	p.SetID()
    34  
    35  	return p
    36  }
    37  
    38  // packageURL returns the PURL for the specific Alpine package (see https://github.com/package-url/purl-spec)
    39  func packageURL(m pkg.ApkMetadata, distro *linux.Release) string {
    40  	if distro == nil {
    41  		return ""
    42  	}
    43  
    44  	qualifiers := map[string]string{
    45  		pkg.PURLQualifierArch: m.Architecture,
    46  	}
    47  
    48  	if m.OriginPackage != m.Package {
    49  		qualifiers[pkg.PURLQualifierUpstream] = m.OriginPackage
    50  	}
    51  
    52  	return packageurl.NewPackageURL(
    53  		packageurl.TypeAlpine,
    54  		strings.ToLower(distro.ID),
    55  		m.Package,
    56  		m.Version,
    57  		pkg.PURLQualifiers(
    58  			qualifiers,
    59  			distro,
    60  		),
    61  		"",
    62  	).ToString()
    63  }