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

     1  package swipl
     2  
     3  import (
     4  	"context"
     5  	"io"
     6  	"regexp"
     7  
     8  	"github.com/anchore/syft/internal/log"
     9  	"github.com/anchore/syft/syft/artifact"
    10  	"github.com/anchore/syft/syft/file"
    11  	"github.com/anchore/syft/syft/pkg"
    12  	"github.com/anchore/syft/syft/pkg/cataloger/generic"
    13  )
    14  
    15  func parsePackPackage(ctx context.Context, resolver file.Resolver, _ *generic.Environment, reader file.LocationReadCloser) ([]pkg.Package, []artifact.Relationship, error) {
    16  	var pkgs []pkg.Package
    17  
    18  	nameRe := regexp.MustCompile(`name\(\s*'?([^')]+)'?\s*\)`)
    19  	versionRe := regexp.MustCompile(`version\('([^']+)'\)`)
    20  	homeRe := regexp.MustCompile(`home\(\s*'([^']+)'\s*\)`)
    21  	authorRe := regexp.MustCompile(`(author|packager)\(\s*'([^']+)'\s*(?:,\s*'([^']+)'\s*)?\)`)
    22  
    23  	data, err := io.ReadAll(reader)
    24  	if err != nil {
    25  		log.WithFields("error", err).Trace("unable to parse Rockspec app")
    26  		return nil, nil, nil
    27  	}
    28  
    29  	name := nameRe.FindSubmatch(data)
    30  	version := versionRe.FindSubmatch(data)
    31  
    32  	if name == nil || version == nil {
    33  		log.Debugf("encountered pack.pl file without a name and/or version field, ignoring (path=%q)", reader.Path())
    34  		return nil, nil, nil
    35  	}
    36  
    37  	entry := pkg.SwiplPackEntry{
    38  		Name:    string(name[1]),
    39  		Version: string(version[1]),
    40  	}
    41  
    42  	home := homeRe.FindSubmatch(data)
    43  
    44  	if home != nil {
    45  		entry.Homepage = string(home[1])
    46  	}
    47  
    48  	authors := authorRe.FindAllSubmatch(data, -1)
    49  
    50  	for _, a := range authors {
    51  		switch string(a[1]) {
    52  		case "author":
    53  			entry.Author = string(a[2])
    54  			entry.AuthorEmail = string(a[3])
    55  		case "packager":
    56  			entry.Packager = string(a[2])
    57  			entry.PackagerEmail = string(a[3])
    58  		}
    59  	}
    60  
    61  	pkgs = append(
    62  		pkgs,
    63  		newSwiplPackPackage(
    64  			ctx,
    65  			resolver,
    66  			entry,
    67  			reader.WithAnnotation(pkg.EvidenceAnnotationKey, pkg.PrimaryEvidenceAnnotation),
    68  		),
    69  	)
    70  
    71  	return pkgs, nil, nil
    72  }