busser / tfautomv

Generate Terraform moved blocks automatically for painless refactoring
https://github.com/busser/tfautomv
Apache License 2.0
705 stars 23 forks source link

Missing newline when appending to `moves.tf` after second run of `tfautomv` #81

Closed untcha closed 9 months ago

untcha commented 9 months ago

Hi!

first of all: tfautomv is awesome! It is currently saving us a lot of work!

We are currently in the progress of refactoring an AWS VPC module which needs to be applied to a lot of VPCs afterward. We are executing tfautomv in two terragrunt hooks:

terraform {
  source = local.module_source

  before_hook "tfautomv_1" {
    commands = ["plan"]
    execute  = ["tfautomv", "-vvv", "--skip-init", "--skip-refresh"]
  }

  before_hook "tfautomv_2" {
    commands = ["plan"]
    execute  = ["tfautomv", "-vvv", "--skip-init", "--skip-refresh"]
  }
}

The reason for executing tfautomv twice is the fact that the moves cannot be identified correctly for vpc_route_table_associations since terraform does not know the subnet_id and route_table_id after the first plan.

When executing tfautomv a second time the moved blocks are not correctly appended to the moves.tf resulting in the following error:

│ Error: Missing newline after block definition
│ 
│   on moves.tf line 80, in moved:
│   77: moved {
│   78:   from = module.this.aws_vpc_ipv4_cidr_block_association.this[1]
│   79:   to   = aws_vpc_ipv4_cidr_block_association.this["nr_172.16.0.0/20"]
│   80: }moved {
│ 
│ A block definition must end with a newline.

In tfautomv\pkg\terraform\move.go I added a \n to:

func (m Move) block() string {
    return fmt.Sprintf("moved {\n  from = %s\n  to   = %s\n}\n", m.FromAddress, m.ToAddress)
}

With that small change, our approach is successful and moved blocks are correctly written in the first run and appended correctly in the second run.

It would be awesome if you could implement a corresponding fix.

Thanks a lot!

busser commented 9 months ago

Thanks for bringing this to my attention! I'll get right on it 😄

busser commented 9 months ago

Fixed in release v0.6.1. Thanks again for reporting this!

untcha commented 9 months ago

Thanks a lot!!! :-)