-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
201 lines (161 loc) · 4.04 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package main
import (
"flag"
"fmt"
"io"
"os"
"strings"
"github.com/fnxpt/cyclonedx-merge/flatmerge"
"github.com/fnxpt/cyclonedx-merge/merge"
"github.com/fnxpt/cyclonedx-merge/utils"
"github.com/CycloneDX/cyclonedx-go"
)
type MergeMode int
const (
MergeModeNormal MergeMode = iota
MergeModeFlat
MergeModeSmart
)
var version = "0.0.7"
var rootComponent = &cyclonedx.Component{
BOMRef: "root",
Name: "root",
Type: cyclonedx.ComponentTypeApplication,
}
var sbom *cyclonedx.BOM
var mode = MergeModeNormal
var outputFormat = cyclonedx.BOMFileFormatJSON
var output = os.Stdout
func main() {
sbom = utils.NewBOM(rootComponent)
parseArguments()
}
func showHelpMenu() {
fmt.Printf("usage: cyclonedx-merge(%s) [options]\n", version)
fmt.Println("options:")
os.Exit(0)
}
func parseArguments() {
flag.CommandLine = flag.NewFlagSet("", flag.ExitOnError)
flag.Usage = func() {
showHelpMenu()
flag.PrintDefaults()
}
flag.Func("group", "group of the merged parent component", func(value string) error {
rootComponent.Group = value
return nil
})
flag.Func("name", "name of the merged parent component", func(value string) error {
rootComponent.Name = value
return nil
})
flag.Func("version", "version of the merged parent component", func(value string) error {
rootComponent.Version = value
return nil
})
flag.Func("bomref", "BOMRef of the merged parent component", func(value string) error {
rootComponent.BOMRef = value
return nil
})
flag.Func("type", "type of the aggregator component", func(value string) error {
rootComponent.Type = cyclonedx.ComponentType(value)
return nil
})
flag.Func("file", "merges file", func(value string) error {
return fileMerge(value)
})
flag.Func("dir", "merges files in directory", func(value string) error {
return dirMerge(value)
})
flag.Func("mode", "merge mode - normal/flat/smart (default: normal)", func(value string) error {
switch value {
case "normal":
mode = MergeModeNormal
case "flat":
mode = MergeModeFlat
case "smart":
mode = MergeModeSmart
default:
return fmt.Errorf("invalid mode")
}
return nil
})
flag.Func("format", "output format - json/xml (default: json)", func(value string) error {
switch value {
case "json":
outputFormat = cyclonedx.BOMFileFormatJSON
case "xml":
outputFormat = cyclonedx.BOMFileFormatXML
default:
return fmt.Errorf("invalid output format")
}
return nil
})
flag.Func("output", "output file (default: stdout)", func(value string) error {
file, err := os.Create(value)
if err != nil {
fmt.Printf("unable to create file %s\n", value)
return err
}
output = file
return nil
})
flag.Parse()
writeSBOM()
}
func fileMerge(value string) error {
if _, err := os.Stat(value); os.IsNotExist(err) {
fmt.Printf("file %s doesn't exist\n", value)
return err
}
file, err := os.Open(value)
if err != nil {
fmt.Printf("unable to open file %s\n", value)
return err
}
bom, err := parseSBOM(file)
if err != nil {
fmt.Printf("unable to parse file %s\n", value)
return err
}
switch mode {
case MergeModeNormal:
merge.MergeSBOM(sbom, bom)
case MergeModeFlat:
flatmerge.MergeSBOM(sbom, bom)
case MergeModeSmart:
panic("not implemented yet")
// smartmerge.MergeSBOM(sbom, bom)
}
return nil
}
func writeSBOM() {
encoder := cyclonedx.NewBOMEncoder(output, outputFormat)
encoder.Encode(sbom)
}
func parseSBOM(input io.Reader) (*cyclonedx.BOM, error) {
bom := &cyclonedx.BOM{}
decoder := cyclonedx.NewBOMDecoder(input, cyclonedx.BOMFileFormatJSON)
err := decoder.Decode(bom)
if err != nil {
return nil, err
}
return bom, err
}
func dirMerge(value string) error {
if _, err := os.Stat(value); os.IsNotExist(err) {
fmt.Printf("directory %s doesn't exist\n", value)
return err
}
entries, err := os.ReadDir(value)
if err != nil {
fmt.Printf("unable to read directory %s\n", value)
return err
}
for _, e := range entries {
if strings.HasSuffix(e.Name(), ".json") || strings.HasSuffix(e.Name(), ".xml") {
fileMerge(fmt.Sprintf("%s/%s", value, e.Name()))
}
}
return nil
}