| 92 | } |
| 93 | |
| 94 | func loadAndProcessTemplates(opts *InitOptions) (*Content, error) { |
| 95 | regoContent, err := templateFS.ReadFile(policyTemplateRegoPath) |
| 96 | if err != nil { |
| 97 | return nil, fmt.Errorf("failed to read Rego template: %w", err) |
| 98 | } |
| 99 | |
| 100 | data := &TemplateData{ |
| 101 | Name: getPolicyName(opts.Name), |
| 102 | Description: getPolicyDescription(opts.Description), |
| 103 | RegoPath: sanitizeName(getPolicyName(opts.Name)) + ".rego", |
| 104 | RegoContent: string(regoContent), |
| 105 | Embedded: opts.Embedded, |
| 106 | MaterialKind: defaultMaterialKind, |
| 107 | } |
| 108 | |
| 109 | // Process main template |
| 110 | content, err := templateFS.ReadFile(policyTemplatePath) |
| 111 | if err != nil { |
| 112 | return nil, fmt.Errorf("failed to read policy template: %w", err) |
| 113 | } |
| 114 | |
| 115 | yamlContent, err := executeTemplate(string(content), data) |
| 116 | if err != nil { |
| 117 | return nil, fmt.Errorf("failed to execute template: %w", err) |
| 118 | } |
| 119 | |
| 120 | // For non-embedded case, we still need the Rego content to write to file |
| 121 | if !opts.Embedded { |
| 122 | return &Content{ |
| 123 | YAML: yamlContent, |
| 124 | Rego: data.RegoContent, |
| 125 | }, nil |
| 126 | } |
| 127 | |
| 128 | return &Content{YAML: yamlContent}, nil |
| 129 | } |
| 130 | |
| 131 | // Add custom template functions |
| 132 | func executeTemplate(content string, data *TemplateData) (string, error) { |