github.com/anchore/syft@v1.38.2/syft/pkg/cataloger/snap/package.go (about)

     1  package snap
     2  
     3  import (
     4  	"github.com/anchore/packageurl-go"
     5  	"github.com/anchore/syft/syft/file"
     6  	"github.com/anchore/syft/syft/pkg"
     7  )
     8  
     9  // newPackage creates a new Package from snap metadata
    10  func newPackage(name, version string, metadata pkg.SnapEntry, locations ...file.Location) pkg.Package {
    11  	p := pkg.Package{
    12  		Name:      name,
    13  		Version:   version,
    14  		Locations: file.NewLocationSet(locations...),
    15  		PURL:      packageURL(name, version, metadata),
    16  		Type:      pkg.DebPkg, // Use Debian package type for compatibility
    17  		Metadata:  metadata,
    18  	}
    19  
    20  	p.SetID()
    21  
    22  	return p
    23  }
    24  
    25  // packageURL returns the PURL for a snap package
    26  func packageURL(name, version string, metadata pkg.SnapEntry) string {
    27  	var qualifiers packageurl.Qualifiers
    28  
    29  	if metadata.Architecture != "" {
    30  		qualifiers = append(qualifiers, packageurl.Qualifier{
    31  			Key:   "arch",
    32  			Value: metadata.Architecture,
    33  		})
    34  	}
    35  
    36  	if metadata.Base != "" {
    37  		qualifiers = append(qualifiers, packageurl.Qualifier{
    38  			Key:   "base",
    39  			Value: metadata.Base,
    40  		})
    41  	}
    42  
    43  	if metadata.SnapType != "" {
    44  		qualifiers = append(qualifiers, packageurl.Qualifier{
    45  			Key:   "type",
    46  			Value: metadata.SnapType,
    47  		})
    48  	}
    49  
    50  	return packageurl.NewPackageURL(
    51  		packageurl.TypeGeneric,
    52  		"snap",
    53  		name,
    54  		version,
    55  		qualifiers,
    56  		"",
    57  	).ToString()
    58  }
    59  
    60  // newDebianPackageFromSnap creates a Debian-style package entry from snap manifest data
    61  func newDebianPackageFromSnap(name, version string, snapMetadata pkg.SnapEntry, locations ...file.Location) pkg.Package {
    62  	p := pkg.Package{
    63  		Name:      name,
    64  		Version:   version,
    65  		Locations: file.NewLocationSet(locations...),
    66  		Type:      pkg.DebPkg,
    67  		PURL:      debianPackageURL(name, version, snapMetadata.Architecture),
    68  		Metadata:  snapMetadata,
    69  	}
    70  
    71  	p.SetID()
    72  	return p
    73  }
    74  
    75  // debianPackageURL creates a PURL for Debian packages found in snaps
    76  func debianPackageURL(name, version, architecture string) string {
    77  	var qualifiers packageurl.Qualifiers
    78  
    79  	if architecture != "" {
    80  		qualifiers = append(qualifiers, packageurl.Qualifier{
    81  			Key:   "arch",
    82  			Value: architecture,
    83  		})
    84  	}
    85  
    86  	return packageurl.NewPackageURL(
    87  		packageurl.TypeDebian,
    88  		"ubuntu", // Assume Ubuntu since most snaps are built on Ubuntu
    89  		name,
    90  		version,
    91  		qualifiers,
    92  		"",
    93  	).ToString()
    94  }