func moduleSourceRefs()

in infra/module-swapper/cmd/swap.go [314:354]


func moduleSourceRefs(f []byte, TFFilePath string) (map[string]string, error) {
	refs := map[string]string{}
	p, err := hclparse.NewParser().ParseHCL(f, TFFilePath)
	if err != nil {
		return nil, fmt.Errorf("failed to parse hcl: %v", err)
	}
	c, _, diags := p.Body.PartialContent(exampleSchema)
	if diags.HasErrors() {
		return nil, fmt.Errorf("failed to parse example content: %v", diags.Error())
	}

	for _, b := range c.Blocks {
		if b.Type != moduleBlockType {
			continue
		}
		if len(b.Labels) != 1 {
			log.Printf("got multiple labels %v, module should only have one", b.Labels)
			continue
		}

		content, _, diags := b.Body.PartialContent(moduleSchema)
		if diags.HasErrors() {
			log.Printf("skipping %s module, failed to parse module content: %v", b.Labels[0], diags.Error())
			continue
		}

		sourcrAttr, exists := content.Attributes[sourceAttrib]
		if !exists {
			log.Printf("skipping %s module, no source attribute", b.Labels[0])
			continue
		}
		var sourceName string
		diags = gohcl.DecodeExpression(sourcrAttr.Expr, nil, &sourceName)
		if diags.HasErrors() {
			log.Printf("skipping %s module, failed to decode source value: %v", b.Labels[0], diags.Error())
			continue
		}
		refs[b.Labels[0]] = sourceName
	}
	return refs, nil
}