From 3a3c2a472bf01f8b7aa2f3b4fc010657ba324d2b Mon Sep 17 00:00:00 2001 From: "Brett T. Hannigan" Date: Wed, 18 May 2022 12:43:09 -0600 Subject: [PATCH 1/3] * Add hook for packer fmt --- hooks/packer-fmt.sh | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100755 hooks/packer-fmt.sh diff --git a/hooks/packer-fmt.sh b/hooks/packer-fmt.sh new file mode 100755 index 00000000..467a4d65 --- /dev/null +++ b/hooks/packer-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 + packer fmt -diff -check "$file" || FMT_ERROR=$? +done + +# reset path to the original value +export PATH=$original_path + +exit ${FMT_ERROR} From 09b3c1a25cee3985344ce77e0834bd2278be893f Mon Sep 17 00:00:00 2001 From: "Brett T. Hannigan" Date: Wed, 18 May 2022 12:45:12 -0600 Subject: [PATCH 2/3] Update README --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index ee07d005..c23ca855 100644 --- a/README.md +++ b/README.md @@ -7,6 +7,7 @@ supported hooks are: * **terraform-fmt**: Automatically run `terraform fmt` on all Terraform code (`*.tf` files). * **terraform-validate**: Automatically run `terraform validate` on all Terraform code (`*.tf` files). +* **packer-fmt**: Automatically run `packer fmt` on all Packer code (`*.pkr.*` files). * **packer-validate**: Automatically run `packer validate` on all Packer code (`*.pkr.*` files). * **terragrunt-hclfmt**: Automatically run `terragrunt hclfmt` on all Terragrunt configurations. * **tflint**: Automatically run [`tflint`](https://github.com/terraform-linters/tflint) on all Terraform code (`*.tf` files). From 53b22eb176d6401c28927957b7fa64a5de953c4a Mon Sep 17 00:00:00 2001 From: "Brett T. Hannigan" Date: Wed, 18 May 2022 12:48:36 -0600 Subject: [PATCH 3/3] Updating yaml configuration --- .pre-commit-hooks.yaml | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/.pre-commit-hooks.yaml b/.pre-commit-hooks.yaml index 89b985b2..804df87f 100644 --- a/.pre-commit-hooks.yaml +++ b/.pre-commit-hooks.yaml @@ -18,6 +18,14 @@ exclude: \.+.terraform\/.*$ require_serial: true +- id: packer-fmt + name: Packer fmt + description: Rewrites all Packer configuration files to a canonical format + entry: hooks/packer-fmt.sh + language: script + files: (\.pkr\.(hcl|json)|\.pkrvars\.hcl)$ + require_serial: true + - id: packer-validate name: Packer validate description: Validates all Packer configuration files