Skip to content

Commit e7785e2

Browse files
committed
fix: nix build for sbom-merger
1 parent 99c04f9 commit e7785e2

File tree

4 files changed

+110
-98
lines changed

4 files changed

+110
-98
lines changed

flake.nix

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
vendorHash = null;
4949

5050
buildPhase = ''
51-
go build -o ubuntu-sbom main.go
51+
go build -o ubuntu-sbom types.go main.go
5252
'';
5353

5454
installPhase = ''
@@ -70,7 +70,7 @@
7070
vendorHash = null;
7171

7272
buildPhase = ''
73-
go build -o sbom-merge merge.go
73+
go build -o sbom-merge types.go merge.go merge_main.go
7474
'';
7575

7676
installPhase = ''
@@ -189,7 +189,7 @@
189189

190190
# Build static binary with no CGO
191191
buildPhase = ''
192-
CGO_ENABLED=0 go build -a -ldflags '-s -w -extldflags "-static"' -o ubuntu-sbom main.go
192+
CGO_ENABLED=0 go build -a -ldflags '-s -w -extldflags "-static"' -o ubuntu-sbom types.go main.go
193193
'';
194194

195195
installPhase = ''
@@ -220,7 +220,7 @@
220220

221221
# Build static binary with no CGO
222222
buildPhase = ''
223-
CGO_ENABLED=0 go build -a -ldflags '-s -w -extldflags "-static"' -o ubuntu-sbom main.go
223+
CGO_ENABLED=0 go build -a -ldflags '-s -w -extldflags "-static"' -o ubuntu-sbom types.go main.go
224224
'';
225225

226226
installPhase = ''

main.go

Lines changed: 0 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -15,74 +15,6 @@ import (
1515
"time"
1616
)
1717

