Define variables as blocks with defaults

Signed-off-by: Patrick Van Stee <patrick@vanstee.me>
This commit is contained in:
Patrick Van Stee
2020-04-15 21:00:20 -04:00
parent 5185d534bc
commit f10d8dab5e
3 changed files with 45 additions and 10 deletions

View File

@ -11,9 +11,8 @@ import (
"github.com/zclconf/go-cty/cty/function/stdlib"
)
// Collection of functions expected to be generally useful in cty-using
// applications, which HCL supports. This set of functions will be available to
// be called in HCL files.
// Collection of generally useful functions in cty-using applications, which
// HCL supports. These functions are available for use in HCL files.
var (
functions = map[string]function.Function{
"absolute": stdlib.AbsoluteFunc,
@ -64,11 +63,35 @@ var (
}
)
// Used in the first pass of decoding instead of the Config struct to disallow
// interpolation while parsing variable blocks.
type staticConfig struct {
Variables []*Variable `hcl:"variable,block"`
Remain hcl.Body `hcl:",remain"`
}
func ParseHCL(dt []byte, fn string) (*Config, error) {
var sc staticConfig
// Decode only variable blocks without interpolation.
if err := hclsimple.Decode(fn, dt, nil, &sc); err != nil {
return nil, err
}
variables := make(map[string]cty.Value)
// Set all variables to their default value if defined.
for _, variable := range sc.Variables {
variables[variable.Name] = cty.StringVal(variable.Default)
}
// Override default with values from environment.
for _, env := range os.Environ() {
parts := strings.SplitN(env, "=", 2)
variables[parts[0]] = cty.StringVal(parts[1])
name, value := parts[0], parts[1]
if _, ok := variables[name]; ok {
variables[name] = cty.StringVal(value)
}
}
ctx := &hcl.EvalContext{
@ -77,6 +100,8 @@ func ParseHCL(dt []byte, fn string) (*Config, error) {
}
var c Config
// Decode with variables and functions.
if err := hclsimple.Decode(fn, dt, ctx, &c); err != nil {
return nil, err
}