pkg/config: add eval func `file`
Similar to `file` in terraform:
@@ -2,6 +2,7 @@ package config
import (
"fmt"
+ "io/ioutil"
"path/filepath"
"github.com/hashicorp/hcl2/gohcl"
@@ -119,6 +120,7 @@ func LoadConfig(configDir string) (*Config, hcl.Diagnostics) {
},
Functions: map[string]function.Function{
"pathexpand": evalFuncPathExpand(),
+ "file": evalFuncFile(),
},
}
@@ -143,6 +145,22 @@ func evalFuncPathExpand() function.Function {
})
}
+func evalFuncFile() function.Function {
+ return function.New(&function.Spec{
+ Params: []function.Parameter{
+ {
+ Name: "path",
+ Type: cty.String,
+ }},
+ Type: function.StaticReturnType(cty.String),
+ Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {
+ filePath := args[0].AsString()
+ content, err := ioutil.ReadFile(filePath)
+ return cty.StringVal(string(content)), err
+ },
+ })
+}
+
// LoadValuesFile reads the file at the given path and parses it as a
// "values file" (flat key.value HCL config) for later use in the
// `EvalContext`.
