func parseBlueprintVersion()

in cli/bpmetadata/tfconfig.go [141:208]


func parseBlueprintVersion(versionsFile *hcl.File, diags hcl.Diagnostics) (string, error) {
	re := regexp.MustCompile(versionRegEx)
	// PartialContent() returns TF content containing blocks and attributes
	// based on the provided schema
	rootContent, _, rootContentDiags := versionsFile.Body.PartialContent(rootSchema)
	diags = append(diags, rootContentDiags...)
	err := hasHclErrors(diags)
	if err != nil {
		return "", err
	}

	// based on the content returned, iterate through blocks and look for
	// the terraform block specfically
	for _, rootBlock := range rootContent.Blocks {
		if rootBlock.Type != "terraform" {
			continue
		}

		// do a PartialContent() call again but now for the provider_meta block
		// within the terraform block
		tfContent, _, tfContentDiags := rootBlock.Body.PartialContent(metaSchema)
		diags = append(diags, tfContentDiags...)
		err := hasHclErrors(diags)
		if err != nil {
			return "", err
		}

		for _, tfContentBlock := range tfContent.Blocks {
			if tfContentBlock.Type != "provider_meta" {
				continue
			}

			// this PartialContent() call with get the module_name attribute
			// that contains the version info
			metaContent, _, metaContentDiags := tfContentBlock.Body.PartialContent(metaBlockSchema)
			diags = append(diags, metaContentDiags...)
			err := hasHclErrors(diags)
			if err != nil {
				return "", err
			}

			versionAttr, defined := metaContent.Attributes["module_name"]
			if !defined {
				return "", fmt.Errorf("module_name not defined for provider_meta")
			}

			// get the module name from the version attribute and extract the
			// version name only
			var modName string
			diags := gohcl.DecodeExpression(versionAttr.Expr, nil, &modName)
			err = hasHclErrors(diags)
			if err != nil {
				return "", err
			}

			m := re.FindStringSubmatch(modName)
			if len(m) > 0 {
				return m[len(m)-1], nil
			}

			return "", nil
		}

		break
	}

	return "", nil
}