Skip to content

Commit

Permalink
feat(helmExecute): Allow custom delimiter (SAP#4312)
Browse files Browse the repository at this point in the history
Co-authored-by: Ralf Pannemans <[email protected]>
Co-authored-by: Johannes Dillmann <[email protected]>
Co-authored-by: Jan von Loewenstein <[email protected]>
  • Loading branch information
3 people authored and maxatsap committed Jul 23, 2024
1 parent 3d6fae2 commit d4bb209
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 2 deletions.
2 changes: 1 addition & 1 deletion cmd/helmExecute.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ func parseAndRenderCPETemplate(config helmExecuteOptions, rootPath string, utils
if err != nil {
return fmt.Errorf("failed to read file: %v", err)
}
generated, err := cpe.ParseTemplate(string(cpeTemplate))
generated, err := cpe.ParseTemplateWithDelimiter(string(cpeTemplate), config.TemplateStartDelimiter, config.TemplateEndDelimiter)
if err != nil {
return fmt.Errorf("failed to parse template: %v", err)
}
Expand Down
22 changes: 22 additions & 0 deletions cmd/helmExecute_generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 8 additions & 1 deletion pkg/piperenv/templating.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,16 @@ import (
"text/template"
)

const DEFAULT_START_DELIMITER = "{{"
const DEFAULT_END_DELIMITER = "}}"

// ParseTemplate allows to parse a template which contains references to the CPE
// Utility functions make it simple to access specific parts of the CPE
func (c *CPEMap) ParseTemplate(cpeTemplate string) (*bytes.Buffer, error) {
return c.ParseTemplateWithDelimiter(cpeTemplate, DEFAULT_START_DELIMITER, DEFAULT_END_DELIMITER)
}

func (c *CPEMap) ParseTemplateWithDelimiter(cpeTemplate string, startDelimiter string, endDelimiter string) (*bytes.Buffer, error) {
funcMap := template.FuncMap{
"cpe": c.cpe,
"cpecustom": c.custom,
Expand All @@ -21,7 +28,7 @@ func (c *CPEMap) ParseTemplate(cpeTemplate string) (*bytes.Buffer, error) {
// This requires alignment on artifact handling before, though
}

tmpl, err := template.New("cpetemplate").Funcs(funcMap).Parse(cpeTemplate)
tmpl, err := template.New("cpetemplate").Delims(startDelimiter, endDelimiter).Funcs(funcMap).Parse(cpeTemplate)
if err != nil {
return nil, fmt.Errorf("failed to parse cpe template '%v': %w", cpeTemplate, err)
}
Expand Down
29 changes: 29 additions & 0 deletions pkg/piperenv/templating_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,35 @@ func TestParseTemplate(t *testing.T) {
}
}

func TestParseTemplateWithDelimiter(t *testing.T) {
tt := []struct {
template string
cpe CPEMap
expected string
expectedError error
}{
{template: `version: [[index .CPE "artifactVersion"]], sha: [[git "commitId"]]`, expected: "version: 1.2.3, sha: thisIsMyTestSha"},
{template: "version: [[", expectedError: fmt.Errorf("failed to parse cpe template 'version: [['")},
{template: `version: [[index .CPE "artifactVersion"]], release: {{ .RELEASE }}`, expected: "version: 1.2.3, release: {{ .RELEASE }}"},
}

cpe := CPEMap{
"artifactVersion": "1.2.3",
"git/commitId": "thisIsMyTestSha",
}

for _, test := range tt {
res, err := cpe.ParseTemplateWithDelimiter(test.template, "[[", "]]")
if test.expectedError != nil {
assert.Contains(t, fmt.Sprint(err), fmt.Sprint(test.expectedError))
} else {
assert.NoError(t, err)
assert.Equal(t, test.expected, (*res).String())
}

}
}

func TestTemplateFunctionCpe(t *testing.T) {
t.Run("CPE from object", func(t *testing.T) {
tt := []struct {
Expand Down
14 changes: 14 additions & 0 deletions resources/metadata/helmExecute.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,20 @@ spec:
- PARAMETERS
- STAGES
- STEPS
- name: templateStartDelimiter
type: string
description: When templating value files, use this start delimiter.
default: "{{"
scope:
- STEPS
- PARAMETERS
- name: templateEndDelimiter
type: string
description: When templating value files, use this end delimiter.
default: "}}"
scope:
- STEPS
- PARAMETERS
containers:
- image: dtzar/helm-kubectl:3
workingDir: /config
Expand Down

0 comments on commit d4bb209

Please sign in to comment.