AlexNabokikh / tfsort

A CLI utility to sort Terraform variables and outputs
Apache License 2.0
138 stars 5 forks source link

Preserve comments #36

Open nikolaik opened 1 year ago

nikolaik commented 1 year ago

Hi and thanks for this nice little tool. Would love it if could also preserve comments

AlexNabokikh commented 1 year ago

Hey @nikolaik, Thank you for your feedback. I appreciate your insights on preserving comments in outputs and variable files. However, it may be challenging to maintain the order of these comments when sorting. As a result, a comment that was added to a specific variable might end up at the top or bottom of the file after sorting, rendering it less useful.

I understand that people often use comments to logically group variable blocks based on their purposes, such as RDS-related vars or SQS-related vars. However, when we decide to sort variables alphabetically - using comments doesn't make much sense anymore. Terraform's description argument is a suitable alternative that serves the purpose of comments and documentation.

I would be interested in hearing more about how you envision this implementation. It would be helpful to see some code examples before and after the proposed changes. Thank you again for your feedback!

timduhenchanter commented 1 year ago

If the comments were declared inside of the variable block I imagine this would be pretty easy to implement. I would support and request this addition. description provides insight for any consumer of the variable but comments are more effective for giving wider definitions to the variable especially with complex modules.

AlexNabokikh commented 1 year ago

@timduhenchanter Well, I think I don't understand the request. If the comment is declared within the variable or outputs block, tfsort will preserve them. Did you mean some specific scenario?

nikolaik commented 1 year ago

Thanks for the reply @AlexNabokikh.

I understand that people often use comments to logically group variable blocks based on their purposes, such as RDS-related vars or SQS-related vars. However, when we decide to sort variables alphabetically - using comments doesn't make much sense anymore. Terraform's description argument is a suitable alternative that serves the purpose of comments and documentation.

That is our main use case for comments in variables.tf files too, grouping a list of variable blocks. I don't necessarily think grouping variables like that is the way to go. Alternatives that come to mind are:

One grouping I often see are required variables, but I'm not sure if that is worth the hassle.

Moving all of the comments to the top of the file might be less surprising, than stripping them completely