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