refactor: add some more useful templating funcs (#4891)

pull/4892/head^2
James Elliott 2023-02-08 01:28:09 +11:00 committed by GitHub
parent b6840a7cdc
commit 726850fe43
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 112 additions and 51 deletions

View File

@ -78,6 +78,9 @@ The following functions which mimic the behaviour of helm exist in most templati
- kindIs
- default
- empty
- indent
- nindent
- uuidv4
See the [Helm Documentation](https://helm.sh/docs/chart_template_guide/function_list/) for more information. Please
note that only the functions listed above are supported and the functions don't necessarily behave exactly the same.
@ -92,3 +95,7 @@ The following is a list of special functions and their syntax.
#### iterate
Input is a single uint. Returns a slice of uints from 0 to the provided uint.
#### fileContent
Input is a path. Returns the content of a file.

View File

@ -17,12 +17,15 @@ import (
"strconv"
"strings"
"time"
"github.com/google/uuid"
)
// FuncMap returns the template FuncMap commonly used in several templates.
func FuncMap() map[string]any {
return map[string]any{
"iterate": FuncIterate,
"fileContent": FuncFileContent,
"env": FuncGetEnv,
"expandenv": FuncExpandEnv,
"split": FuncStringSplit,
@ -73,6 +76,9 @@ func FuncMap() map[string]any {
"kindIs": FuncKindIs,
"default": FuncDefault,
"empty": FuncEmpty,
"indent": FuncIndent,
"nindent": FuncNewlineIndent,
"uuidv4": FuncUUIDv4,
}
}
@ -384,3 +390,31 @@ func FuncEmpty(v any) bool {
return false
}
}
// FuncIndent is a helper function that provides similar functionality to the helm indent func.
func FuncIndent(indent int, value string) string {
padding := strings.Repeat(" ", indent)
return padding + strings.ReplaceAll(value, "\n", "\n"+padding)
}
// FuncNewlineIndent is a helper function that provides similar functionality to the helm nindent func.
func FuncNewlineIndent(indent int, value string) string {
return "\n" + FuncIndent(indent, value)
}
// FuncUUIDv4 is a helper function that provides similar functionality to the helm uuidv4 func.
func FuncUUIDv4() string {
return uuid.New().String()
}
// FuncFileContent returns the file content.
func FuncFileContent(path string) (data string, err error) {
var raw []byte
if raw, err = os.ReadFile(path); err != nil {
return "", err
}
return string(raw), nil
}

View File

@ -638,3 +638,23 @@ func TestFuncEmpty(t *testing.T) {
})
}
}
func TestFuncIndent(t *testing.T) {
testCases := []struct {
name string
have string
indent int
expected []string
}{
{"ShouldIndentZeroMultiLine", "abc\n123", 0, []string{"abc\n123", "\nabc\n123"}},
{"ShouldIndentOneMultiLine", "abc\n123", 1, []string{" abc\n 123", "\n abc\n 123"}},
{"ShouldIndentOneSingleLine", "abc", 1, []string{" abc", "\n abc"}},
{"ShouldIndentZeroSingleLine", "abc", 0, []string{"abc", "\nabc"}},
}
for _, tc := range testCases {
for i, f := range []func(i int, v string) string{FuncIndent, FuncNewlineIndent} {
assert.Equal(t, tc.expected[i], f(tc.indent, tc.have))
}
}
}