Add read function to read the content of a file

This commit is contained in:
nicolas.dorier 2019-03-16 20:07:40 +09:00
parent 4edc190faa
commit 18ba39e768
2 changed files with 17 additions and 0 deletions

View File

@ -364,6 +364,7 @@ For example, this is a JSON version of an emitted RuntimeContainer struct:
* *`keys $map`*: Returns the keys from `$map`. If `$map` is `nil`, a `nil` is returned. If `$map` is not a `map`, an error will be thrown.
* *`last $array`*: Returns the last value of an array.
* *`parseBool $string`*: parseBool returns the boolean value represented by the string. It accepts 1, t, T, TRUE, true, True, 0, f, F, FALSE, false, False. Any other value returns an error. Alias for [`strconv.ParseBool`](http://golang.org/pkg/strconv/#ParseBool)
* *`read $string`*: Read the content of the file located at `$path`
* *`replace $string $old $new $count`*: Replaces up to `$count` occurences of `$old` with `$new` in `$string`. Alias for [`strings.Replace`](http://golang.org/pkg/strings/#Replace)
* *`sha1 $string`*: Returns the hexadecimal representation of the SHA1 hash of `$string`.
* *`split $string $sep`*: Splits `$string` into a slice of substrings delimited by `$sep`. Alias for [`strings.Split`](http://golang.org/pkg/strings/#Split)

View File

@ -31,6 +31,21 @@ func exists(path string) (bool, error) {
return false, err
}
func read(path string) (string, error) {
_, err := os.Stat(path)
if err != nil {
if os.IsNotExist(err) {
return "", nil
}
return "", err
}
b, err := ioutil.ReadFile(path)
if err != nil {
return "", err
}
return string(b), nil
}
func getArrayValues(funcName string, entries interface{}) (*reflect.Value, error) {
entriesVal := reflect.ValueOf(entries)
@ -426,6 +441,7 @@ func newTemplate(name string) *template.Template {
"dict": dict,
"dir": dirList,
"exists": exists,
"read": read,
"first": arrayFirst,
"groupBy": groupBy,
"groupByKeys": groupByKeys,