From 5fc3e9d76eb826b2cb27e9a9d99a644dd7175b80 Mon Sep 17 00:00:00 2001 From: Yevgeniy Brikman Date: Wed, 9 Dec 2020 08:56:00 +0000 Subject: [PATCH 1/2] Update terraform-fmt to match terraform-validate This PR updates the `terraform-fmt` hook as follows: 1. Extract all the directories with Terraform code first and run `terraform fmt` once per directory. 1. Print out the directory where we're running `terraform fmt` to make debugging errors much easier. 1. Instead of exiting on the first error, save the exit codes, and print out all `fmt` errors before exiting. These changes are very similar to https://github.com/gruntwork-io/pre-commit/pull/45. --- hooks/terraform-fmt.sh | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/hooks/terraform-fmt.sh b/hooks/terraform-fmt.sh index 37187b07..ffe8e69d 100755 --- a/hooks/terraform-fmt.sh +++ b/hooks/terraform-fmt.sh @@ -7,8 +7,14 @@ set -e # workaround to allow GitHub Desktop to work, add this (hopefully harmless) setting here. export PATH=$PATH:/usr/local/bin -for file in "$@"; do - pushd "$(dirname "$file")" >/dev/null - terraform fmt -write=true "$(basename "$file")" +# Store and return last failure from fmt so this can validate every directory passed before exiting +FMT_ERROR=0 + +for dir in $(echo "$@" | xargs -n1 dirname | sort -u | uniq); do + echo "--> Running 'terraform fmt' in directory '$dir'" + pushd "$dir" >/dev/null + terraform fmt -write=true || FMT_ERROR=$? popd >/dev/null done + +exit ${FMT_ERROR} From cbe601c0e748de5de314e902e4b61f1e9cff0fbb Mon Sep 17 00:00:00 2001 From: Yevgeniy Brikman Date: Thu, 10 Dec 2020 11:07:18 +0000 Subject: [PATCH 2/2] Update hooks/terraform-fmt.sh Co-authored-by: David Alger --- hooks/terraform-fmt.sh | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/hooks/terraform-fmt.sh b/hooks/terraform-fmt.sh index ffe8e69d..ba8c3741 100755 --- a/hooks/terraform-fmt.sh +++ b/hooks/terraform-fmt.sh @@ -10,11 +10,8 @@ export PATH=$PATH:/usr/local/bin # Store and return last failure from fmt so this can validate every directory passed before exiting FMT_ERROR=0 -for dir in $(echo "$@" | xargs -n1 dirname | sort -u | uniq); do - echo "--> Running 'terraform fmt' in directory '$dir'" - pushd "$dir" >/dev/null - terraform fmt -write=true || FMT_ERROR=$? - popd >/dev/null +for file in "$@"; do + terraform fmt -diff -check "$file" || FMT_ERROR=$? done exit ${FMT_ERROR}