github.com/noqcks/syft@v0.0.0-20230920222752-a9e2c4e288e5/syft/pkg/cataloger/haskell/parse_cabal_freeze.go (about)

     1  package haskell
     2  
     3  import (
     4  	"bufio"
     5  	"errors"
     6  	"fmt"
     7  	"io"
     8  	"strings"
     9  
    10  	"github.com/anchore/syft/syft/artifact"
    11  	"github.com/anchore/syft/syft/file"
    12  	"github.com/anchore/syft/syft/pkg"
    13  	"github.com/anchore/syft/syft/pkg/cataloger/generic"
    14  )
    15  
    16  var _ generic.Parser = parseCabalFreeze
    17  
    18  // parseCabalFreeze is a parser function for cabal.project.freeze contents, returning all packages discovered.
    19  func parseCabalFreeze(_ file.Resolver, _ *generic.Environment, reader file.LocationReadCloser) ([]pkg.Package, []artifact.Relationship, error) {
    20  	r := bufio.NewReader(reader)
    21  	var pkgs []pkg.Package
    22  	for {
    23  		line, err := r.ReadString('\n')
    24  		switch {
    25  		case errors.Is(io.EOF, err):
    26  			return pkgs, nil, nil
    27  		case err != nil:
    28  			return nil, nil, fmt.Errorf("failed to parse cabal.project.freeze file: %w", err)
    29  		}
    30  
    31  		if !strings.Contains(line, "any.") {
    32  			continue
    33  		}
    34  
    35  		line = strings.TrimSpace(line)
    36  		startPkgEncoding, endPkgEncoding := strings.Index(line, "any.")+4, strings.Index(line, ",")
    37  		// case where comma not found for last package in constraint list
    38  		if endPkgEncoding == -1 {
    39  			endPkgEncoding = len(line)
    40  		}
    41  		if startPkgEncoding >= endPkgEncoding || startPkgEncoding < 0 {
    42  			continue
    43  		}
    44  
    45  		line = line[startPkgEncoding:endPkgEncoding]
    46  		fields := strings.Split(line, " ==")
    47  
    48  		pkgName, pkgVersion := fields[0], fields[1]
    49  		pkgs = append(
    50  			pkgs,
    51  			newPackage(
    52  				pkgName,
    53  				pkgVersion,
    54  				nil,
    55  				reader.Location.WithAnnotation(pkg.EvidenceAnnotationKey, pkg.PrimaryEvidenceAnnotation),
    56  			),
    57  		)
    58  	}
    59  }