Skip to content

Commit 6380cba

Browse files
Execute paths without the .tmpl extension as templates (#654)
## Changes The `.tmpl` extension is only meant as a qualifier for whether the file content is executed as a template. All file paths in the `template` directory should be treated as valid go text templates. Before only paths with the `.tmpl` extensions would be resolved as templates, after this change, all file paths are interpreted as templates. ## Tests Unit test. The newly added unit tests also asserts that the file path is correct, even when the `.tmpl` extension is missing.
1 parent 179b4f4 commit 6380cba

4 files changed

Lines changed: 32 additions & 8 deletions

File tree

libs/template/renderer.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -124,19 +124,29 @@ func (r *renderer) computeFile(relPathTemplate string) (file, error) {
124124
}
125125
perm := info.Mode().Perm()
126126

127+
// Execute relative path template to get destination path for the file
128+
relPath, err := r.executeTemplate(relPathTemplate)
129+
if err != nil {
130+
return nil, err
131+
}
132+
127133
// If file name does not specify the `.tmpl` extension, then it is copied
128134
// over as is, without treating it as a template
129135
if !strings.HasSuffix(relPathTemplate, templateExtension) {
130136
return &copyFile{
131137
dstPath: &destinationPath{
132138
root: r.instanceRoot,
133-
relPath: relPathTemplate,
139+
relPath: relPath,
134140
},
135141
perm: perm,
136142
ctx: r.ctx,
137143
srcPath: relPathTemplate,
138144
srcFiler: r.templateFiler,
139145
}, nil
146+
} else {
147+
// Trim the .tmpl suffix from file name, if specified in the template
148+
// path
149+
relPath = strings.TrimSuffix(relPath, templateExtension)
140150
}
141151

142152
// read template file's content
@@ -160,13 +170,6 @@ func (r *renderer) computeFile(relPathTemplate string) (file, error) {
160170
return nil, fmt.Errorf("failed to compute file content for %s. %w", relPathTemplate, err)
161171
}
162172

163-
// Execute relative path template to get materialized path for the file
164-
relPathTemplate = strings.TrimSuffix(relPathTemplate, templateExtension)
165-
relPath, err := r.executeTemplate(relPathTemplate)
166-
if err != nil {
167-
return nil, err
168-
}
169-
170173
return &inMemoryFile{
171174
dstPath: &destinationPath{
172175
root: r.instanceRoot,

libs/template/renderer_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,3 +459,17 @@ func TestRendererFileTreeRendering(t *testing.T) {
459459
assert.DirExists(t, filepath.Join(tmpDir, "my_directory"))
460460
assert.FileExists(t, filepath.Join(tmpDir, "my_directory", "my_file"))
461461
}
462+
463+
func TestRendererSubTemplateInPath(t *testing.T) {
464+
ctx := context.Background()
465+
tmpDir := t.TempDir()
466+
467+
r, err := newRenderer(ctx, nil, "./testdata/template-in-path/template", "./testdata/template-in-path/library", tmpDir)
468+
require.NoError(t, err)
469+
470+
err = r.walk()
471+
require.NoError(t, err)
472+
473+
assert.Equal(t, filepath.Join(tmpDir, "my_directory", "my_file"), r.files[0].DstPath().absPath())
474+
assert.Equal(t, "my_directory/my_file", r.files[0].DstPath().relPath)
475+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{{define "dir_name" -}}
2+
my_directory
3+
{{- end}}
4+
5+
{{define "file_name" -}}
6+
my_file
7+
{{- end}}

libs/template/testdata/template-in-path/template/{{template `dir_name`}}/{{template `file_name`}}

Whitespace-only changes.

0 commit comments

Comments
 (0)