numtide / treefmt

one CLI to format your repo [maintainers=@zimbatm,@brianmcgee]
https://treefmt.com
MIT License
575 stars 35 forks source link

terraform fmt doesn't take files #97

Closed zimbatm closed 1 year ago

zimbatm commented 3 years ago

terraform fmt doesn't obey the treefmt spec


$ terraform fmt --help
Usage: terraform 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.

  -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.
zimbatm commented 3 years ago

I found the issue, it only takes one path as an argument.

zimbatm commented 3 years ago

If you want to help, please :+1: this upstream PR: https://github.com/hashicorp/terraform/pull/28191

zimbatm commented 2 years ago

Another workaround is to use hclfmt:

[formatter.hcl]
command = "hclfmt"
options = [
    "-w" # write in place
]
includes = ["*.hcl", "*.tf"]

Note that hclfmt isn't widely distributed. I didn't find it in Homebrew or nixpkgs.

Here is the nix package: https://github.com/NixOS/nixpkgs/pull/182472

nikolay commented 1 year ago

@zimbatm Terraform v1.13 got released today and terraform fmt takes a list of files now.

zimbatm commented 1 year ago

finally!