MCPcopy Index your code
hub / github.com/AlexNabokikh/tfsort

github.com/AlexNabokikh/tfsort @v0.7.1

Chat with this repo
repository ↗ · DeepWiki ↗ · release v0.7.1 ↗ · + Follow
30 symbols 96 edges 8 files 18 documented · 60%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

tfsort

badge-gh-ci badge-gh-release go-report-card maintainability test-coverage

Logo

tfsort is a command-line utility designed for meticulous engineers who prefer to keep their Terraform variable, output, locals and terraform blocks sorted alphabetically. It also corrects spacing issues between these blocks and removes leading or trailing newlines in the processed files. tfsort can read from files or stdin, write to files or stdout, and process directories recursively.

Contents

Demo

asciicast

Key Features

  • Alphabetical Sorting: Sorts variable, output, locals and terraform blocks within your HCL files.
  • Flexible Input/Output:
  • Read from a specific file or standard input (stdin).
  • Overwrite the input file, write to a new file, or print to standard output (stdout).
  • Recursive Processing: Sort files in an entire directory and its subdirectories.
  • Intelligently skips common version control (.git) and Terraform utility directories (.terraform, .terragrunt-cache).
  • Dry Run Mode: Preview changes without modifying any files.
  • Code Formatting:
  • Corrects spacing between sorted blocks.
  • Removes unnecessary leading or trailing newlines from the file.

Supported File Types

tfsort processes files with the following extensions:

  • .tf
  • .hcl
  • .tofu

Installation

Homebrew

To install tfsort using Homebrew:

  1. Add the tap:

bash brew tap alexnabokikh/tfsort

  1. Install tfsort:

bash brew install tfsort

Nix

nix-env -iA nixpkgs.tfsort

Chocolatey (Windows)

To install tfsort using Chocolatey:

choco install tfsort

Using go install

If you have Go installed and configured, you can install tfsort directly using go install:

go install github.com/AlexNabokikh/tfsort@latest

This command will download the source code, compile it, and install the tfsort binary into your $GOPATH/bin or $GOBIN directory (make sure this directory is in your system's PATH).

Binary Release

Download the latest binary release for your operating system from the Releases Page.

From Source

Alternatively, build tfsort from source:

  1. Clone the repository:

bash git clone https://github.com/AlexNabokikh/tfsort.git cd tfsort

  1. Build the binary:

bash go build .

This will create a tfsort executable in the current directory.

Usage

Command Synopsis

tfsort [file_or_directory|-] [flags]

Arguments

  • file_or_directory (optional):
  • Path to a single Terraform/HCL file (e.g., variables.tf).
  • Path to a directory to process recursively (requires the -r flag).
  • - (optional):
  • Instructs tfsort to read input from stdin.
  • If no file/directory argument is provided and stdin is detected as a pipe (e.g., cat file.tf | tfsort), tfsort will read from stdin.
  • If no arguments are provided and stdin is not a pipe, tfsort will show the help message.

Flags

  • -o, --out <path>:
  • Specifies the path to the output file.
  • If the input is a file and -o is not provided, the input file is overwritten.
  • If the input is stdin and -o is not provided, the output is sent to stdout.
  • This flag cannot be used with -r, --recursive.
  • -d, --dry-run:
  • Previews the changes by printing the sorted content to stdout.
  • No files will be modified when this flag is used.
  • -r, --recursive:
  • Recursively sorts supported files in the specified directory.
  • Files are modified in-place unless -d, --dry-run is also specified.
  • If -r is used, a directory path must be provided as an argument.
  • Cannot be used with stdin input (-) or the -o, --out flag.
  • -h, --help:
  • Displays a comprehensive help message, listing available commands, arguments, and flags with their descriptions.
  • -v, --version:
  • Displays the installed version of the tfsort application, typically including the version number, commit hash, and build date if available.

Examples

  1. Sort a single file in-place: (Sorts variable, output, locals and terraform blocks in my_variables.tf and overwrites the file)

bash tfsort my_variables.tf

  1. Sort a single file and write to a new file:

bash tfsort my_variables.tf -o sorted_variables.tf

  1. Preview changes for a single file (dry run): (Prints the sorted content to the console without modifying my_variables.tf)

bash tfsort my_variables.tf -d

  1. Sort content from stdin and print to stdout:

bash cat my_config.tf | tfsort -

Or, if tfsort is part of a pipeline and no file argument is given:

bash cat my_config.tf | tfsort

  1. Sort content from stdin and write to a file:

bash cat my_config.tf | tfsort - -o sorted_from_stdin.tf

  1. Recursively sort files in a directory (in-place): (Sorts all .tf, .hcl, .tofu files in my_terraform_project/ and its subdirectories, modifying them in-place. Skips .git, .terraform, .terragrunt-cache.)

bash tfsort -r ./my_terraform_project/

  1. Recursively sort files in a directory (dry run): (Prints what would be changed for each file to the console without modifying them.)

bash tfsort -r ./my_terraform_project/ -d

Contributing

Contributions are welcome! Please read the CONTRIBUTING.md file for guidelines on how to contribute to this project, including code contributions, bug reports, and feature suggestions.

Code of Conduct

This project adheres to the Contributor Covenant Code of Conduct. By participating, you are expected to uphold this code. Please read the CODE_OF_CONDUCT.md file for details.

Author

This project was created by Alexander Nabokikh.

License

This software is available under the following licenses:

Core symbols most depended-on inside this repo

Shape

Function 27
Struct 2
Method 1

Languages

Go100%

Modules by API surface

internal/hclsort/tfsort_test.go11 symbols
internal/hclsort/hcl_processor.go5 symbols
cmd/root.go5 symbols
internal/hclsort/file_handler.go4 symbols
internal/hclsort/types.go2 symbols
internal/hclsort/ingestor.go2 symbols
main.go1 symbols

For agents

$ claude mcp add tfsort \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact

Ask about this repo answers extend the page