Skip to content

Commit 80255cc

Browse files
committed
Move the enrichment into the parlay package vs the command
1 parent ec511a1 commit 80255cc

File tree

2 files changed

+41
-31
lines changed

2 files changed

+41
-31
lines changed

internal/commands/enrich.go

+1-31
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ import (
1010
"github.com/snyk/parlay/pkg/parlay"
1111

1212
cdx "github.com/CycloneDX/cyclonedx-go"
13-
"github.com/package-url/packageurl-go"
14-
"github.com/remeh/sizedwaitgroup"
1513
"github.com/spf13/cobra"
1614
)
1715

@@ -38,35 +36,7 @@ func NewEnrichCommand(logger *log.Logger) *cobra.Command {
3836
panic(err)
3937
}
4038

41-
wg := sizedwaitgroup.New(20)
42-
43-
newComponents := make([]cdx.Component, len(*bom.Components))
44-
45-
for i, component := range *bom.Components {
46-
wg.Add()
47-
go func(component cdx.Component, i int) {
48-
purl, _ := packageurl.FromString(component.PackageURL)
49-
resp, err := parlay.GetPackageData(purl)
50-
if err == nil {
51-
packageData := resp.JSON200
52-
if packageData != nil {
53-
if packageData.Description != nil {
54-
component.Description = *packageData.Description
55-
}
56-
if packageData.Licenses != nil {
57-
licences := cdx.LicenseChoice{Expression: *packageData.Licenses}
58-
component.Licenses = &cdx.Licenses{licences}
59-
}
60-
}
61-
}
62-
newComponents[i] = component
63-
wg.Done()
64-
}(component, i)
65-
}
66-
67-
wg.Wait()
68-
69-
bom.Components = &newComponents
39+
bom = parlay.EnrichSBOM(bom)
7040

7141
err = cdx.NewBOMEncoder(os.Stdout, cdx.BOMFileFormatJSON).Encode(bom)
7242
},

pkg/parlay/enrich.go

+40
Original file line numberDiff line numberDiff line change
@@ -1 +1,41 @@
11
package parlay
2+
3+
import (
4+
5+
cdx "github.com/CycloneDX/cyclonedx-go"
6+
"github.com/package-url/packageurl-go"
7+
"github.com/remeh/sizedwaitgroup"
8+
)
9+
10+
func EnrichSBOM(bom *cdx.BOM) *cdx.BOM {
11+
wg := sizedwaitgroup.New(20)
12+
13+
newComponents := make([]cdx.Component, len(*bom.Components))
14+
15+
for i, component := range *bom.Components {
16+
wg.Add()
17+
go func(component cdx.Component, i int) {
18+
purl, _ := packageurl.FromString(component.PackageURL)
19+
resp, err := GetPackageData(purl)
20+
if err == nil {
21+
packageData := resp.JSON200
22+
if packageData != nil {
23+
if packageData.Description != nil {
24+
component.Description = *packageData.Description
25+
}
26+
if packageData.Licenses != nil {
27+
licences := cdx.LicenseChoice{Expression: *packageData.Licenses}
28+
component.Licenses = &cdx.Licenses{licences}
29+
}
30+
}
31+
}
32+
newComponents[i] = component
33+
wg.Done()
34+
}(component, i)
35+
}
36+
37+
wg.Wait()
38+
39+
bom.Components = &newComponents
40+
return bom
41+
}

0 commit comments

Comments
 (0)