Skip to content

Add Terraform formatter step #959

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

Closed
wants to merge 7 commits into from

Conversation

carhartl
Copy link
Contributor

@carhartl carhartl commented Oct 8, 2021

This PR adds the ability for Spotless to test for/enforce consistent formatting for Terraform files (.tf, .tfvars).

The step shells out to the external terraform fmt command, the implementation being similar to the existing Black step.

I found that often times terraform files are part of an application repository, thus enhancing Spotless in order to be able to use a single build tool/interface for keeping formatting for these sources consistent.

Here's a repository I used for doing integration testing of the gradle plugin: https://github.com/carhartl/spotless-terraform-test

See https://www.terraform.io/docs/cli/commands/fmt.html for more information regarding Terraform formatting.

The step shells out to the external `terraform fmt` command, the
implementation being similar to the existing Black step.

This step is supposed to ensure consistent, conventional formatting for
.tf and .tfvars files.
@nedtwigg
Copy link
Member

nedtwigg commented Oct 8, 2021

Looks great! I'm gonna give this 24 hrs in case anybody else has comments, then I'll merge and release.

@nedtwigg
Copy link
Member

nedtwigg commented Oct 8, 2021

Actually, just one missing piece: the feature matrix in the root README. It's a bit tricky, the actual text of the table is rendered by an embedded freshmark script above it (edit the comment, run spotlessApply, and it will render the table), see this PR for an example.

@carhartl
Copy link
Contributor Author

carhartl commented Oct 9, 2021

#949 looks interesting, doesn’t that mean instead of this entire PR we could use the native cmd capability instead for formatting terraform files? Must have overlooked this..

@nedtwigg
Copy link
Member

nedtwigg commented Oct 9, 2021

You are correct, another way to accomplish this PR would be to port NativeCmd to plugin-gradle's FormatExtension, rather than have special-purpose terraform stuff. I have no preference either way.

@carhartl
Copy link
Contributor Author

I think having NativeCmd available in plugin-gradle will provide more value overall, and add less code, so I'm giving this a try, preparing a new PR..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants