syft/syft/pkg/cataloger/haskell/parse_cabal_freeze.go
anchore-actions-token-generator[bot] 4b7ae0ed3b
chore(deps): update tools to latest versions (#3121)
* chore(deps): update tools to latest versions

Signed-off-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

* chore: update code to reflect new linter settings for error messages

Signed-off-by: Christopher Phillips <32073428+spiffcs@users.noreply.github.com>

---------

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>
2024-08-16 17:56:36 +00:00

61 lines
1.4 KiB
Go

package haskell
import (
"bufio"
"context"
"errors"
"fmt"
"io"
"strings"
"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"
)
var _ generic.Parser = parseCabalFreeze
// parseCabalFreeze is a parser function for cabal.project.freeze contents, returning all packages discovered.
func parseCabalFreeze(_ context.Context, _ file.Resolver, _ *generic.Environment, reader file.LocationReadCloser) ([]pkg.Package, []artifact.Relationship, error) {
r := bufio.NewReader(reader)
var pkgs []pkg.Package
for {
line, err := r.ReadString('\n')
switch {
case errors.Is(err, io.EOF):
return pkgs, nil, nil
case err != nil:
return nil, nil, fmt.Errorf("failed to parse cabal.project.freeze file: %w", err)
}
if !strings.Contains(line, "any.") {
continue
}
line = strings.TrimSpace(line)
startPkgEncoding, endPkgEncoding := strings.Index(line, "any.")+4, strings.Index(line, ",")
// case where comma not found for last package in constraint list
if endPkgEncoding == -1 {
endPkgEncoding = len(line)
}
if startPkgEncoding >= endPkgEncoding || startPkgEncoding < 0 {
continue
}
line = line[startPkgEncoding:endPkgEncoding]
fields := strings.Split(line, " ==")
pkgName, pkgVersion := fields[0], fields[1]
pkgs = append(
pkgs,
newPackage(
pkgName,
pkgVersion,
nil,
reader.Location,
),
)
}
}