Skip to content

feat: comment template function #612

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,7 @@ For example, this is a JSON version of an emitted RuntimeContainer struct:
- [Functions from Sprig v3](https://masterminds.github.io/sprig/), except for those that have the same name as one of the following functions.
- _`closest $array $value`_: Returns the longest matching substring in `$array` that matches `$value`
- _`coalesce ...`_: Returns the first non-nil argument.
- _`comment $delimiter $string`_: Returns `$string` with each line prefixed by `$delimiter` (helpful for debugging combined with Sprig `toPrettyJson`: `{{ toPrettyJson $ | comment "#" }}`).
- _`contains $map $key`_: Returns `true` if `$map` contains `$key`. Takes maps from `string` to any type.
- _`dir $path`_: Returns an array of filenames in the specified `$path`.
- _`exists $path`_: Returns `true` if `$path` refers to an existing file or directory. Takes a string.
Expand Down
7 changes: 7 additions & 0 deletions internal/template/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"log"
"os"
"reflect"
"regexp"
"strings"
)

Expand Down Expand Up @@ -57,6 +58,12 @@ func intersect(l1, l2 []string) []string {
return keys
}

// comment prefix each line of the source string with the provided comment delimiter string
func comment(delimiter string, source string) string {
regexPattern := regexp.MustCompile(`(?m)^`)
return regexPattern.ReplaceAllString(source, delimiter)
}

func contains(input interface{}, key interface{}) bool {
if input == nil {
return false
Expand Down
18 changes: 18 additions & 0 deletions internal/template/functions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,24 @@ import (
"github.com/stretchr/testify/assert"
)

func TestComment(t *testing.T) {
env := map[string]string{
"bar": "baz",
"foo": "test",
}

expected := `# {
# "bar": "baz",
# "foo": "test"
# }`

tests := templateTestList{
{`{{toPrettyJson . | comment "# "}}`, env, expected},
}

tests.run(t)
}

func TestContainsString(t *testing.T) {
env := map[string]string{
"PORT": "1234",
Expand Down
1 change: 1 addition & 0 deletions internal/template/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ func newTemplate(name string) *template.Template {
tmpl.Funcs(sprig.TxtFuncMap()).Funcs(template.FuncMap{
"closest": arrayClosest,
"coalesce": coalesce,
"comment": comment,
"contains": contains,
"dir": dirList,
"eval": eval,
Expand Down