diff --git a/.pre-commit-hooks.yaml b/.pre-commit-hooks.yaml index ee10f8228..789d7095a 100644 --- a/.pre-commit-hooks.yaml +++ b/.pre-commit-hooks.yaml @@ -41,3 +41,11 @@ language: script files: (\.tf|\.tfvars)$ exclude: \.terraform\/.*$ + +- id: terragrunt_fmt + name: Terragrunt fmt + description: Rewrites all Terragrunt configuration files to a canonical format. + entry: terragrunt_fmt.sh + language: script + files: (\.hcl)$ + exclude: \.terraform\/.*$ diff --git a/README.md b/README.md index c05352dec..539195b70 100644 --- a/README.md +++ b/README.md @@ -52,6 +52,7 @@ There are several [pre-commit](http://pre-commit.com/) hooks to keep Terraform c * `terraform_docs` - Inserts input and output documentation into `README.md`. Recommended. * `terraform_docs_without_aggregate_type_defaults` - Inserts input and output documentation into `README.md` without aggregate type defaults. * `terraform_docs_replace` - Runs `terraform-docs` and pipes the output directly to README.md +* `terragrunt_fmt` - Rewrites all Terragrunt configuration files to a canonical format. Check the [source file](https://github.com/antonbabenko/pre-commit-terraform/blob/master/.pre-commit-hooks.yaml) to know arguments used for each hook. diff --git a/terragrunt_fmt.sh b/terragrunt_fmt.sh new file mode 100755 index 000000000..ee23131e7 --- /dev/null +++ b/terragrunt_fmt.sh @@ -0,0 +1,23 @@ +#!/usr/bin/env bash + +set -e + +declare -a paths + +index=0 + +for file_with_path in "$@"; do + file_with_path="${file_with_path// /__REPLACED__SPACE__}" + + paths[index]=$(dirname "$file_with_path") + + let "index+=1" +done + +for path_uniq in $(echo "${paths[*]}" | tr ' ' '\n' | sort -u); do + path_uniq="${path_uniq//__REPLACED__SPACE__/ }" + + pushd "$path_uniq" > /dev/null + terragrunt hclfmt + popd > /dev/null +done