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

     1  /*
     2  Package conda provides a concrete Cataloger implementation for packages within the Conda ecosystem.
     3  */
     4  package conda
     5  
     6  import (
     7  	"context"
     8  	"encoding/json"
     9  	"fmt"
    10  
    11  	"github.com/anchore/syft/syft/artifact"
    12  	"github.com/anchore/syft/syft/file"
    13  	"github.com/anchore/syft/syft/pkg"
    14  	"github.com/anchore/syft/syft/pkg/cataloger/generic"
    15  )
    16  
    17  // NewCondaMetaCataloger returns a new cataloger object for Conda environments by parsing the package metadata files in conda-meta.
    18  func NewCondaMetaCataloger() pkg.Cataloger {
    19  	return generic.NewCataloger("conda-meta-cataloger").
    20  		WithParserByGlobs(parseCondaMetaJSON, "**/conda-meta/*.json")
    21  }
    22  
    23  func parseCondaMetaJSON(ctx context.Context, _ file.Resolver, _ *generic.Environment, reader file.LocationReadCloser) ([]pkg.Package, []artifact.Relationship, error) {
    24  	dec := json.NewDecoder(reader)
    25  	var meta pkg.CondaMetaPackage
    26  	if err := dec.Decode(&meta); err != nil {
    27  		return nil, nil, fmt.Errorf("failed to parse conda-meta package file at %s: %w", reader.RealPath, err)
    28  	}
    29  
    30  	p := pkg.Package{
    31  		Name:      meta.Name,
    32  		Version:   meta.Version,
    33  		Locations: file.NewLocationSet(reader.Location),
    34  		Licenses: pkg.NewLicenseSet(
    35  			pkg.NewLicenseFromLocationsWithContext(ctx, meta.License, reader.Location),
    36  		),
    37  		Language: pkg.UnknownLanguage,
    38  		Type:     pkg.CondaPkg,
    39  		Metadata: meta,
    40  	}
    41  	p.SetID()
    42  
    43  	return []pkg.Package{
    44  		p,
    45  	}, nil, nil
    46  }