-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
106 lines (92 loc) · 2.93 KB
/
main.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"log"
"os"
"github.com/spdx/tools-golang/convert"
"github.com/spdx/tools-golang/spdx"
"github.com/spdx/tools-golang/spdx/common"
"github.com/spdx/tools-golang/spdx/v2/v2_3"
)
func main() {
if len(os.Args) < 2 {
log.Fatal("Not enough arguments, pass as args at least 2 SBOM files to be merged")
}
finalSbomFileName := os.Args[1]
finalSbomFile, err := os.Open(finalSbomFileName)
if err != nil {
log.Fatalf("cannot open file %s: %w", finalSbomFileName, err)
}
finalSpdxDoc, err := Read(finalSbomFile)
if err != nil {
log.Fatalf("cannot convert %s to SPDX: %w", finalSbomFileName, err)
}
// merge into the finalSbom all the files that come after it in arguments
for _, v := range os.Args[2:] {
sbomFile, err := os.Open(v)
if err != nil {
log.Fatalf("cannot open file %s: %w", v, err)
}
spdxDoc, err := Read(sbomFile)
if err != nil {
log.Fatalf("cannot convert %s to SPDX: %w", v, err)
}
finalSpdxDoc.Packages = append(finalSpdxDoc.Packages, spdxDoc.Packages...)
finalSpdxDoc.Files = append(finalSpdxDoc.Files, spdxDoc.Files...)
finalSpdxDoc.Relationships = append(finalSpdxDoc.Relationships, spdxDoc.Relationships...)
finalSpdxDoc.OtherLicenses = append(finalSpdxDoc.OtherLicenses, spdxDoc.OtherLicenses...)
// These two are not present in either SBOM that I'm dealing with right now
// but might be useful in future:
finalSpdxDoc.Snippets = append(finalSpdxDoc.Snippets, spdxDoc.Snippets...)
finalSpdxDoc.Annotations = append(finalSpdxDoc.Annotations, spdxDoc.Annotations...)
// ExternalDocumentReferences does not seem relevant right now, one big SBOM rather
}
finalJsonSpdx, _ := json.MarshalIndent(finalSpdxDoc, "", " ")
fmt.Println(string(finalJsonSpdx))
}
// Read takes an io.Reader and returns a fully-parsed current model SPDX Document
// or an error if any error is encountered.
func Read(content io.Reader) (*spdx.Document, error) {
doc := spdx.Document{}
err := ReadInto(content, &doc)
return &doc, err
}
// ReadInto takes an io.Reader, reads in the SPDX document at the version provided
// and converts to the doc version
func ReadInto(content io.Reader, doc common.AnyDocument) error {
if !convert.IsPtr(doc) {
return fmt.Errorf("doc to read into must be a pointer")
}
buf := new(bytes.Buffer)
_, err := buf.ReadFrom(content)
if err != nil {
return err
}
var data interface{}
err = json.Unmarshal(buf.Bytes(), &data)
if err != nil {
return err
}
val, ok := data.(map[string]interface{})
if !ok {
return fmt.Errorf("not a valid SPDX JSON document")
}
version, ok := val["spdxVersion"]
if !ok {
return fmt.Errorf("JSON document does not contain spdxVersion field")
}
if version == v2_3.Version {
var doc v2_3.Document
err = json.Unmarshal(buf.Bytes(), &doc)
if err != nil {
return err
}
data = doc
} else {
return fmt.Errorf("unsupported SDPX version: %s", version)
}
return convert.Document(data, doc)
}