18-
// SPDX Document structure
19-
type SPDXDocument struct {
20-
SPDXVersion string `json:"spdxVersion"`
21-
DataLicense string `json:"dataLicense"`
22-
SPDXID string `json:"SPDXID"`
23-
Name string `json:"name"`
24-
DocumentNamespace string `json:"documentNamespace"`
25-
CreationInfo CreationInfo `json:"creationInfo"`
26-
Packages []Package `json:"packages"`
27-
Relationships []Relationship `json:"relationships"`
28-
}
29-
30-
type CreationInfo struct {
31-
Created string `json:"created"`
32-
Creators []string `json:"creators"`
33-
LicenseListVersion string `json:"licenseListVersion"`
34-
}
35-
36-
type Package struct {
37-
SPDXID string `json:"SPDXID"`
38-
Name string `json:"name"`
39-
DownloadLocation string `json:"downloadLocation"`
40-
FilesAnalyzed bool `json:"filesAnalyzed"`
41-
VerificationCode *Verification `json:"verificationCode,omitempty"`
42-
Checksums []Checksum `json:"checksums,omitempty"`
43-
HomePage string `json:"homePage,omitempty"`
44-
LicenseConcluded string `json:"licenseConcluded"`
45-
LicenseDeclared string `json:"licenseDeclared"`
46-
CopyrightText string `json:"copyrightText"`
47-
Description string `json:"description,omitempty"`
48-
PackageVersion string `json:"versionInfo,omitempty"`
49-
Supplier string `json:"supplier,omitempty"`
50-
ExternalRefs []ExternalRef `json:"externalRefs,omitempty"`
51-
}
52-
53-
type Verification struct {
54-
Value string `json:"packageVerificationCodeValue"`
55-
}
56-
57-
type Checksum struct {
58-
Algorithm string `json:"algorithm"`
59-
Value string `json:"checksumValue"`
60-
}
61-
62-
type Relationship struct {
63-
SPDXElementID string `json:"spdxElementId"`
64-
RelatedSPDXElement string `json:"relatedSpdxElement"`
65-
RelationshipType string `json:"relationshipType"`
66-
}
67-
68-
type ExternalRef struct {
69-
Category string `json:"referenceCategory"`
70-
Type string `json:"referenceType"`
71-
Locator string `json:"referenceLocator"`
72-
}
73-
74-
type DpkgPackage struct {
75-
Name string
76-
Version string
77-
Architecture string
78-
Status string
79-
Maintainer string
80-
Homepage string
81-
Description string
82-
License string
83-
Copyright string
84-
}
85-
8618
func main() {
8719
var (
8820
outputFile = flag.String("output", "ubuntu-sbom.spdx.json", "Output file path")
@@ -308,21 +240,6 @@ func (g *SBOMGenerator) calculatePackageChecksum(packageName string) string {
308240
return fmt.Sprintf("%x", h.Sum(nil))
309241
}
310242

311-
func hashFile(path string) string {
312-
file, err := os.Open(path)
313-
if err != nil {
314-
return ""
315-
}
316-
defer file.Close()
317-
318-
h := sha256.New()
319-
if _, err := io.Copy(h, file); err != nil {
320-
return ""
321-
}
322-
323-
return fmt.Sprintf("%x", h.Sum(nil))
324-
}
325-
326243
func (g *SBOMGenerator) Save(doc *SPDXDocument, outputPath string) error {
327244
file, err := os.Create(outputPath)
328245
if err != nil {
@@ -442,14 +359,3 @@ func sanitizeName(name string) string {
442359
re := regexp.MustCompile(`[^a-zA-Z0-9-.]`)
443360
return re.ReplaceAllString(name, "-")
444361
}
445-
446-
func generateUUID() string {
447-
// Simple UUID v4 generation
448-
b := make([]byte, 16)
449-
for i := range b {
450-
b[i] = byte(time.Now().UnixNano() & 0xff)
451-
}
452-
453-
return fmt.Sprintf("%x-%x-%x-%x-%x",
454-
b[0:4], b[4:6], b[6:8], b[8:10], b[10:])
455-
}

merge_main.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package main
2+
3+
func main() {
4+
mainMerge()
5+
}

types.go

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package main
2+
3+
import (
4+
"crypto/sha256"
5+
"fmt"
6+
"io"
7+
"os"
8+
"time"
9+
)
10+
11+
type SPDXDocument struct {
12+
SPDXVersion string `json:"spdxVersion"`
13+
DataLicense string `json:"dataLicense"`
14+
SPDXID string `json:"SPDXID"`
15+
Name string `json:"name"`
16+
DocumentNamespace string `json:"documentNamespace"`
17+
CreationInfo CreationInfo `json:"creationInfo"`
18+
Packages []Package `json:"packages"`
19+
Relationships []Relationship `json:"relationships"`
20+
}
21+
22+
type CreationInfo struct {
23+
Created string `json:"created"`
24+
Creators []string `json:"creators"`
25+
LicenseListVersion string `json:"licenseListVersion"`
26+
}
27+
28+
type Package struct {
29+
SPDXID string `json:"SPDXID"`
30+
Name string `json:"name"`
31+
DownloadLocation string `json:"downloadLocation"`
32+
FilesAnalyzed bool `json:"filesAnalyzed"`
33+
VerificationCode *Verification `json:"verificationCode,omitempty"`
34+
Checksums []Checksum `json:"checksums,omitempty"`
35+
HomePage string `json:"homePage,omitempty"`
36+
LicenseConcluded string `json:"licenseConcluded"`
37+
LicenseDeclared string `json:"licenseDeclared"`
38+
CopyrightText string `json:"copyrightText"`
39+
Description string `json:"description,omitempty"`
40+
PackageVersion string `json:"versionInfo,omitempty"`
41+
Supplier string `json:"supplier,omitempty"`
42+
ExternalRefs []ExternalRef `json:"externalRefs,omitempty"`
43+
}
44+
45+
type Verification struct {
46+
Value string `json:"packageVerificationCodeValue"`
47+
}
48+
49+
type Checksum struct {
50+
Algorithm string `json:"algorithm"`
51+
Value string `json:"checksumValue"`
52+
}
53+
54+
type Relationship struct {
55+
SPDXElementID string `json:"spdxElementId"`
56+
RelatedSPDXElement string `json:"relatedSpdxElement"`
57+
RelationshipType string `json:"relationshipType"`
58+
}
59+
60+
type ExternalRef struct {
61+
Category string `json:"referenceCategory"`
62+
Type string `json:"referenceType"`
63+
Locator string `json:"referenceLocator"`
64+
}
65+
66+
type DpkgPackage struct {
67+
Name string
68+
Version string
69+
Architecture string
70+
Status string
71+
Maintainer string
72+
Homepage string
73+
Description string
74+
License string
75+
Copyright string
76+
}
77+
78+
func generateUUID() string {
79+
b := make([]byte, 16)
80+
for i := range b {
81+
b[i] = byte(time.Now().UnixNano() & 0xff)
82+
}
83+
84+
return fmt.Sprintf("%x-%x-%x-%x-%x",
85+
b[0:4], b[4:6], b[6:8], b[8:10], b[10:])
86+
}
87+
88+
func hashFile(path string) string {
89+
file, err := os.Open(path)
90+
if err != nil {
91+
return ""
92+
}
93+
defer file.Close()
94+
95+
h := sha256.New()
96+
if _, err := io.Copy(h, file); err != nil {
97+
return ""
98+
}
99+
100+
return fmt.Sprintf("%x", h.Sum(nil))
101+
}

0 commit comments

Comments
 (0)