Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hook to sort definition blocks in HCL files #446

Open
yermulnik opened this issue Nov 4, 2022 · 4 comments
Open

Hook to sort definition blocks in HCL files #446

yermulnik opened this issue Nov 4, 2022 · 4 comments
Labels
estimate/2days Need 2 work days to be done feature New feature or request

Comments

@yermulnik
Copy link
Collaborator

What problem are you facing?

A while ago before orcs groundlessly and treacherously come against Ukraine while I was poking around some TF issue I stumbled upon this thread about sorting Terraform files and created a tiny AWK script to achieve the goal of sorting definition blocks in .tf files.

And now I had a thought that someone may find it useful and may want to create a pre-commit-terraform hook to benefit from it (I don't actually heavily use either and thus I'm not sure I'd be able to allocate some reasonable amount of time for this).
So if there are any volunteers to pick this task up, I'd be happy to assist as much as time permits.

Thanks in advance for the contribution and effort 🙇🏻

How could pre-commit-terraform help solve your problem?

Sorting definition blocks in *.tf files may make your code more beautiful and clear. Or may not 😏

@yermulnik yermulnik added the feature New feature or request label Nov 4, 2022
@MaxymVlasov MaxymVlasov added the estimate/2days Need 2 work days to be done label Nov 4, 2022
@MaxymVlasov
Copy link
Collaborator

That pretty much looks like the current terraform_docs realization, jfyi. So will be a good idea to look into it to understand how to +- do the same as a standalone hook.

@oviliz
Copy link

oviliz commented Feb 22, 2023

@yermulnik when using your tf_vars_sort.awk I am getting the following:

❯ cat _variables.tf  | awk -f tf_vars_sort.awk | tee _variables_sorted.tf                                                                             ⇣15.9 KiB/s ⇡15 KiB/s 192.168.1.107  │ 208.127.175.132  │ 100%  │ 405 Mbps  
awk: syntax error at source line 11 source file tf_vars_sort.awk
 context is
                switch ($0) >>>  { <<< 
awk: illegal statement at source line 11 source file tf_vars_sort.awk
awk: illegal statement at source line 11 source file tf_vars_sort.awk
        3 missing }'s

@MaxymVlasov
Copy link
Collaborator

@oviliz be sure that you use gawk awk not mawk. That's root cause in most cases

@yermulnik
Copy link
Collaborator Author

@oviliz https://gist.github.com/yermulnik/7e0cf991962680d406692e1db1b551e6

Tested with GNU Awk 5.0.1, API: 2.0 (GNU MPFR 4.0.2, GNU MP 6.2.0)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
estimate/2days Need 2 work days to be done feature New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants