swap format readseekers for readers (#2515)

Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>
This commit is contained in:
Alex Goodman
2024-01-23 16:44:57 -05:00
committed by GitHub
parent 8e39ca6dfc
commit 0fe13888d5
10 changed files with 188 additions and 43 deletions
+9 -7
View File
@@ -11,6 +11,7 @@ import (
"github.com/anchore/syft/internal/log"
"github.com/anchore/syft/syft/format/common/cyclonedxhelpers"
"github.com/anchore/syft/syft/format/internal/cyclonedxutil"
"github.com/anchore/syft/syft/format/internal/stream"
"github.com/anchore/syft/syft/sbom"
)
@@ -26,9 +27,10 @@ func NewFormatDecoder() sbom.FormatDecoder {
}
}
func (d decoder) Decode(reader io.ReadSeeker) (*sbom.SBOM, sbom.FormatID, string, error) {
if reader == nil {
return nil, "", "", fmt.Errorf("no SBOM bytes provided")
func (d decoder) Decode(r io.Reader) (*sbom.SBOM, sbom.FormatID, string, error) {
reader, err := stream.SeekableReader(r)
if err != nil {
return nil, "", "", err
}
id, version := d.Identify(reader)
@@ -52,8 +54,9 @@ func (d decoder) Decode(reader io.ReadSeeker) (*sbom.SBOM, sbom.FormatID, string
return s, id, version, nil
}
func (d decoder) Identify(reader io.ReadSeeker) (sbom.FormatID, string) {
if reader == nil {
func (d decoder) Identify(r io.Reader) (sbom.FormatID, string) {
reader, err := stream.SeekableReader(r)
if err != nil {
return "", ""
}
@@ -69,8 +72,7 @@ func (d decoder) Identify(reader io.ReadSeeker) (sbom.FormatID, string) {
dec := xml.NewDecoder(reader)
var doc Document
err := dec.Decode(&doc)
if err != nil {
if err = dec.Decode(&doc); err != nil {
// maybe not xml? maybe not valid? doesn't matter, we won't process it.
return "", ""
}