feat(template): add readFile

closes #4518
This commit is contained in:
Jan De Dobbeleer 2023-12-04 19:41:58 +01:00 committed by Jan De Dobbeleer
parent 35823e5b78
commit 5493ddb640
3 changed files with 11 additions and 1 deletions

View file

@ -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)
}

View file

@ -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 {

View file

@ -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>\{\{ .Code &vert; hresult \}\}</code> | 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. |
<!-- markdownlint-enable MD013 -->