diff --git a/.pre-commit-hooks.yaml b/.pre-commit-hooks.yaml index 789d7095a..c7f403941 100644 --- a/.pre-commit-hooks.yaml +++ b/.pre-commit-hooks.yaml @@ -42,6 +42,14 @@ files: (\.tf|\.tfvars)$ exclude: \.terraform\/.*$ +- id: terraform_tflint + name: Terraform validate with tflint + description: Validates all Terraform configuration files with TFLint. + entry: terraform_tflint.sh + language: script + files: (\.tf|\.tfvars)$ + exclude: \.terraform\/.*$ + - id: terragrunt_fmt name: Terragrunt fmt description: Rewrites all Terragrunt configuration files to a canonical format. diff --git a/terraform_tflint.sh b/terraform_tflint.sh new file mode 100755 index 000000000..4e6c23e76 --- /dev/null +++ b/terraform_tflint.sh @@ -0,0 +1,23 @@ +#!/usr/bin/env bash +set -e + +declare -a paths +declare -a tfvars_files + +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 + tflint --deep + popd > /dev/null +done