diff --git a/examples/decode-bom/main.go b/examples/decode-bom/main.go index 7f193b9..2954be4 100644 --- a/examples/decode-bom/main.go +++ b/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)) } diff --git a/examples/encode-bom/main.go b/examples/encode-bom/main.go index 7f193b9..44be8cc 100644 --- a/examples/encode-bom/main.go +++ b/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) + } }