refactor: add some more useful templating funcs (#4891)
parent
b6840a7cdc
commit
726850fe43
|
@ -78,6 +78,9 @@ The following functions which mimic the behaviour of helm exist in most templati
|
||||||
- kindIs
|
- kindIs
|
||||||
- default
|
- default
|
||||||
- empty
|
- empty
|
||||||
|
- indent
|
||||||
|
- nindent
|
||||||
|
- uuidv4
|
||||||
|
|
||||||
See the [Helm Documentation](https://helm.sh/docs/chart_template_guide/function_list/) for more information. Please
|
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.
|
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
|
#### iterate
|
||||||
|
|
||||||
Input is a single uint. Returns a slice of uints from 0 to the provided uint.
|
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.
|
||||||
|
|
|
@ -17,62 +17,68 @@ import (
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/google/uuid"
|
||||||
)
|
)
|
||||||
|
|
||||||
// FuncMap returns the template FuncMap commonly used in several templates.
|
// FuncMap returns the template FuncMap commonly used in several templates.
|
||||||
func FuncMap() map[string]any {
|
func FuncMap() map[string]any {
|
||||||
return map[string]any{
|
return map[string]any{
|
||||||
"iterate": FuncIterate,
|
"iterate": FuncIterate,
|
||||||
"env": FuncGetEnv,
|
"fileContent": FuncFileContent,
|
||||||
"expandenv": FuncExpandEnv,
|
"env": FuncGetEnv,
|
||||||
"split": FuncStringSplit,
|
"expandenv": FuncExpandEnv,
|
||||||
"splitList": FuncStringSplitList,
|
"split": FuncStringSplit,
|
||||||
"join": FuncElemsJoin,
|
"splitList": FuncStringSplitList,
|
||||||
"contains": FuncStringContains,
|
"join": FuncElemsJoin,
|
||||||
"hasPrefix": FuncStringHasPrefix,
|
"contains": FuncStringContains,
|
||||||
"hasSuffix": FuncStringHasSuffix,
|
"hasPrefix": FuncStringHasPrefix,
|
||||||
"lower": strings.ToLower,
|
"hasSuffix": FuncStringHasSuffix,
|
||||||
"keys": FuncKeys,
|
"lower": strings.ToLower,
|
||||||
"sortAlpha": FuncSortAlpha,
|
"keys": FuncKeys,
|
||||||
"upper": strings.ToUpper,
|
"sortAlpha": FuncSortAlpha,
|
||||||
"title": strings.ToTitle,
|
"upper": strings.ToUpper,
|
||||||
"trim": strings.TrimSpace,
|
"title": strings.ToTitle,
|
||||||
"trimAll": FuncStringTrimAll,
|
"trim": strings.TrimSpace,
|
||||||
"trimSuffix": FuncStringTrimSuffix,
|
"trimAll": FuncStringTrimAll,
|
||||||
"trimPrefix": FuncStringTrimPrefix,
|
"trimSuffix": FuncStringTrimSuffix,
|
||||||
"replace": FuncStringReplace,
|
"trimPrefix": FuncStringTrimPrefix,
|
||||||
"quote": FuncStringQuote,
|
"replace": FuncStringReplace,
|
||||||
"sha1sum": FuncHashSum(sha1.New),
|
"quote": FuncStringQuote,
|
||||||
"sha256sum": FuncHashSum(sha256.New),
|
"sha1sum": FuncHashSum(sha1.New),
|
||||||
"sha512sum": FuncHashSum(sha512.New),
|
"sha256sum": FuncHashSum(sha256.New),
|
||||||
"squote": FuncStringSQuote,
|
"sha512sum": FuncHashSum(sha512.New),
|
||||||
"now": time.Now,
|
"squote": FuncStringSQuote,
|
||||||
"b64enc": FuncB64Enc,
|
"now": time.Now,
|
||||||
"b64dec": FuncB64Dec,
|
"b64enc": FuncB64Enc,
|
||||||
"b32enc": FuncB32Enc,
|
"b64dec": FuncB64Dec,
|
||||||
"b32dec": FuncB32Dec,
|
"b32enc": FuncB32Enc,
|
||||||
"list": FuncList,
|
"b32dec": FuncB32Dec,
|
||||||
"dict": FuncDict,
|
"list": FuncList,
|
||||||
"get": FuncGet,
|
"dict": FuncDict,
|
||||||
"set": FuncSet,
|
"get": FuncGet,
|
||||||
"isAbs": path.IsAbs,
|
"set": FuncSet,
|
||||||
"base": path.Base,
|
"isAbs": path.IsAbs,
|
||||||
"dir": path.Dir,
|
"base": path.Base,
|
||||||
"ext": path.Ext,
|
"dir": path.Dir,
|
||||||
"clean": path.Clean,
|
"ext": path.Ext,
|
||||||
"osBase": filepath.Base,
|
"clean": path.Clean,
|
||||||
"osClean": filepath.Clean,
|
"osBase": filepath.Base,
|
||||||
"osDir": filepath.Dir,
|
"osClean": filepath.Clean,
|
||||||
"osExt": filepath.Ext,
|
"osDir": filepath.Dir,
|
||||||
"osIsAbs": filepath.IsAbs,
|
"osExt": filepath.Ext,
|
||||||
"deepEqual": reflect.DeepEqual,
|
"osIsAbs": filepath.IsAbs,
|
||||||
"typeOf": FuncTypeOf,
|
"deepEqual": reflect.DeepEqual,
|
||||||
"typeIs": FuncTypeIs,
|
"typeOf": FuncTypeOf,
|
||||||
"typeIsLike": FuncTypeIsLike,
|
"typeIs": FuncTypeIs,
|
||||||
"kindOf": FuncKindOf,
|
"typeIsLike": FuncTypeIsLike,
|
||||||
"kindIs": FuncKindIs,
|
"kindOf": FuncKindOf,
|
||||||
"default": FuncDefault,
|
"kindIs": FuncKindIs,
|
||||||
"empty": FuncEmpty,
|
"default": FuncDefault,
|
||||||
|
"empty": FuncEmpty,
|
||||||
|
"indent": FuncIndent,
|
||||||
|
"nindent": FuncNewlineIndent,
|
||||||
|
"uuidv4": FuncUUIDv4,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -384,3 +390,31 @@ func FuncEmpty(v any) bool {
|
||||||
return false
|
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
|
||||||
|
}
|
||||||
|
|
|
@ -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))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue