From dfddd7634d3286aef62ab5dc857347c98d79790d Mon Sep 17 00:00:00 2001 From: odgrim Date: Wed, 25 Sep 2024 21:36:20 -0700 Subject: [PATCH 1/3] add hclvalidate and terragrunt equiv hooks for terraform hooks --- hooks/terragrunt-fmt.sh | 21 +++++++++++++++++++++ hooks/terragrunt-hclvalidate.sh | 14 ++++++++++++++ hooks/terragrunt-validate.sh | 24 ++++++++++++++++++++++++ 3 files changed, 59 insertions(+) create mode 100755 hooks/terragrunt-fmt.sh create mode 100755 hooks/terragrunt-hclvalidate.sh create mode 100755 hooks/terragrunt-validate.sh diff --git a/hooks/terragrunt-fmt.sh b/hooks/terragrunt-fmt.sh new file mode 100755 index 00000000..83492565 --- /dev/null +++ b/hooks/terragrunt-fmt.sh @@ -0,0 +1,21 @@ +#!/usr/bin/env bash + +set -e + +# OSX GUI apps do not pick up environment variables the same way as Terminal apps and there are no easy solutions, +# especially as Apple changes the GUI app behavior every release (see https://stackoverflow.com/q/135688/483528). As a +# workaround to allow GitHub Desktop to work, add this (hopefully harmless) setting here. +original_path=$PATH +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 + terragrunt --terragrunt-no-auto-init fmt -diff -check "$file" || FMT_ERROR=$? +done + +# reset path to the original value +export PATH=$original_path + +exit ${FMT_ERROR} diff --git a/hooks/terragrunt-hclvalidate.sh b/hooks/terragrunt-hclvalidate.sh new file mode 100755 index 00000000..57eda17d --- /dev/null +++ b/hooks/terragrunt-hclvalidate.sh @@ -0,0 +1,14 @@ +#!/usr/bin/env bash + +set -e + +# OSX GUI apps do not pick up environment variables the same way as Terminal apps and there are no easy solutions, +# especially as Apple changes the GUI app behavior every release (see https://stackoverflow.com/q/135688/483528). As a +# 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 + terragrunt hclvalidate "$(basename "$file")" + popd >/dev/null +done diff --git a/hooks/terragrunt-validate.sh b/hooks/terragrunt-validate.sh new file mode 100755 index 00000000..436edead --- /dev/null +++ b/hooks/terragrunt-validate.sh @@ -0,0 +1,24 @@ +#!/usr/bin/env bash + +set -e + +# OSX GUI apps do not pick up environment variables the same way as Terminal apps and there are no easy solutions, +# especially as Apple changes the GUI app behavior every release (see https://stackoverflow.com/q/135688/483528). As a +# workaround to allow GitHub Desktop to work, add this (hopefully harmless) setting here. +export PATH=$PATH:/usr/local/bin + +# Disable output not usually helpful when running in automation (such as guidance to run plan after init) +export TF_IN_AUTOMATION=1 + +# Store and return last failure from validate so this can validate every directory passed before exiting +VALIDATE_ERROR=0 + +for dir in $(echo "$@" | xargs -n1 dirname | sort -u | uniq); do + echo "--> Running 'terraform validate' in directory '$dir'" + pushd "$dir" >/dev/null + terragrunt init -backend=false || VALIDATE_ERROR=$? + terragrunt validate || VALIDATE_ERROR=$? + popd >/dev/null +done + +exit ${VALIDATE_ERROR} From a937e535cc0f1f3abc283918afba16c0d9fd4344 Mon Sep 17 00:00:00 2001 From: odgrim Date: Wed, 25 Sep 2024 21:42:10 -0700 Subject: [PATCH 2/3] configure new commands --- .pre-commit-hooks.yaml | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/.pre-commit-hooks.yaml b/.pre-commit-hooks.yaml index 76e78365..fabed9e4 100644 --- a/.pre-commit-hooks.yaml +++ b/.pre-commit-hooks.yaml @@ -9,6 +9,15 @@ exclude: \.+.terraform\/.*$ require_serial: true +- id: terragrunt-fmt + name: Terragrunt fmt + description: Rewrites all Terraform configuration files to a canonical format using terragrunt. + entry: hooks/terragrunt-fmt.sh + language: script + files: \.tf$ + exclude: \.+.terraform\/.*$ + require_serial: true + - id: terraform-validate name: Terraform validate description: Validates all Terraform configuration files @@ -18,6 +27,15 @@ exclude: \.+.terraform\/.*$ require_serial: true +- id: terragrunt-validate + name: Terragrunt validate + description: Validates all Terraform configuration files using terragrunt. + entry: hooks/terragrunt-validate.sh + language: script + files: \.tf$ + exclude: \.+.terraform\/.*$ + require_serial: true + - id: packer-validate name: Packer validate description: Validates all Packer configuration files @@ -46,6 +64,17 @@ .+\.terragrunt-cache\/.*$| )$ +- id: terragrunt-hclvalidate + name: Terragrunt hclvalidate + description: Validates all Terragrunt configuration files to a canonical format + entry: hooks/terragrunt-hclvalidate.sh + language: script + files: \.hcl$ + exclude: > + (?x)^( + .+\.terraform\/.*$| + .+\.terragrunt-cache\/.*$| + )$ - id: shellcheck name: Shellcheck Bash Linter description: Performs linting on bash scripts From 848a56044ca269238572f91d8ae0455702fc879e Mon Sep 17 00:00:00 2001 From: odgrim Date: Wed, 25 Sep 2024 21:50:59 -0700 Subject: [PATCH 3/3] add whitespace --- .pre-commit-hooks.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/.pre-commit-hooks.yaml b/.pre-commit-hooks.yaml index fabed9e4..7084df49 100644 --- a/.pre-commit-hooks.yaml +++ b/.pre-commit-hooks.yaml @@ -75,6 +75,7 @@ .+\.terraform\/.*$| .+\.terragrunt-cache\/.*$| )$ + - id: shellcheck name: Shellcheck Bash Linter description: Performs linting on bash scripts