in infra/module-swapper/cmd/swap.go [357:383]
func writeModuleRefs(f []byte, p string, moduleRefs map[string]string) ([]byte, error) {
wf, diags := hclwrite.ParseConfig(f, p, hcl.Pos{})
if diags.HasErrors() {
return nil, fmt.Errorf("failed to parse hcl: %v", diags.Error())
}
for _, b := range wf.Body().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
}
newSource, exists := moduleRefs[b.Labels()[0]]
if !exists {
continue
}
b.Body().SetAttributeValue(sourceAttrib, cty.StringVal(newSource))
}
var testS strings.Builder
_, err := wf.WriteTo(&testS)
if err != nil {
return nil, fmt.Errorf("failed to write hcl: %v", diags.Error())
}
return []byte(testS.String()), nil
}