Skip to content

Commit

Permalink
Add example for iterating product id and product helper
Browse files Browse the repository at this point in the history
  • Loading branch information
koplas committed Mar 3, 2025
1 parent c208a8f commit 3684004
Showing 1 changed file with 94 additions and 0 deletions.
94 changes: 94 additions & 0 deletions examples/product_lister/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// Package main implements a simple demo program to
// work with the csaf library.
package main

import (
"encoding/json"
"flag"
"fmt"
"github.com/gocsaf/csaf/v3/csaf"
"log"
"os"
)

func main() {
flag.Usage = func() {
if _, err := fmt.Fprintf(flag.CommandLine.Output(),
"Usage:\n %s [OPTIONS] files...\n\nOptions:\n", os.Args[0]); err != nil {
log.Fatalf("error: %v\n", err)
}
flag.PrintDefaults()
}
printProductIdentHelper := flag.Bool("print_ident_helper", false, "print product helper mapping")
flag.Parse()

files := flag.Args()
if len(files) == 0 {
log.Println("No files given.")
return
}
if err := run(files, *printProductIdentHelper); err != nil {
log.Fatalf("error: %v\n", err)
}
}

// run prints all product IDs and their full_product_names and product_identification_helpers.
func run(files []string, printProductIdentHelper bool) error {
for _, file := range files {
adv, err := csaf.LoadAdvisory(file)
if err != nil {
return fmt.Errorf("loading %q failed: %w", file, err)
}
if printProductIdentHelper {
printProductIdentHelperMapping(adv)
} else {
printProductIDMapping(adv)
}
}

return nil
}

// printProductIDMapping prints all product ids with their name and identification helper.
func printProductIDMapping(adv *csaf.Advisory) {
type productNameHelperMapping struct {
fullProductName *csaf.FullProductName
productIdentificationHelper *csaf.ProductIdentificationHelper
}

productIDMap := map[csaf.ProductID][]productNameHelperMapping{}
if fpns := adv.ProductTree.FullProductNames; fpns != nil {
for _, fpn := range *fpns {
if fpn != nil && fpn.ProductID != nil {
productIDMap[*fpn.ProductID] = append(productIDMap[*fpn.ProductID], productNameHelperMapping{
fullProductName: fpn,
productIdentificationHelper: fpn.ProductIdentificationHelper,
})
}
}
}
jsonData, _ := json.MarshalIndent(productIDMap, "", " ")
fmt.Println(string(jsonData))
}

// printProductIdentHelperMapping prints all product identifier helper with their product id.
func printProductIdentHelperMapping(adv *csaf.Advisory) {
type productIdentIdMapping struct {

Check failure on line 76 in examples/product_lister/main.go

View workflow job for this annotation

GitHub Actions / build

type productIdentIdMapping should be productIdentIDMapping
productNameHelperMapping csaf.ProductIdentificationHelper
productID *csaf.ProductID
}

productIdentMap := []productIdentIdMapping{}
if fpns := adv.ProductTree.FullProductNames; fpns != nil {
for _, fpn := range *fpns {
if fpn != nil && fpn.ProductIdentificationHelper != nil {
productIdentMap = append(productIdentMap, productIdentIdMapping{
productNameHelperMapping: *fpn.ProductIdentificationHelper,
productID: fpn.ProductID,
})
}
}
}
jsonData, _ := json.MarshalIndent(productIdentMap, "", " ")
fmt.Println(string(jsonData))
}

0 comments on commit 3684004

Please sign in to comment.