minamijoyo / tfupdate

Update version constraints in your Terraform configurations
MIT License
539 stars 23 forks source link

Add version block even if block is missing #72

Open nitrocode opened 1 year ago

nitrocode commented 1 year ago

I noticed if I ran this, it doesn't return any output, and exits with 0 even though it did not make any modification to the module.

This module does not have a terraform { required_version = "x.y.z" } block.

tfupdate terraform -v ">= 1.0.0" my_module/

It would be nice if it would by default or by an additional option, it could add this block to a specified file, and if no specified file, it could add/append it to the module's versions.tf.

# if no required_version block, this creates a new versions.tf with this version set
tfupdate terraform -v ">= 1.0.0" my_module/
# if no required_version block, this creates a new my_versions.tf with this version set
tfupdate terraform -v ">= 1.0.0" my_module/my_versions.tf
minamijoyo commented 1 year ago

HI @nitrocode, thank you for your proposal.

For the user interface, I hesitate to enable it by default because tfupdate is intended to be applied recursively to multiple directories. Still, it is acceptable if it is added as a new opt-in flag.

The problem is that the current implementation rewrites each file on the fly. This means there is no way to detect whether requirements are defined or not in other files. We need to split the rewrite implementation into two phases; analyzing and updating. I can't start the implementation now but will consider it when refactoring.