mirror of
https://github.com/anchore/syft.git
synced 2026-11-03 16:06:36 +01:00
feat: Allow specific versions of formats to be specified (#1543)
This commit is contained in:
+30
-5
@@ -2,6 +2,7 @@ package sbom
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
)
|
||||
|
||||
@@ -18,20 +19,41 @@ func (f FormatID) String() string {
|
||||
return string(f)
|
||||
}
|
||||
|
||||
const AnyVersion = ""
|
||||
|
||||
type Format interface {
|
||||
ID() FormatID
|
||||
IDs() []FormatID
|
||||
Version() string
|
||||
Encode(io.Writer, SBOM) error
|
||||
Decode(io.Reader) (*SBOM, error)
|
||||
Validate(io.Reader) error
|
||||
fmt.Stringer
|
||||
}
|
||||
|
||||
type format struct {
|
||||
id FormatID
|
||||
ids []FormatID
|
||||
version string
|
||||
encoder Encoder
|
||||
decoder Decoder
|
||||
validator Validator
|
||||
}
|
||||
|
||||
func (f format) IDs() []FormatID {
|
||||
return f.ids
|
||||
}
|
||||
|
||||
func (f format) Version() string {
|
||||
return f.version
|
||||
}
|
||||
|
||||
func (f format) String() string {
|
||||
if f.version == AnyVersion {
|
||||
return f.ID().String()
|
||||
}
|
||||
return fmt.Sprintf("%s@%s", f.ID(), f.version)
|
||||
}
|
||||
|
||||
// Decoder is a function that can convert an SBOM document of a specific format from a reader into Syft native objects.
|
||||
type Decoder func(reader io.Reader) (*SBOM, error)
|
||||
|
||||
@@ -47,9 +69,10 @@ type Encoder func(io.Writer, SBOM) error
|
||||
// really represent a different format that also uses json)
|
||||
type Validator func(reader io.Reader) error
|
||||
|
||||
func NewFormat(id FormatID, encoder Encoder, decoder Decoder, validator Validator) Format {
|
||||
return &format{
|
||||
id: id,
|
||||
func NewFormat(version string, encoder Encoder, decoder Decoder, validator Validator, ids ...FormatID) Format {
|
||||
return format{
|
||||
ids: ids,
|
||||
version: version,
|
||||
encoder: encoder,
|
||||
decoder: decoder,
|
||||
validator: validator,
|
||||
@@ -57,7 +80,7 @@ func NewFormat(id FormatID, encoder Encoder, decoder Decoder, validator Validato
|
||||
}
|
||||
|
||||
func (f format) ID() FormatID {
|
||||
return f.id
|
||||
return f.ids[0]
|
||||
}
|
||||
|
||||
func (f format) Encode(output io.Writer, s SBOM) error {
|
||||
@@ -81,3 +104,5 @@ func (f format) Validate(reader io.Reader) error {
|
||||
|
||||
return f.validator(reader)
|
||||
}
|
||||
|
||||
var _ Format = (*format)(nil)
|
||||
|
||||
@@ -15,7 +15,7 @@ func dummyEncoder(io.Writer, SBOM) error {
|
||||
}
|
||||
|
||||
func dummyFormat(name string) Format {
|
||||
return NewFormat(FormatID(name), dummyEncoder, nil, nil)
|
||||
return NewFormat(AnyVersion, dummyEncoder, nil, nil, FormatID(name))
|
||||
}
|
||||
|
||||
type writerConfig struct {
|
||||
|
||||
Reference in New Issue
Block a user