mirror of
https://github.com/opentofu/opentofu.git
synced 2024-12-30 10:47:14 -06:00
05e3b47ba1
This method loads a "values file" -- also known as a "tfvars file" -- and returns the values found inside. A values file is an HCL file (in either native or JSON syntax) whose top-level body is treated as a set of arbitrary key/value pairs whose values may not depend on any variables or functions. We will load values files through a configs.Parser -- even though values files are not strictly-speaking part of configuration -- because this causes them to be registered in our source code cache so that we can generate source code snippets if we need to report any diagnostics.
44 lines
1.3 KiB
Go
44 lines
1.3 KiB
Go
package configs
|
|
|
|
import (
|
|
"github.com/hashicorp/hcl2/hcl"
|
|
"github.com/zclconf/go-cty/cty"
|
|
)
|
|
|
|
// LoadValuesFile reads the file at the given path and parses it as a "values
|
|
// file", which is an HCL config file whose top-level attributes are treated
|
|
// as arbitrary key.value pairs.
|
|
//
|
|
// If the file cannot be read -- for example, if it does not exist -- then
|
|
// a nil map will be returned along with error diagnostics. Callers may wish
|
|
// to disregard the returned diagnostics in this case and instead generate
|
|
// their own error message(s) with additional context.
|
|
//
|
|
// If the returned diagnostics has errors when a non-nil map is returned
|
|
// then the map may be incomplete but should be valid enough for careful
|
|
// static analysis.
|
|
//
|
|
// This method wraps LoadHCLFile, and so it inherits the syntax selection
|
|
// behaviors documented for that method.
|
|
func (p *Parser) LoadValuesFile(path string) (map[string]cty.Value, hcl.Diagnostics) {
|
|
body, diags := p.LoadHCLFile(path)
|
|
if body == nil {
|
|
return nil, diags
|
|
}
|
|
|
|
vals := make(map[string]cty.Value)
|
|
attrs, attrDiags := body.JustAttributes()
|
|
diags = append(diags, attrDiags...)
|
|
if attrs == nil {
|
|
return vals, diags
|
|
}
|
|
|
|
for name, attr := range attrs {
|
|
val, valDiags := attr.Expr.Value(nil)
|
|
diags = append(diags, valDiags...)
|
|
vals[name] = val
|
|
}
|
|
|
|
return vals, diags
|
|
}
|