Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,5 @@ test_output
**/generated-plugin

!.github/ISSUE_TEMPLATE/config.yml

evaluation_results
41 changes: 24 additions & 17 deletions cmd/generate-plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,16 @@ import (
type CatalogData struct {
layer2.Catalog
ServiceName string
Requirements []string
Requirements []Req
ApplicabilityCategories []string
StrippedName string
}

type Req struct {
Id string
Text string
}

var (
TemplatesDir string
SourcePath string
Expand Down Expand Up @@ -84,7 +89,7 @@ func generatePlugin() {
return err
}
if !info.IsDir() {
err = generateFileFromTemplate(data, path, OutputDir)
err = generateFileFromTemplate(data, path)
if err != nil {
logger.Error(fmt.Sprintf("Failed while writing in dir '%s': %s", OutputDir, err))
}
Expand Down Expand Up @@ -148,21 +153,21 @@ func setupTemplatesDir() error {
return err
}

func generateFileFromTemplate(data CatalogData, templatePath, OutputDir string) error {
func generateFileFromTemplate(data CatalogData, templatePath string) error {
templateContent, err := os.ReadFile(templatePath)
if err != nil {
return fmt.Errorf("error reading template file %s: %w", templatePath, err)
}

// Determine relative path from templates dir so we can preserve subdirs in output
relativePath, err := filepath.Rel(TemplatesDir, templatePath)
relativeFilepath, err := filepath.Rel(TemplatesDir, templatePath)
if err != nil {
return fmt.Errorf("error calculating relative path for %s: %w", templatePath, err)
}

// If the template is not a text template, copy it over as-is (preserve mode)
if filepath.Ext(templatePath) != ".txt" {
return copyNonTemplateFile(templatePath, filepath.Join(OutputDir, relativePath))
return copyNonTemplateFile(templatePath, relativeFilepath)
}

tmpl, err := template.New("plugin").Funcs(template.FuncMap{
Expand All @@ -177,14 +182,13 @@ func generateFileFromTemplate(data CatalogData, templatePath, OutputDir string)
}
return out
},
"snake_case": snakeCase,
"simplifiedName": simplifiedName,
"snake_case": snakeCase,
}).Parse(string(templateContent))
if err != nil {
return fmt.Errorf("error parsing template file %s: %w", templatePath, err)
}

outputPath := filepath.Join(OutputDir, strings.TrimSuffix(relativePath, ".txt"))
outputPath := filepath.Join(OutputDir, strings.TrimSuffix(relativeFilepath, ".txt"))

err = os.MkdirAll(filepath.Dir(outputPath), os.ModePerm)
if err != nil {
Expand Down Expand Up @@ -215,7 +219,11 @@ func (c *CatalogData) getAssessmentRequirements() error {
for _, family := range c.ControlFamilies {
for _, control := range family.Controls {
for _, requirement := range control.AssessmentRequirements {
c.Requirements = append(c.Requirements, requirement.Id)
req := Req{
Id: requirement.Id,
Text: requirement.Text,
}
c.Requirements = append(c.Requirements, req)
// Add applicability categories if unique
for _, a := range requirement.Applicability {
if !sdkutils.StringSliceContains(c.ApplicabilityCategories, a) {
Expand All @@ -240,8 +248,11 @@ func writeCatalogFile(catalog *layer2.Catalog) error {
return fmt.Errorf("error marshaling YAML: %w", err)
}

dirPath := filepath.Join(OutputDir, "data", simplifiedName(catalog.Metadata.Id, catalog.Metadata.Version))
filePath := filepath.Join(dirPath, "catalog.yaml")
dirPath := filepath.Join(OutputDir, "data", "catalogs")
id := snakeCase(catalog.Metadata.Id)
version := snakeCase(catalog.Metadata.Version)
fileName := fmt.Sprintf("catalog_%s_%s.yaml", id, version)
filePath := filepath.Join(dirPath, fileName)

err = os.MkdirAll(dirPath, os.ModePerm)
if err != nil {
Expand All @@ -261,12 +272,8 @@ func snakeCase(in string) string {
strings.ReplaceAll(in, ".", "_"), "-", "_"))
}

func simplifiedName(catalogId string, catalogVersion string) string {
return fmt.Sprintf("%s_%s", snakeCase(catalogId), snakeCase(catalogVersion))
}

func copyNonTemplateFile(templatePath, relativePath string) error {
outputPath := filepath.Join(OutputDir, relativePath)
func copyNonTemplateFile(templatePath, relativeFilepath string) error {
outputPath := filepath.Join(OutputDir, relativeFilepath)
if err := os.MkdirAll(filepath.Dir(outputPath), os.ModePerm); err != nil {
return fmt.Errorf("error creating directories for %s: %w", outputPath, err)
}
Expand Down
Loading