github.com/nextlinux/gosbom@v0.81.1-0.20230627115839-1ff50c281391/gosbom/pkg/cataloger/binary/package.go (about)

     1  package binary
     2  
     3  import (
     4  	"reflect"
     5  
     6  	"github.com/nextlinux/gosbom/gosbom/cpe"
     7  	"github.com/nextlinux/gosbom/gosbom/file"
     8  	"github.com/nextlinux/gosbom/gosbom/pkg"
     9  )
    10  
    11  func newPackage(classifier classifier, location file.Location, matchMetadata map[string]string) *pkg.Package {
    12  	version, ok := matchMetadata["version"]
    13  	if !ok {
    14  		return nil
    15  	}
    16  
    17  	update := matchMetadata["update"]
    18  
    19  	var cpes []cpe.CPE
    20  	for _, c := range classifier.CPEs {
    21  		c.Version = version
    22  		c.Update = update
    23  		cpes = append(cpes, c)
    24  	}
    25  
    26  	p := pkg.Package{
    27  		Name:    classifier.Package,
    28  		Version: version,
    29  		Locations: file.NewLocationSet(
    30  			location.WithAnnotation(pkg.EvidenceAnnotationKey, pkg.PrimaryEvidenceAnnotation),
    31  		),
    32  		Type:         pkg.BinaryPkg,
    33  		CPEs:         cpes,
    34  		FoundBy:      catalogerName,
    35  		MetadataType: pkg.BinaryMetadataType,
    36  		Metadata: pkg.BinaryMetadata{
    37  			Matches: []pkg.ClassifierMatch{
    38  				{
    39  					Classifier: classifier.Class,
    40  					Location:   location,
    41  				},
    42  			},
    43  		},
    44  	}
    45  
    46  	if classifier.Type != "" {
    47  		p.Type = classifier.Type
    48  	}
    49  
    50  	if !reflect.DeepEqual(classifier.PURL, emptyPURL) {
    51  		purl := classifier.PURL
    52  		purl.Version = version
    53  		p.PURL = purl.ToString()
    54  	}
    55  
    56  	if classifier.Language != "" {
    57  		p.Language = classifier.Language
    58  	}
    59  
    60  	p.SetID()
    61  
    62  	return &p
    63  }