diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index f80cfc805..ba19cfc9f 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,6 +1,6 @@ repos: - repo: git://github.com/pre-commit/pre-commit-hooks - rev: v2.2.3 + rev: v2.4.0 hooks: - id: check-yaml - id: end-of-file-fixer @@ -8,3 +8,8 @@ repos: - id: check-case-conflict - id: check-merge-conflict - id: check-executables-have-shebangs +#- repo: git://github.com/jumanjihouse/pre-commit-hooks +# rev: 1.11.2 +# hooks: +# - id: shellcheck +# - id: shfmt diff --git a/CHANGELOG.md b/CHANGELOG.md index 4e7a09234..f6bd51076 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,6 @@ ## [Unreleased] - ## [v1.22.0] - 2020-01-13 diff --git a/README.md b/README.md index 355ff9711..39f855cbd 100644 --- a/README.md +++ b/README.md @@ -1,20 +1,19 @@ # Collection of git hooks for Terraform to be used with [pre-commit framework](http://pre-commit.com/) -[![Github tag](https://img.shields.io/github/tag/antonbabenko/pre-commit-terraform.svg)](https://github.com/antonbabenko/pre-commit-terraform/releases) ![](https://img.shields.io/maintenance/yes/2019.svg) [![Help Contribute to Open Source](https://www.codetriage.com/antonbabenko/pre-commit-terraform/badges/users.svg)](https://www.codetriage.com/antonbabenko/pre-commit-terraform) +[![Github tag](https://img.shields.io/github/tag/antonbabenko/pre-commit-terraform.svg)](https://github.com/antonbabenko/pre-commit-terraform/releases) ![](https://img.shields.io/maintenance/yes/2020.svg) [![Help Contribute to Open Source](https://www.codetriage.com/antonbabenko/pre-commit-terraform/badges/users.svg)](https://www.codetriage.com/antonbabenko/pre-commit-terraform) ## How to install ### 1. Install dependencies -* [`pre-commit`](http://pre-commit.com/#install) -* [`terraform-docs`](https://github.com/segmentio/terraform-docs) (required for `terraform_docs` hooks) -* GNU `awk` (required for `terraform_docs` hooks in Terraform 0.12) -* [`TFLint`](https://github.com/wata727/tflint) (required for `terraform_tflint` hook) +* [`pre-commit`](https://pre-commit.com/#install) +* [`terraform-docs`](https://github.com/segmentio/terraform-docs) required for `terraform_docs` hooks. `GNU awk` is required if using `terraform-docs` older than 0.8.0 with Terraform 0.12. +* [`TFLint`](https://github.com/terraform-linters/tflint) required for `terraform_tflint` hook. ##### MacOS ```bash -brew install pre-commit awk terraform-docs tflint +brew install pre-commit gawk terraform-docs tflint ``` ##### Ubuntu @@ -23,7 +22,7 @@ brew install pre-commit awk terraform-docs tflint sudo apt install python-pip3 gawk &&\ pip3 install pre-commit curl -L "$(curl -s https://api.github.com/repos/segmentio/terraform-docs/releases/latest | grep -o -E "https://.+?-linux-amd64")" > terraform-docs && chmod +x terraform-docs && sudo mv terraform-docs /usr/bin/ -curl -L "$(curl -s https://api.github.com/repos/wata727/tflint/releases/latest | grep -o -E "https://.+?_linux_amd64.zip")" > tflint.zip && unzip tflint.zip && rm tflint.zip && sudo mv tflint /usr/bin/ +curl -L "$(curl -s https://api.github.com/repos/terraform-linters/tflint/releases/latest | grep -o -E "https://.+?_linux_amd64.zip")" > tflint.zip && unzip tflint.zip && rm tflint.zip && sudo mv tflint /usr/bin/ ``` ### 2. Install the pre-commit hook globally @@ -42,7 +41,7 @@ Step into the repository you want to have the pre-commit hooks installed and run git init cat < .pre-commit-config.yaml - repo: git://github.com/antonbabenko/pre-commit-terraform - rev: v1.19.0 + rev: # Get the latest from: https://github.com/antonbabenko/pre-commit-terraform/releases hooks: - id: terraform_fmt - id: terraform_docs @@ -59,7 +58,7 @@ pre-commit run -a ## Available Hooks -There are several [pre-commit](http://pre-commit.com/) hooks to keep Terraform configurations (both `*.tf` and `*.tfvars`) and Terragrunt configurations (`*.hcl`) in a good shape: +There are several [pre-commit](https://pre-commit.com/) hooks to keep Terraform configurations (both `*.tf` and `*.tfvars`) and Terragrunt configurations (`*.hcl`) in a good shape: | Hook name | Description | | ------------------------------------------------ | -------------------------------------------------------------------------------------------------------------------------- | @@ -68,7 +67,7 @@ There are several [pre-commit](http://pre-commit.com/) hooks to keep Terraform c | `terraform_docs` | Inserts input and output documentation into `README.md`. Recommended. | | `terraform_docs_without_aggregate_type_defaults` | Inserts input and output documentation into `README.md` without aggregate type defaults. | | `terraform_docs_replace` | Runs `terraform-docs` and pipes the output directly to README.md | -| `terraform_tflint` | Validates all Terraform configuration files with [TFLint](https://github.com/wata727/tflint). | +| `terraform_tflint` | Validates all Terraform configuration files with [TFLint](https://github.com/terraform-linters/tflint). | | `terragrunt_fmt` | Rewrites all [Terragrunt](https://github.com/gruntwork-io/terragrunt) configuration files (`*.hcl`) to a canonical format. | Check the [source file](https://github.com/antonbabenko/pre-commit-terraform/blob/master/.pre-commit-hooks.yaml) to know arguments used for each hook. @@ -94,8 +93,6 @@ if they are present in `README.md`. 1. It is possible to pass additional arguments to shell scripts when using `terraform_docs` and `terraform_docs_without_aggregate_type_defaults`. Send pull-request with the new hook if there is something missing. -1. `terraform-docs` works with Terraform 0.12 but support is hackish (it requires `awk` to be installed) and may contain bugs. You can follow the native support of Terraform 0.12 in `terraform-docs` in [issue #62](https://github.com/segmentio/terraform-docs/issues/62). - ## Notes about terraform_tflint hooks 1. `terraform_tflint` supports custom arguments so you can enable module inspection, deep check mode etc. diff --git a/terraform_docs.sh b/terraform_docs.sh index 490f157c8..60a09a8c9 100755 --- a/terraform_docs.sh +++ b/terraform_docs.sh @@ -25,19 +25,38 @@ main() { esac done - local hack_terraform_docs=$(terraform version | head -1 | grep -c 0.12) + local hack_terraform_docs + hack_terraform_docs=$(terraform version | head -1 | grep -c 0.12) - if [[ "$hack_terraform_docs" == "1" ]]; then - which awk 2>&1 >/dev/null || ( echo "awk is required for terraform-docs hack to work with Terraform 0.12"; exit 1) + if [[ ! $(command -v terraform-docs) ]]; then + echo "ERROR: terraform-docs is required by terraform_docs pre-commit hook but is not installed or in the system's PATH." + exit 1 + fi + + local is_old_terraform_docs + is_old_terraform_docs=$(terraform-docs version | grep -o "v0.[1-7]" | tail -1) + + if [[ -z "$is_old_terraform_docs" ]]; then # Using terraform-docs 0.8+ (preferred) + + terraform_docs "0" "$args" "$files" + + elif [[ "$hack_terraform_docs" == "1" ]]; then # Using awk script because terraform-docs is older than 0.8 and terraform 0.12 is used + + if [[ ! $(command -v awk) ]]; then + echo "ERROR: awk is required for terraform-docs hack to work with Terraform 0.12." + exit 1 + fi tmp_file_awk=$(mktemp "${TMPDIR:-/tmp}/terraform-docs-XXXXXXXXXX") terraform_docs_awk "$tmp_file_awk" terraform_docs "$tmp_file_awk" "$args" "$files" rm -f "$tmp_file_awk" - else + + else # Using terraform 0.11 and no awk script is needed for that + terraform_docs "0" "$args" "$files" - fi + fi } terraform_docs() { @@ -76,7 +95,7 @@ terraform_docs() { fi if [[ "$terraform_docs_awk_file" == "0" ]]; then - terraform-docs md $args ./ > "$tmp_file" + terraform-docs md "$args" ./ > "$tmp_file" else # Can't append extension for mktemp, so renaming instead tmp_file_docs=$(mktemp "${TMPDIR:-/tmp}/terraform-docs-XXXXXXXXXX") @@ -84,7 +103,7 @@ terraform_docs() { tmp_file_docs_tf="$tmp_file_docs.tf" awk -f "$terraform_docs_awk_file" ./*.tf > "$tmp_file_docs_tf" - terraform-docs md $args "$tmp_file_docs_tf" > "$tmp_file" + terraform-docs md "$args" "$tmp_file_docs_tf" > "$tmp_file" rm -f "$tmp_file_docs_tf" fi @@ -103,24 +122,19 @@ terraform_docs() { terraform_docs_awk() { readonly output_file=$1 - cat <<"EOF" > $output_file + cat <<"EOF" > "$output_file" # This script converts Terraform 0.12 variables/outputs to something suitable for `terraform-docs` # As of terraform-docs v0.6.0, HCL2 is not supported. This script is a *dirty hack* to get around it. # https://github.com/segmentio/terraform-docs/ # https://github.com/segmentio/terraform-docs/issues/62 - # Script was originally found here: https://github.com/cloudposse/build-harness/blob/master/bin/terraform-docs.awk - { if ( $0 ~ /\{/ ) { braceCnt++ } - if ( $0 ~ /\}/ ) { braceCnt-- } - - # ---------------------------------------------------------------------------------------------- # variable|output "..." { # ---------------------------------------------------------------------------------------------- @@ -142,8 +156,6 @@ terraform_docs_awk() { # Print variable|output line print $0 } - - # ---------------------------------------------------------------------------------------------- # default = ... # ---------------------------------------------------------------------------------------------- @@ -177,8 +189,6 @@ terraform_docs_awk() { } } } - - # ---------------------------------------------------------------------------------------------- # type = ... # ---------------------------------------------------------------------------------------------- @@ -211,7 +221,6 @@ terraform_docs_awk() { } else { type = $3 } - # legacy quoted types: "string", "list", and "map" if (type ~ /^[[:space:]]*"(.*?)"[[:space:]]*$/) { print " type = " type @@ -229,8 +238,6 @@ terraform_docs_awk() { blockTypeCnt -= gsub(/\}/, "") } } - - # ---------------------------------------------------------------------------------------------- # description = ... # ---------------------------------------------------------------------------------------------- @@ -240,8 +247,6 @@ terraform_docs_awk() { print $0 } } - - # ---------------------------------------------------------------------------------------------- # value = ... # ---------------------------------------------------------------------------------------------- @@ -251,8 +256,6 @@ terraform_docs_awk() { # print $0 # } #} - - # ---------------------------------------------------------------------------------------------- # Newlines, comments, everything else # ----------------------------------------------------------------------------------------------