LinuxCommandLibrary

tofu-fmt

Format infrastructure-as-code configuration files

TLDR

Format the configuration in the current directory

$ tofu fmt
copy

Format the configuration in the current directory and subdirectories
$ tofu fmt -recursive
copy

Display diffs of formatting changes
$ tofu fmt -diff
copy

Do not list files that were formatted to stdout
$ tofu fmt -list=false
copy

SYNOPSIS

tofu-fmt [options] [path ...]

PARAMETERS

-w
    Write result to source files instead of stdout.

-list=true|false
    List files whose formatting differs from tofu-fmt's.

-recursive
    Process files in subdirectories, as well.

-diff
    Display diffs of what tofu-fmt would do, instead of rewriting files.

[path ...]
    Path to the Terraform file or directory to format. If no path is specified, the current directory is used.

DESCRIPTION

tofu-fmt is a command-line tool used for automatically formatting Terraform configuration files. It ensures consistent style and readability across your Terraform projects by adhering to a standardized format. It's part of the broader Terraform ecosystem but can be used standalone to format .tf and .tfvars files.
It improves collaboration and reduces the amount of time spent manually formatting code. tofu-fmt relies on the HashiCorp Configuration Language (HCL) parser to understand the structure of Terraform files, enabling accurate and reliable formatting.

CAVEATS

tofu-fmt's formatting rules are opinionated and may not align with all coding styles. Be sure to review the changes it makes to ensure they meet your project's requirements.

EXIT CODES

tofu-fmt exits with a zero status if all files were formatted successfully. It exits with a non-zero status if any error occurred or if the `-list` option is used and files need formatting.

IMPACT ON VERSION CONTROL

Using tofu-fmt before committing changes to version control systems like Git ensures that code is consistently formatted. This reduces noise in diffs caused by formatting changes and makes it easier to review code.

HISTORY

tofu-fmt was introduced to provide a standardized way of formatting Terraform code, similar to tools like `go fmt` for Go. It has evolved alongside Terraform, adapting to new HCL features and incorporating user feedback. Its usage has become widespread in Terraform projects to maintain code quality and consistency.

SEE ALSO

Copied to clipboard