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

     1  package elixir
     2  
     3  import (
     4  	"bufio"
     5  	"errors"
     6  	"fmt"
     7  	"io"
     8  	"regexp"
     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  	"github.com/lineaje-labs/syft/internal/log"
    15  )
    16  
    17  // integrity check
    18  var _ generic.Parser = parseMixLock
    19  
    20  var mixLockDelimiter = regexp.MustCompile(`[%{}\n" ,:]+`)
    21  
    22  // parseMixLock parses a mix.lock and returns the discovered Elixir packages.
    23  func parseMixLock(
    24  	_ file.Resolver, _ *generic.Environment, reader file.LocationReadCloser,
    25  ) ([]pkg.Package, []artifact.Relationship, error) {
    26  	r := bufio.NewReader(reader)
    27  
    28  	var packages []pkg.Package
    29  	for {
    30  		line, err := r.ReadString('\n')
    31  		switch {
    32  		case errors.Is(io.EOF, err):
    33  			return packages, nil, nil
    34  		case err != nil:
    35  			return nil, nil, fmt.Errorf("failed to parse mix.lock file: %w", err)
    36  		}
    37  		tokens := mixLockDelimiter.Split(line, -1)
    38  		if len(tokens) < 6 {
    39  			continue
    40  		}
    41  		name, version, hash, hashExt := tokens[1], tokens[4], tokens[5], tokens[len(tokens)-2]
    42  
    43  		if name == "" {
    44  			log.WithFields("path", reader.RealPath).Debug("skipping empty package name from mix.lock file")
    45  			continue
    46  		}
    47  
    48  		packages = append(packages,
    49  			newPackage(
    50  				pkg.ElixirMixLockEntry{
    51  					Name:       name,
    52  					Version:    version,
    53  					PkgHash:    hash,
    54  					PkgHashExt: hashExt,
    55  				},
    56  				reader.Location.WithAnnotation(pkg.EvidenceAnnotationKey, pkg.PrimaryEvidenceAnnotation),
    57  			),
    58  		)
    59  	}
    60  }