syft/syft/pkg/cataloger/swipl/parse_pack.go
anchore-actions-token-generator[bot] f11377fe30
chore(deps): update tools to latest versions (#3775)
---------
Signed-off-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Signed-off-by: Christopher Phillips <32073428+spiffcs@users.noreply.github.com>
Co-authored-by: spiffcs <32073428+spiffcs@users.noreply.github.com>
2025-04-03 17:35:26 +00:00

71 lines
1.7 KiB
Go

package swipl
import (
"context"
"io"
"regexp"
"github.com/anchore/syft/internal/log"
"github.com/anchore/syft/syft/artifact"
"github.com/anchore/syft/syft/file"
"github.com/anchore/syft/syft/pkg"
"github.com/anchore/syft/syft/pkg/cataloger/generic"
)
func parsePackPackage(_ context.Context, _ file.Resolver, _ *generic.Environment, reader file.LocationReadCloser) ([]pkg.Package, []artifact.Relationship, error) {
var pkgs []pkg.Package
nameRe := regexp.MustCompile(`name\(\s*'?([^')]+)'?\s*\)`)
versionRe := regexp.MustCompile(`version\('([^']+)'\)`)
homeRe := regexp.MustCompile(`home\(\s*'([^']+)'\s*\)`)
authorRe := regexp.MustCompile(`(author|packager)\(\s*'([^']+)'\s*(?:,\s*'([^']+)'\s*)?\)`)
data, err := io.ReadAll(reader)
if err != nil {
log.WithFields("error", err).Trace("unable to parse Rockspec app")
return nil, nil, nil
}
name := nameRe.FindSubmatch(data)
version := versionRe.FindSubmatch(data)
if name == nil || version == nil {
log.Debugf("encountered pack.pl file without a name and/or version field, ignoring (path=%q)", reader.Path())
return nil, nil, nil
}
entry := pkg.SwiplPackEntry{
Name: string(name[1]),
Version: string(version[1]),
}
home := homeRe.FindSubmatch(data)
if home != nil {
entry.Homepage = string(home[1])
}
authors := authorRe.FindAllSubmatch(data, -1)
for _, a := range authors {
switch string(a[1]) {
case "author":
entry.Author = string(a[2])
entry.AuthorEmail = string(a[3])
case "packager":
entry.Packager = string(a[2])
entry.PackagerEmail = string(a[3])
}
}
pkgs = append(
pkgs,
newSwiplPackPackage(
entry,
reader.WithAnnotation(pkg.EvidenceAnnotationKey, pkg.PrimaryEvidenceAnnotation),
),
)
return pkgs, nil, nil
}