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

Feature: Sort Variables #111

Closed
sblack4 opened this issue May 18, 2020 · 7 comments
Closed

Feature: Sort Variables #111

sblack4 opened this issue May 18, 2020 · 7 comments
Labels
estimate/1week Need 1 work week to be done feature New feature or request stale Denotes an issue or PR has remained open with no activity and has become stale.

Comments

@sblack4
Copy link

sblack4 commented May 18, 2020

My friends and I have been trying to standardize our terraform modules. One of the things we've started to do is sorting everything, particularly variables, alphabetically. I think this would make a great pre-commit hook.

There's a feature request out there for it hashicorp/terraform#12959 and I've got a script to do this I stole from that thread. I've never written my own pre-commit hook but I'd love to take on something simple like this to get a better idea of how the pre-commit hooks work

@MaxymVlasov MaxymVlasov added the feature New feature or request label Aug 19, 2021
@MaxymVlasov MaxymVlasov added kind/feature/new_hook estimate/1week Need 1 work week to be done and removed feature New feature or request labels Sep 9, 2021
@antonbabenko antonbabenko added feature New feature or request and removed kind/feature/new_hook labels Sep 14, 2021
@MaxymVlasov
Copy link
Collaborator

Hi @sblack4
You can start from CONTRIBUTING.md guide and check the pre-commit.com website.

Feel free to ask any questions - it also helps me write a better contribution doc (#216)

@yermulnik
Copy link
Collaborator

If someone has a will to implement this feature request as a pre-commit hook, here's what I came up with using awk only: hashicorp/terraform#12959 (comment)

Quoting here for convenience:

I came up achieving the goal of sorting variables.tf with a simple awk script (which for most of the cases eliminates the need of external (non-base) deps like python, hcltool, et alia):
https://gist.github.com/yermulnik/7e0cf991962680d406692e1db1b551e6
Since this was more of an exercise out of curiosity, it was tested with GNU Awk 5.0.1, API: 2.0 (GNU MPFR 4.0.2, GNU MP 6.2.0) only, which I've got installed by default as /bin/awk on my Ubuntu 20.04.3 LTS.
Works for files with variable definitions only.
Usage: cat variables.tf | awk -f tf_vars_sort.awk | tee sorted_variables.tf
Hope this helps someone 😉

@antonbabenko
Copy link
Owner

@yermulnik Nice solution! We have just removed awk magic from terraform_docs hook which was necessary a long time ago. I remember how painful it was to maintain it and fix weird cases in the past.

I am not sure we really need to bring such a solution here because it will require maintenance (new arguments, new blocks can be added in variable blocks).

@yermulnik
Copy link
Collaborator

yermulnik commented Oct 23, 2021

JFYI: I've just extended the script to handle also some other general types of TF resources (not only variable's!) — please let me know if you find anything which would better be improved. Thanks.

I remember how painful it was to maintain it and fix weird cases in the past.

100% agree. The only reason I created this awk script was an academic interest to create a solution which requires no external dependencies apart from those which are in base of most Linux distros (basically only GNU awk).

I am not sure we really need to bring such a solution here because it will require maintenance (new arguments, new blocks can be added in variable blocks).

Yep, I agree for 100%. And this is why I haven't gone with a hook but with a helper script as I personally never needed this.
If someone feels this can be worth of a pre-commit hook, I'd be glad to volunteer to assist in development. Otherwise I also will be glad to try and implement such functionality using any other language officially supported by pre-commit-terraform.

Hope feature requester would be interested to get it done if this is worth of implementation for any occasional use case.

@yermulnik
Copy link
Collaborator

because it will require maintenance (new arguments, new blocks can be added in variable blocks).

Hopefully this should work w/o issues for any general-purpose TF resource blocks despite adding new arguments or nested blocks to them.

@github-actions
Copy link

This issue has been automatically marked as stale because it has been open 30 days
with no activity. Remove stale label or comment or this issue will be closed in 10 days

@github-actions github-actions bot added the stale Denotes an issue or PR has remained open with no activity and has become stale. label Nov 24, 2021
@github-actions
Copy link

github-actions bot commented Dec 5, 2021

This issue was automatically closed because of stale in 10 days

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
estimate/1week Need 1 work week to be done feature New feature or request stale Denotes an issue or PR has remained open with no activity and has become stale.
Projects
None yet
Development

No branches or pull requests

4 participants