github.com/google/osv-scalibr@v0.4.1/packageindex/package_index.go (about)

     1  // Copyright 2025 Google LLC
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  // Package packageindex is a wrapper around the collected package, which
    16  // provides methods for fast lookup of identified software.
    17  package packageindex
    18  
    19  import (
    20  	"github.com/google/osv-scalibr/extractor"
    21  )
    22  
    23  // PackageIndex allows you to query the package result.
    24  type PackageIndex struct {
    25  	// Two-dimensional map: package type -> (package name -> Package).
    26  	pkgMap map[string]map[string][]*extractor.Package
    27  }
    28  
    29  // New creates a PackageIndex based on the specified extraction results.
    30  func New(pkgs []*extractor.Package) (*PackageIndex, error) {
    31  	pkgMap := make(map[string]map[string][]*extractor.Package)
    32  	for _, pkg := range pkgs {
    33  		name := pkg.Name
    34  		purlType := pkg.PURLType
    35  		if p := pkg.PURL(); p != nil {
    36  			name = p.Name
    37  			purlType = p.Type
    38  		}
    39  		if _, ok := pkgMap[purlType]; !ok {
    40  			pkgMap[purlType] = make(map[string][]*extractor.Package)
    41  		}
    42  		pkgMap[purlType][name] = append(pkgMap[purlType][name], pkg)
    43  	}
    44  	return &PackageIndex{pkgMap: pkgMap}, nil
    45  }
    46  
    47  // GetAll lists all detected software packages.
    48  func (px *PackageIndex) GetAll() []*extractor.Package {
    49  	result := []*extractor.Package{}
    50  	for _, m := range px.pkgMap {
    51  		for _, p := range m {
    52  			result = append(result, p...)
    53  		}
    54  	}
    55  	return result
    56  }
    57  
    58  // GetAllOfType lists all detected software package of a given purl
    59  // package type (e.g. "deb" "golang" "pypi").
    60  func (px *PackageIndex) GetAllOfType(pkgType string) []*extractor.Package {
    61  	result := []*extractor.Package{}
    62  	m, ok := px.pkgMap[pkgType]
    63  	if !ok {
    64  		return result
    65  	}
    66  	for _, p := range m {
    67  		result = append(result, p...)
    68  	}
    69  	return result
    70  }
    71  
    72  // GetSpecific lists all versions of a software with the specified name+package type.
    73  func (px *PackageIndex) GetSpecific(name string, pkgType string) []*extractor.Package {
    74  	result := []*extractor.Package{}
    75  	m, ok := px.pkgMap[pkgType]
    76  	if !ok {
    77  		return result
    78  	}
    79  	p, ok := m[name]
    80  	if !ok {
    81  		return result
    82  	}
    83  	return p
    84  }