func readConfigStr()

in tools/test-reader/reader/reader.go [308:365]


func readConfigStr(configStr string) (Step, error) {
	// Remove fmt substitutions because they interfere with hcl parsing.
	// Replace with a value that can be parsed outside quotation marks.
	configStr = subPattern.ReplaceAllString(configStr, "true")
	parser := hclparse.NewParser()
	file, diagnostics := parser.ParseHCL([]byte(configStr), "config.hcl")
	if diagnostics.HasErrors() {
		return nil, fmt.Errorf("errors parsing hcl: %v", diagnostics.Errs())
	}
	content, diagnostics := file.Body.Content(&hcl.BodySchema{
		Blocks: []hcl.BlockHeaderSchema{
			{
				Type:       "resource",
				LabelNames: []string{"type", "name"},
			},
			{
				Type:       "data",
				LabelNames: []string{"type", "name"},
			},
			{
				Type:       "output",
				LabelNames: []string{"name"},
			},
			{
				Type:       "provider",
				LabelNames: []string{"name"},
			},
			{
				Type: "locals",
			},
		},
	})
	if diagnostics.HasErrors() {
		return nil, fmt.Errorf("errors getting hcl body content: %v", diagnostics.Errs())
	}
	m := make(map[string]Resources)
	errs := make([]error, 0)
	for _, block := range content.Blocks {
		if len(block.Labels) != 2 {
			continue
		}
		if _, ok := m[block.Labels[0]]; !ok {
			// Create an empty map for this resource type.
			m[block.Labels[0]] = make(Resources)
		}
		// Use the resource name as a key.
		resourceConfig, err := readHCLBlockBody(block.Body, file.Bytes)
		if err != nil {
			errs = append(errs, err)
		}
		resourceConfig = flattenResource(resourceConfig, "")
		m[block.Labels[0]][block.Labels[1]] = resourceConfig
	}
	if len(errs) > 0 {
		return m, fmt.Errorf("errors reading hcl blocks: %v", errs)
	}
	return m, nil
}