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

Allow terraform fmt to be run on a single file. #30072

Closed
ilanpillemer opened this issue Dec 2, 2021 · 4 comments
Closed

Allow terraform fmt to be run on a single file. #30072

ilanpillemer opened this issue Dec 2, 2021 · 4 comments
Assignees
Labels
confirmed a Terraform Core team member has reproduced this issue documentation enhancement explained a Terraform Core team member has described the root cause of this issue in code

Comments

@ilanpillemer
Copy link

Current Terraform Version

v1.0.11

Use-cases

I would like to be able to run terraform fmt on a single file and not an entire directory. With all the same options.
For example terraform format -f main.tf, or terraform format -no-color -check -f main.tf

Attempted Solutions

Not possible.

Proposal

Usage: terraform [global options] fmt [options] [DIR]

	Rewrites all Terraform configuration files to a canonical format. Both
	configuration files (.tf) and variables files (.tfvars) are updated.
	JSON files (.tf.json or .tfvars.json) are not modified.

	If DIR is not specified then the current working directory will be used.
	If DIR is "-" then content will be read from STDIN. The given content must
	be in the Terraform language native syntax; JSON is not supported.

Options:

  -list=false    Don't list files whose formatting differs
                 (always disabled if using STDIN)

  -write=false   Don't write to source files
                 (always disabled if using STDIN or -check)

  -diff          Display diffs of formatting changes

  -check         Check if the input is formatted. Exit status will be 0 if all
                 input is properly formatted and non-zero otherwise.

  -f             If specified, file is used instead of every file in directory

  -no-color      If specified, output won't contain any color.

  -recursive     Also process files in subdirectories. By default, only the
                 given directory (or current directory) is processed.

References

No

@ilanpillemer ilanpillemer added enhancement new new issue not yet triaged labels Dec 2, 2021
@crw crw self-assigned this Dec 3, 2021
@crw
Copy link
Collaborator

crw commented Dec 3, 2021

Thanks for this well-written report. The good news is that you can already specify a single filename in the place of DIR or - that will be formatted without impacting the rest of the folder. For example:

terraform fmt main.tf

will format only main.tf. I will keep this issue open as a documentation issue. If I have missed a detail or nuance in this request, please let me know. Thanks again!

@crw crw added confirmed a Terraform Core team member has reproduced this issue documentation explained a Terraform Core team member has described the root cause of this issue in code good first issue and removed good first issue labels Dec 3, 2021
@rubiagatra
Copy link

I would like to help, but where do I start @crw ?

Should I update to Usage: terraform [global options] fmt [options] [DIR|FILE] and all the documentation ?

@crw
Copy link
Collaborator

crw commented Dec 8, 2021

Thanks @rubiagatra, the answer is that I am not sure at the moment! I will update if and when I can provide clear guidance.

@crw crw removed the new new issue not yet triaged label Dec 9, 2021
BrianSidebotham pushed a commit to BrianSidebotham/terraform that referenced this issue Jun 21, 2022
… option can target a single file instead
BrianSidebotham added a commit to BrianSidebotham/terraform that referenced this issue Jun 21, 2022
… option can target a single file instead
laurapacilio added a commit that referenced this issue Jul 11, 2022
Fixes #30072 - Update docs to show single file usage
laurapacilio added a commit that referenced this issue Jul 11, 2022
…72/gradually-trusting-wahoo

Backport of Fixes #30072 - Update docs to show single file usage into v1.2
@github-actions
Copy link

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.
If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 11, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
confirmed a Terraform Core team member has reproduced this issue documentation enhancement explained a Terraform Core team member has described the root cause of this issue in code
Projects
None yet
Development

No branches or pull requests

3 participants