forked from anchore/syft
-
Notifications
You must be signed in to change notification settings - Fork 0
/
encoder.go
90 lines (73 loc) · 1.79 KB
/
encoder.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package spdxtagvalue
import (
"fmt"
"io"
"github.com/spdx/tools-golang/convert"
"github.com/spdx/tools-golang/spdx/v2/v2_1"
"github.com/spdx/tools-golang/spdx/v2/v2_2"
"github.com/spdx/tools-golang/spdx/v2/v2_3"
"github.com/spdx/tools-golang/tagvalue"
"github.com/gsoc2/syft/syft/format/common/spdxhelpers"
"github.com/gsoc2/syft/syft/format/internal/spdxutil"
"github.com/gsoc2/syft/syft/sbom"
)
const ID = spdxutil.TagValueFormatID
func SupportedVersions() []string {
return spdxutil.SupportedVersions(ID)
}
type EncoderConfig struct {
Version string
}
type encoder struct {
cfg EncoderConfig
}
func NewFormatEncoderWithConfig(cfg EncoderConfig) (sbom.FormatEncoder, error) {
return encoder{
cfg: cfg,
}, nil
}
func DefaultEncoderConfig() EncoderConfig {
return EncoderConfig{
Version: spdxutil.DefaultVersion,
}
}
func (e encoder) ID() sbom.FormatID {
return ID
}
func (e encoder) Aliases() []string {
return []string{
"spdx",
"spdx-tv",
}
}
func (e encoder) Version() string {
return e.cfg.Version
}
func (e encoder) Encode(writer io.Writer, s sbom.SBOM) error {
latestDoc := spdxhelpers.ToFormatModel(s)
if latestDoc == nil {
return fmt.Errorf("unable to convert SBOM to SPDX document")
}
var err error
var encodeDoc any
switch e.cfg.Version {
case "2.1":
doc := v2_1.Document{}
err = convert.Document(latestDoc, &doc)
encodeDoc = doc
case "2.2":
doc := v2_2.Document{}
err = convert.Document(latestDoc, &doc)
encodeDoc = doc
case "2.3", "", "2", "2.x":
doc := v2_3.Document{}
err = convert.Document(latestDoc, &doc)
encodeDoc = doc
default:
return fmt.Errorf("unsupported SPDX version %q", e.cfg.Version)
}
if err != nil {
return fmt.Errorf("unable to convert SBOM to SPDX document: %w", err)
}
return tagvalue.Write(encodeDoc, writer)
}