github.com/lineaje-labs/syft@v0.98.1-0.20231227153149-9e393f60ff1b/syft/format/internal/cyclonedxutil/decoder.go (about)

     1  package cyclonedxutil
     2  
     3  import (
     4  	"fmt"
     5  	"io"
     6  
     7  	"github.com/CycloneDX/cyclonedx-go"
     8  )
     9  
    10  type Decoder struct {
    11  	format cyclonedx.BOMFileFormat
    12  }
    13  
    14  func NewDecoder(format cyclonedx.BOMFileFormat) Decoder {
    15  	return Decoder{
    16  		format: format,
    17  	}
    18  }
    19  
    20  func (d Decoder) Decode(reader io.ReadSeeker) (*cyclonedx.BOM, error) {
    21  	doc := &cyclonedx.BOM{
    22  		Components: &[]cyclonedx.Component{},
    23  	}
    24  	if _, err := reader.Seek(0, io.SeekStart); err != nil {
    25  		return nil, fmt.Errorf("unable to seek to start of CycloneDX SBOM: %w", err)
    26  	}
    27  	err := cyclonedx.NewBOMDecoder(reader, d.format).Decode(doc)
    28  	if err != nil {
    29  		return nil, err
    30  	}
    31  
    32  	return doc, nil
    33  }