diff --git a/src/template/files.go b/src/template/files.go index 9d136a14..862ef604 100644 --- a/src/template/files.go +++ b/src/template/files.go @@ -1,6 +1,9 @@ package template -import "path/filepath" +import ( + "os" + "path/filepath" +) func glob(pattern string) (bool, error) { matches, err := filepath.Glob(pattern) @@ -9,3 +12,8 @@ func glob(pattern string) (bool, error) { } return len(matches) > 0, nil } + +func readFile(path string) string { + content, _ := os.ReadFile(path) + return string(content) +} diff --git a/src/template/func_map.go b/src/template/func_map.go index 3e9e9fb7..0a13d03e 100644 --- a/src/template/func_map.go +++ b/src/template/func_map.go @@ -19,6 +19,7 @@ func funcMap() template.FuncMap { "reason": GetReasonFromStatus, "hresult": hresult, "trunc": trunc, + "readFile": readFile, } for key, fun := range sprig.TxtFuncMap() { if _, ok := funcMap[key]; !ok { diff --git a/website/docs/configuration/templates.mdx b/website/docs/configuration/templates.mdx index b8d063fb..d4f6859d 100644 --- a/website/docs/configuration/templates.mdx +++ b/website/docs/configuration/templates.mdx @@ -169,6 +169,7 @@ use them. | `{{ if matchP "*.Repo" .Path }}Repo{{ else }}No Repo{{ end }}` | Exposes [regexp.MatchString][regexpms] as a boolean template function. | | `{{ replaceP "c.t" "cut code cat" "dog" }}` | Exposes [regexp.ReplaceAllString][regexpra] as a string template function. | | \{\{ .Code | hresult \}\} | Transform a status code to its HRESULT value for easy troubleshooting. For example `-1978335212` becomes `0x8A150014`. | +| `{{ readFile ".version.json" }}` | Read a file in the current directory. Returns a string. |