func()

in pkg/transform_rename_block_element.go [61:92]


func (r *RenameAttributeOrNestedBlockTransform) rename(blocks []terraform.Block, attributePath []string, newName string, renameOnlyNewNameAbsent bool) {
	if len(attributePath) == 1 {
		old := attributePath[0]
		for _, b := range blocks {
			body := b.WriteBody()
			attributes := body.Attributes()
			attr, ok := attributes[old]
			if ok {
				if _, newNameExist := attributes[newName]; !newNameExist || !renameOnlyNewNameAbsent {
					body.SetAttributeRaw(newName, attr.Expr().BuildTokens(nil))
				}
				body.RemoveAttribute(old)
				continue
			}
			for _, nb := range body.Blocks() {
				if r.nestedBlockType(nb) != old {
					continue
				}
				r.setNestedBlockType(nb, old, newName)
			}
		}
		return
	}
	nbName := attributePath[0]
	for _, b := range blocks {
		nestedBlocks, ok := b.GetNestedBlocks()[nbName]
		if !ok {
			continue
		}
		r.rename(castBlockSlice(nestedBlocks), attributePath[1:], newName, false)
	}
}