From 72a7240fd0711641d974cf61661f3bb1d88c9dfa Mon Sep 17 00:00:00 2001 From: "Sascha L. Teichmann" Date: Thu, 30 Jun 2022 05:04:00 +0200 Subject: [PATCH] write category documents in create. --- cmd/csaf_provider/create.go | 17 +++++++++++++++++ csaf/rolie.go | 14 ++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/cmd/csaf_provider/create.go b/cmd/csaf_provider/create.go index a5bc182..6529c2f 100644 --- a/cmd/csaf_provider/create.go +++ b/cmd/csaf_provider/create.go @@ -14,6 +14,7 @@ import ( "errors" "fmt" "os" + "path" "path/filepath" "strings" "unicode" @@ -65,6 +66,15 @@ func createWellknown(_ *config, wellknown string) error { // No creation for the "csaf" option will be done. // It creates also symbolic links to feed folders. func createFeedFolders(c *config, wellknown string) error { + + // If we have static configured categories we need to create + // the category documents. + var catDoc *csaf.ROLIECategoryDocument + + if categories := c.StaticCategories(); len(categories) > 0 { + catDoc = csaf.NewROLIECategoryDocument(categories...) + } + for _, t := range c.TLPs { if t == tlpCSAF { continue @@ -83,6 +93,13 @@ func createFeedFolders(c *config, wellknown string) error { return err } } + // Store the category document. + if catDoc != nil { + catPath := path.Join(tlpLink, "category.json") + if err := util.WriteToFile(catPath, catDoc); err != nil { + return err + } + } } return nil } diff --git a/csaf/rolie.go b/csaf/rolie.go index 18f3fd6..84dc3de 100644 --- a/csaf/rolie.go +++ b/csaf/rolie.go @@ -79,6 +79,20 @@ type ROLIECategoryDocument struct { Categories ROLIECategories `json:"categories"` } +// NewROLIECategoryDocument creates a new ROLIE category document from a list +// of categories. +func NewROLIECategoryDocument(categories ...string) *ROLIECategoryDocument { + cats := make([]ROLIECategory, len(categories)) + for i, cat := range categories { + cats[i] = ROLIECategory{Term: cat} + } + return &ROLIECategoryDocument{ + Categories: ROLIECategories{ + Category: cats, + }, + } +} + // LoadROLIECategoryDocument loads a ROLIE category document from a reader. func LoadROLIECategoryDocument(r io.Reader) (*ROLIECategoryDocument, error) { var rcd ROLIECategoryDocument