github.com/lineaje-labs/syft@v0.98.1-0.20231227153149-9e393f60ff1b/syft/pkg/cataloger/dart/package.go (about)

     1  package dart
     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  func newPubspecLockPackage(name string, raw pubspecLockPackage, locations ...file.Location) pkg.Package {
    10  	metadata := pkg.DartPubspecLockEntry{
    11  		Name:      name,
    12  		Version:   raw.Version,
    13  		HostedURL: raw.getHostedURL(),
    14  		VcsURL:    raw.getVcsURL(),
    15  	}
    16  
    17  	p := pkg.Package{
    18  		Name:      name,
    19  		Version:   raw.Version,
    20  		Locations: file.NewLocationSet(locations...),
    21  		PURL:      packageURL(metadata),
    22  		Language:  pkg.Dart,
    23  		Type:      pkg.DartPubPkg,
    24  		Metadata:  metadata,
    25  	}
    26  
    27  	p.SetID()
    28  
    29  	return p
    30  }
    31  
    32  func packageURL(m pkg.DartPubspecLockEntry) string {
    33  	var qualifiers packageurl.Qualifiers
    34  
    35  	if m.HostedURL != "" {
    36  		qualifiers = append(qualifiers, packageurl.Qualifier{
    37  			Key:   "hosted_url",
    38  			Value: m.HostedURL,
    39  		})
    40  	} else if m.VcsURL != "" { // Default to using Hosted if somehow both are provided
    41  		qualifiers = append(qualifiers, packageurl.Qualifier{
    42  			Key:   "vcs_url",
    43  			Value: m.VcsURL,
    44  		})
    45  	}
    46  
    47  	return packageurl.NewPackageURL(
    48  		packageurl.TypePub,
    49  		"",
    50  		m.Name,
    51  		m.Version,
    52  		qualifiers,
    53  		"",
    54  	).ToString()
    55  }