github.com/lineaje-labs/syft@v0.98.1-0.20231227153149-9e393f60ff1b/syft/format/cyclonedxxml/encoder.go (about) 1 package cyclonedxxml 2 3 import ( 4 "github.com/CycloneDX/cyclonedx-go" 5 6 "github.com/anchore/syft/syft/sbom" 7 "github.com/lineaje-labs/syft/syft/format/internal/cyclonedxutil" 8 ) 9 10 var _ sbom.FormatEncoder = (*encoder)(nil) 11 12 const ID = cyclonedxutil.XMLFormatID 13 14 func SupportedVersions() []string { 15 return cyclonedxutil.SupportedVersions(ID) 16 } 17 18 type EncoderConfig struct { 19 Version string 20 Pretty bool // set indent to 0 21 } 22 23 type encoder struct { 24 cfg EncoderConfig 25 cyclonedxutil.Encoder 26 } 27 28 func NewFormatEncoderWithConfig(cfg EncoderConfig) (sbom.FormatEncoder, error) { 29 enc, err := cyclonedxutil.NewEncoder(cfg.Version, cyclonedx.BOMFileFormatXML, cfg.Pretty) 30 if err != nil { 31 return nil, err 32 } 33 return encoder{ 34 cfg: cfg, 35 Encoder: enc, 36 }, nil 37 } 38 39 func DefaultEncoderConfig() EncoderConfig { 40 return EncoderConfig{ 41 Version: cyclonedxutil.DefaultVersion, 42 Pretty: false, 43 } 44 } 45 46 func (e encoder) ID() sbom.FormatID { 47 return ID 48 } 49 50 func (e encoder) Aliases() []string { 51 return []string{ 52 "cyclonedx", 53 "cyclone", 54 "cdx", 55 } 56 } 57 58 func (e encoder) Version() string { 59 return e.cfg.Version 60 }