diff --git a/pre_commit_hooks/terraform/validate.sh b/pre_commit_hooks/terraform/validate.sh index c007911..1610aca 100755 --- a/pre_commit_hooks/terraform/validate.sh +++ b/pre_commit_hooks/terraform/validate.sh @@ -5,7 +5,15 @@ set -e # set default aws region for validating aws providers export AWS_REGION=${AWS_REGION:="none"} +TF_VALIDATE_ERROR=0 + while read -r dir; do - terraform init -backend=false "$dir" - terraform validate "$dir" -done < <(printf '%s\n' "${@}" | xargs -i dirname {} | sort -u) + echo "### Running 'terraform validate' in directory '$dir'" + + pushd "$dir" >/dev/null + terraform init -backend=false || TF_VALIDATE_ERROR=$? + terraform validate || TF_VALIDATE_ERROR=$? + popd >/dev/null +done < <(printf '%s\n' "${@}" | xargs -n1 dirname | sort -u | uniq) + +exit ${TF_VALIDATE_ERROR}