-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add example for iterating product id and product helper
- Loading branch information
Showing
1 changed file
with
94 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
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)) | ||
} |