github.com/sfdevops1/terrra4orm@v0.11.12-beta1/configs/parser_values.go (about)

     1  package configs
     2  
     3  import (
     4  	"github.com/hashicorp/hcl2/hcl"
     5  	"github.com/zclconf/go-cty/cty"
     6  )
     7  
     8  // LoadValuesFile reads the file at the given path and parses it as a "values
     9  // file", which is an HCL config file whose top-level attributes are treated
    10  // as arbitrary key.value pairs.
    11  //
    12  // If the file cannot be read -- for example, if it does not exist -- then
    13  // a nil map will be returned along with error diagnostics. Callers may wish
    14  // to disregard the returned diagnostics in this case and instead generate
    15  // their own error message(s) with additional context.
    16  //
    17  // If the returned diagnostics has errors when a non-nil map is returned
    18  // then the map may be incomplete but should be valid enough for careful
    19  // static analysis.
    20  //
    21  // This method wraps LoadHCLFile, and so it inherits the syntax selection
    22  // behaviors documented for that method.
    23  func (p *Parser) LoadValuesFile(path string) (map[string]cty.Value, hcl.Diagnostics) {
    24  	body, diags := p.LoadHCLFile(path)
    25  	if body == nil {
    26  		return nil, diags
    27  	}
    28  
    29  	vals := make(map[string]cty.Value)
    30  	attrs, attrDiags := body.JustAttributes()
    31  	diags = append(diags, attrDiags...)
    32  	if attrs == nil {
    33  		return vals, diags
    34  	}
    35  
    36  	for name, attr := range attrs {
    37  		val, valDiags := attr.Expr.Value(nil)
    38  		diags = append(diags, valDiags...)
    39  		vals[name] = val
    40  	}
    41  
    42  	return vals, diags
    43  }