Skip to content

Commit

Permalink
feat: add simple examples for encoding and decoding
Browse files Browse the repository at this point in the history
  • Loading branch information
nscuro committed Mar 4, 2021
1 parent c2aa0a0 commit b227b5f
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 2 deletions.
23 changes: 22 additions & 1 deletion examples/decode-bom/main.go
@@ -1,5 +1,26 @@
package main

import (
"fmt"
"net/http"

"github.com/CycloneDX/cyclonedx-go"
)

func main() {
panic("TODO")
res, err := http.Get("https://github.com/DependencyTrack/dependency-track/releases/download/4.1.0/bom.json")
if err != nil {
panic(err)
}
defer res.Body.Close()

bom := new(cyclonedx.BOM)
decoder := cyclonedx.NewBOMDecoder(res.Body, cyclonedx.BOMFileFormatJSON)
if err = decoder.Decode(bom); err != nil {
panic(err)
}

fmt.Printf("Successfully decoded BOM of %s\n", bom.Metadata.Component.PackageURL)
fmt.Printf("- Generated: %s with %s\n", bom.Metadata.Timestamp, (*bom.Metadata.Tools)[0].Name)
fmt.Printf("- Components: %d\n", len(*bom.Components))
}
54 changes: 53 additions & 1 deletion examples/encode-bom/main.go
@@ -1,5 +1,57 @@
package main

import (
"os"
"time"

cdx "github.com/CycloneDX/cyclonedx-go"
)

func main() {
panic("TODO")
bom := cdx.NewBOM()
bom.Metadata = &cdx.Metadata{
Timestamp: time.Now().Format(time.RFC3339),
Component: &cdx.Component{
BOMRef: "pkg:golang/acme-inc/acme-app@1.0.0",
Type: cdx.ComponentTypeApplication,
Name: "ACME Application",
Version: "1.0.0",
},
}
bom.Components = &[]cdx.Component{
{
BOMRef: "pkg:golang/github.com/CycloneDX/cyclonedx-go@0.1.0",
Type: cdx.ComponentTypeLibrary,
Author: "CycloneDX",
Name: "cyclonedx-go",
Version: "0.1.0",
Description: "Go library to consume and produce CycloneDX Software Bill of Materials (SBOM)",
PackageURL: "pkg:golang/github.com/CycloneDX/cyclonedx-go@0.1.0",
ExternalReferences: &[]cdx.ExternalReference{
{
Type: cdx.ERTypeIssueTracker,
URL: "https://github.com/CycloneDX/cyclonedx-go/issues",
},
{
Type: cdx.ERTypeWebsite,
URL: "https://cyclonedx.org",
},
},
},
}
bom.Dependencies = &[]cdx.Dependency{
{
Ref: "pkg:golang/acme-inc/acme-app@1.0.0",
Dependencies: &[]cdx.Dependency{
{Ref: "pkg:golang/github.com/CycloneDX/cyclonedx-go@0.1.0"},
},
},
}

encoder := cdx.NewBOMEncoder(os.Stdout, cdx.BOMFileFormatXML)
encoder.SetPretty(true)

if err := encoder.Encode(bom); err != nil {
panic(err)
}
}

0 comments on commit b227b5f

Please sign in to comment.