Skip to content
This repository was archived by the owner on Jun 27, 2025. It is now read-only.

Commit f614622

Browse files
authored
Merge pull request #261 from jrasell/gh-260
Add fileContents template func to read a file into a template.
2 parents 714d7f9 + 59ef4da commit f614622

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

template/funcs.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"encoding/json"
55
"errors"
66
"fmt"
7+
"io/ioutil"
78
"os"
89
"strconv"
910
"strings"
@@ -22,6 +23,7 @@ func funcMap(consulClient *consul.Client) template.FuncMap {
2223
"consulKeyExists": consulKeyExistsFunc(consulClient),
2324
"consulKeyOrDefault": consulKeyOrDefaultFunc(consulClient),
2425
"env": envFunc(),
26+
"fileContents": fileContents(),
2527
"loop": loop,
2628
"parseBool": parseBool,
2729
"parseFloat": parseFloat,
@@ -232,3 +234,16 @@ func envFunc() func(string) (string, error) {
232234
return os.Getenv(s), nil
233235
}
234236
}
237+
238+
func fileContents() func(string) (string, error) {
239+
return func(s string) (string, error) {
240+
if s == "" {
241+
return "", nil
242+
}
243+
contents, err := ioutil.ReadFile(s)
244+
if err != nil {
245+
return "", err
246+
}
247+
return string(contents), nil
248+
}
249+
}

0 commit comments

Comments
 (0)