Skip to content

Commit

Permalink
feat: use comments instead of frontmatter
Browse files Browse the repository at this point in the history
  • Loading branch information
adityathebe committed Jul 26, 2024
1 parent 9e842f2 commit 3fe0463
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 34 deletions.
51 changes: 30 additions & 21 deletions template.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,18 +257,18 @@ func LoadSharedLibrary(source string) error {
}

func parseAndStripTemplateHeader(template Template) (Template, error) {
fm, content := extractFrontmatterAndContent(template.Template)
if fm == "" {
header, content := extractHeaderAndContent(template.Template)
if header == "" {
return template, nil
}

template.Template = content
scanner := bufio.NewScanner(strings.NewReader(fm))
for scanner.Scan() {
line := scanner.Text()
split := strings.SplitN(line, "=", 2)

fields := strings.Fields(header)
for _, field := range fields {
split := strings.SplitN(field, "=", 2)
if len(split) != 2 {
return template, fmt.Errorf("invalid header: %s", line)
return template, fmt.Errorf("invalid header: %s", field)
}

switch split[0] {
Expand All @@ -282,23 +282,32 @@ func parseAndStripTemplateHeader(template Template) (Template, error) {
return template, nil
}

func extractFrontmatterAndContent(input string) (string, string) {
const delimiter = "---"
const templateHeaderPrefix = "# gotemplate: "

input = strings.TrimSpace(input)
func extractHeaderAndContent(template string) (string, string) {
scanner := bufio.NewScanner(strings.NewReader(template))

if !strings.HasPrefix(input, delimiter) {
return "", input
}
// Loop through headers.
// There could be multiple, we look for the gotemplate header.
for scanner.Scan() {
line := scanner.Text()

endIndex := strings.Index(input[len(delimiter):], delimiter)
if endIndex == -1 {
return "", input
}
if line == "---" {
// Special case for yaml where the header might not start from the first line.
continue
}

// end of headers.
isHeader := strings.HasPrefix(line, "#")
if !isHeader {
break
}

frontmatterEndIndex := endIndex + 2*len(delimiter)
frontmatter := strings.TrimSpace(input[len(delimiter) : endIndex+len(delimiter)])
content := strings.TrimSpace(input[frontmatterEndIndex:])
if strings.HasPrefix(line, templateHeaderPrefix) {
header := strings.TrimPrefix(line, templateHeaderPrefix)
return header, strings.Replace(template, fmt.Sprintf("%s\n", line), "", 1)
}
}

return frontmatter, content
return "", template
}
25 changes: 17 additions & 8 deletions tests/gomplate_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package tests

import (
"fmt"
"os"
"testing"
"time"
Expand Down Expand Up @@ -84,20 +85,28 @@ func TestGomplate(t *testing.T) {

func TestGomplateHeaders(t *testing.T) {
tests := []struct {
env map[string]interface{}
template string
out string
env map[string]interface{}
template string
out string
expectErr bool
}{
{map[string]interface{}{"name": "world"}, readFile(t, "testdata/template-header.txt"), "Hello, world"},
{map[string]interface{}{"name": "world"}, readFile(t, "testdata/gotemplate/template-multiple-headers.yaml"), readFile(t, "testdata/gotemplate/expected/template-multiple-headers.yaml"), false},
{map[string]interface{}{"name": "world"}, readFile(t, "testdata/gotemplate/template-header.txt"), "Hello, world", false},
{map[string]interface{}{"name": "world"}, readFile(t, "testdata/gotemplate/bad-template-header.txt"), "", true},
{map[string]interface{}{"name": "world"}, readFile(t, "testdata/gotemplate/template-header-override.txt"), "Hello, world. This ${should} not be {{touched}}", false},
}

for _, tc := range tests {
t.Run(tc.template, func(t *testing.T) {
for i, tc := range tests {
t.Run(fmt.Sprintf("%d", i+1), func(t *testing.T) {
out, err := gomplate.RunTemplate(tc.env, gomplate.Template{
Template: tc.template,
})
assert.ErrorIs(t, err, nil)
assert.Equal(t, tc.out, out)
if tc.expectErr {
assert.Error(t, err)
} else {
assert.ErrorIs(t, err, nil)
assert.Equal(t, tc.out, out)
}
})
}
}
Expand Down
2 changes: 2 additions & 0 deletions tests/testdata/gotemplate/bad-template-header.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# gotemplate: left-delim=$[ [ right-delim=]]
Hello, $[[.name]]
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
# yaml-language-server: $schema=https://raw.githubusercontent.com/fluxcd-community/flux2-schemas/main/helmrepository-source-v1beta2.json
name: Hello world
2 changes: 2 additions & 0 deletions tests/testdata/gotemplate/template-header-override.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# gotemplate: left-delim=$[[ right-delim=]]
Hello, $[[.name]]. This ${should} not be {{touched}}
2 changes: 2 additions & 0 deletions tests/testdata/gotemplate/template-header.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# gotemplate: left-delim=$[[ right-delim=]]
Hello, $[[.name]]
4 changes: 4 additions & 0 deletions tests/testdata/gotemplate/template-multiple-headers.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
# yaml-language-server: $schema=https://raw.githubusercontent.com/fluxcd-community/flux2-schemas/main/helmrepository-source-v1beta2.json
# gotemplate: left-delim=<<< right-delim=>>>
name: Hello <<<.name>>>
5 changes: 0 additions & 5 deletions tests/testdata/template-header.txt

This file was deleted.

0 comments on commit 3fe0463

Please sign in to comment.