MCPcopy Create free account
hub / github.com/chainloop-dev/chainloop / buildBoilerplate

Function buildBoilerplate

pkg/policies/engine/rego/boilerplate.go:130–153  ·  view source on GitHub ↗

buildBoilerplate constructs the boilerplate template based on what's missing

(rules *existingRules)

Source from the content-addressed store, hash-verified

128
129// buildBoilerplate constructs the boilerplate template based on what's missing
130func buildBoilerplate(rules *existingRules) (string, error) {
131 data := boilerplateData{
132 NeedsResult: !rules.hasRule[ruleResult],
133 NeedsDefaultSkipReason: !rules.hasDefault[ruleSkipReason] && !rules.hasRule[ruleSkipReason],
134 NeedsSkipReasonRule: !rules.hasRule[ruleSkipReason],
135 NeedsDefaultSkipped: !rules.hasDefault[ruleSkipped] && !rules.hasRule[ruleSkipped],
136 NeedsSkippedRule: !rules.hasRule[ruleSkipped],
137 NeedsDefaultIgnore: !rules.hasDefault[ruleIgnore] && !rules.hasRule[ruleIgnore],
138 NeedsDefaultValidInput: !rules.hasDefault[ruleValidInput] && !rules.hasRule[ruleValidInput],
139 NeedsDefaultViolations: !rules.hasDefault[ruleViolations] && !rules.hasRule[ruleViolations],
140 }
141
142 tmpl, err := template.New("boilerplate").Parse(boilerplateTemplate)
143 if err != nil {
144 return "", fmt.Errorf("failed to parse boilerplate template: %w", err)
145 }
146
147 var buf bytes.Buffer
148 if err := tmpl.Execute(&buf, data); err != nil {
149 return "", fmt.Errorf("failed to execute boilerplate template: %w", err)
150 }
151
152 return buf.String(), nil
153}
154
155// injectAfterImports inserts the injection block after the package declaration and existing imports
156func injectAfterImports(module *ast.Module, originalPolicy, injection string) (string, error) {

Callers 1

InjectBoilerplateFunction · 0.85

Calls 3

ParseMethod · 0.80
ExecuteMethod · 0.65
StringMethod · 0.65

Tested by

no test coverage detected