Skip to content

Commit c10343f

Browse files
authored
Merge pull request #612 from nginx-proxy/comment-function
feat: comment template function
2 parents 82bddc6 + b7422a2 commit c10343f

File tree

4 files changed

+27
-0
lines changed

4 files changed

+27
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,7 @@ For example, this is a JSON version of an emitted RuntimeContainer struct:
377377
- [Functions from Sprig v3](https://masterminds.github.io/sprig/), except for those that have the same name as one of the following functions.
378378
- _`closest $array $value`_: Returns the longest matching substring in `$array` that matches `$value`
379379
- _`coalesce ...`_: Returns the first non-nil argument.
380+
- _`comment $delimiter $string`_: Returns `$string` with each line prefixed by `$delimiter` (helpful for debugging combined with Sprig `toPrettyJson`: `{{ toPrettyJson $ | comment "#" }}`).
380381
- _`contains $map $key`_: Returns `true` if `$map` contains `$key`. Takes maps from `string` to any type.
381382
- _`dir $path`_: Returns an array of filenames in the specified `$path`.
382383
- _`exists $path`_: Returns `true` if `$path` refers to an existing file or directory. Takes a string.

internal/template/functions.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"log"
1010
"os"
1111
"reflect"
12+
"regexp"
1213
"strings"
1314
)
1415

@@ -57,6 +58,12 @@ func intersect(l1, l2 []string) []string {
5758
return keys
5859
}
5960

61+
// comment prefix each line of the source string with the provided comment delimiter string
62+
func comment(delimiter string, source string) string {
63+
regexPattern := regexp.MustCompile(`(?m)^`)
64+
return regexPattern.ReplaceAllString(source, delimiter)
65+
}
66+
6067
func contains(input interface{}, key interface{}) bool {
6168
if input == nil {
6269
return false

internal/template/functions_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,24 @@ import (
1212
"github.com/stretchr/testify/assert"
1313
)
1414

15+
func TestComment(t *testing.T) {
16+
env := map[string]string{
17+
"bar": "baz",
18+
"foo": "test",
19+
}
20+
21+
expected := `# {
22+
# "bar": "baz",
23+
# "foo": "test"
24+
# }`
25+
26+
tests := templateTestList{
27+
{`{{toPrettyJson . | comment "# "}}`, env, expected},
28+
}
29+
30+
tests.run(t)
31+
}
32+
1533
func TestContainsString(t *testing.T) {
1634
env := map[string]string{
1735
"PORT": "1234",

internal/template/template.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ func newTemplate(name string) *template.Template {
6060
tmpl.Funcs(sprig.TxtFuncMap()).Funcs(template.FuncMap{
6161
"closest": arrayClosest,
6262
"coalesce": coalesce,
63+
"comment": comment,
6364
"contains": contains,
6465
"dir": dirList,
6566
"eval": eval,

0 commit comments

Comments
 (0)