From e9250bd69bb312d55364213ff5ff037a09be55d9 Mon Sep 17 00:00:00 2001 From: Yevgeniy Brikman Date: Thu, 10 Dec 2020 16:11:04 +0000 Subject: [PATCH] Update terraform-fmt to match terraform-validate (#46) This PR updates the `terraform-fmt` hook as follows: 1. Run with `-diff -check` so the differences are printed, rather than made on disk. 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 | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/hooks/terraform-fmt.sh b/hooks/terraform-fmt.sh index 37187b07..ba8c3741 100755 --- a/hooks/terraform-fmt.sh +++ b/hooks/terraform-fmt.sh @@ -7,8 +7,11 @@ set -e # workaround to allow GitHub Desktop to work, add this (hopefully harmless) setting here. 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 file in "$@"; do - pushd "$(dirname "$file")" >/dev/null - terraform fmt -write=true "$(basename "$file")" - popd >/dev/null + terraform fmt -diff -check "$file" || FMT_ERROR=$? done + +exit ${FMT_ERROR}