From 6547993836cc1b68e399ba4843bc6bc994a73296 Mon Sep 17 00:00:00 2001 From: tyron Date: Mon, 9 Aug 2021 14:43:40 -0400 Subject: [PATCH 1/3] write changes to disk on terraform_fmt by default --- hooks/terraform-fmt.sh | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/hooks/terraform-fmt.sh b/hooks/terraform-fmt.sh index ba8c3741..1857a3ee 100755 --- a/hooks/terraform-fmt.sh +++ b/hooks/terraform-fmt.sh @@ -7,11 +7,43 @@ set -e # workaround to allow GitHub Desktop to work, add this (hopefully harmless) setting here. export PATH=$PATH:/usr/local/bin +write_changes=true +FILES=() + +parse_arguments() { + while (($# > 0)); do + # Grab param and value splitting on " " or "=" with parameter expansion + local PARAMETER="${1%[ =]*}" + local VALUE="${1#*[ =]}" + if [[ "$PARAMETER" == "$VALUE" ]]; then VALUE="$2"; fi + shift + case "$PARAMETER" in + --no-write) + write_changes=false + ;; + -*) + echo "Error: Unknown option: $PARAMETER" >&2 + exit 1 + ;; + *) + FILES+=("$PARAMETER") + ;; + esac + done +} + +parse_arguments "$@" + # Store and return last failure from fmt so this can validate every directory passed before exiting FMT_ERROR=0 -for file in "$@"; do - terraform fmt -diff -check "$file" || FMT_ERROR=$? +for file in "$FILES"; do + file=$(dirname "$file") + if [ "$write_changes" = true ]; then + terraform fmt "$file" || FMT_ERROR=$? + else + terraform fmt -diff -check "$file" || FMT_ERROR=$? + fi done exit ${FMT_ERROR} From 7b019429f09485a19573ab45d059b9ebd604039c Mon Sep 17 00:00:00 2001 From: tyron Date: Mon, 9 Aug 2021 15:51:36 -0400 Subject: [PATCH 2/3] adding info on new args --- README.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/README.md b/README.md index 3569e62e..ce935090 100644 --- a/README.md +++ b/README.md @@ -127,6 +127,21 @@ Now when the pre-commit hook runs, it will call `helm lint` with both `linter_va helm lint -f values.yaml -f linter_values.yaml . ``` +## terraform-fmt arguments + +By default, `terraform-fmt` will write changes back to the original file. +You can skip this by setting the `--no-write` flag. + +```yaml +repos: + - repo: https://github.com/gruntwork-io/pre-commit + rev: + hooks: + - id: terraform-fmt + args: ["--no-write"] +``` + + ## Shellcheck Arguments To enable optional shellcheck features you can use the `--enable` flag. From 837cfa83f27ec2dd4a4c6860b2a6ca9392642fe0 Mon Sep 17 00:00:00 2001 From: tyron Date: Mon, 9 Aug 2021 16:26:16 -0400 Subject: [PATCH 3/3] rename flag --- README.md | 4 ++-- hooks/terraform-fmt.sh | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index ce935090..dae1f939 100644 --- a/README.md +++ b/README.md @@ -130,7 +130,7 @@ helm lint -f values.yaml -f linter_values.yaml . ## terraform-fmt arguments By default, `terraform-fmt` will write changes back to the original file. -You can skip this by setting the `--no-write` flag. +You can skip this by setting the `--no-autofix` flag. ```yaml repos: @@ -138,7 +138,7 @@ repos: rev: hooks: - id: terraform-fmt - args: ["--no-write"] + args: ["--no-autofix"] ``` diff --git a/hooks/terraform-fmt.sh b/hooks/terraform-fmt.sh index 1857a3ee..b778c12e 100755 --- a/hooks/terraform-fmt.sh +++ b/hooks/terraform-fmt.sh @@ -18,7 +18,7 @@ parse_arguments() { if [[ "$PARAMETER" == "$VALUE" ]]; then VALUE="$2"; fi shift case "$PARAMETER" in - --no-write) + --no-autofix) write_changes=false ;; -*)