mirror of
https://github.com/tofuutils/pre-commit-opentofu.git
synced 2025-10-15 17:38:54 +02:00
Added support for terraform-docs 0.8.0 with proper support for Terraform 0.12 syntax (bye-bye awk) (#85)
This commit is contained in:
parent
9fd71e3934
commit
b99e3500e1
4 changed files with 42 additions and 38 deletions
|
|
@ -1,6 +1,6 @@
|
||||||
repos:
|
repos:
|
||||||
- repo: git://github.com/pre-commit/pre-commit-hooks
|
- repo: git://github.com/pre-commit/pre-commit-hooks
|
||||||
rev: v2.2.3
|
rev: v2.4.0
|
||||||
hooks:
|
hooks:
|
||||||
- id: check-yaml
|
- id: check-yaml
|
||||||
- id: end-of-file-fixer
|
- id: end-of-file-fixer
|
||||||
|
|
@ -8,3 +8,8 @@ repos:
|
||||||
- id: check-case-conflict
|
- id: check-case-conflict
|
||||||
- id: check-merge-conflict
|
- id: check-merge-conflict
|
||||||
- id: check-executables-have-shebangs
|
- id: check-executables-have-shebangs
|
||||||
|
#- repo: git://github.com/jumanjihouse/pre-commit-hooks
|
||||||
|
# rev: 1.11.2
|
||||||
|
# hooks:
|
||||||
|
# - id: shellcheck
|
||||||
|
# - id: shfmt
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,6 @@
|
||||||
## [Unreleased]
|
## [Unreleased]
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<a name="v1.22.0"></a>
|
<a name="v1.22.0"></a>
|
||||||
## [v1.22.0] - 2020-01-13
|
## [v1.22.0] - 2020-01-13
|
||||||
|
|
||||||
|
|
|
||||||
21
README.md
21
README.md
|
|
@ -1,20 +1,19 @@
|
||||||
# Collection of git hooks for Terraform to be used with [pre-commit framework](http://pre-commit.com/)
|
# Collection of git hooks for Terraform to be used with [pre-commit framework](http://pre-commit.com/)
|
||||||
|
|
||||||
[](https://github.com/antonbabenko/pre-commit-terraform/releases)  [](https://www.codetriage.com/antonbabenko/pre-commit-terraform)
|
[](https://github.com/antonbabenko/pre-commit-terraform/releases)  [](https://www.codetriage.com/antonbabenko/pre-commit-terraform)
|
||||||
|
|
||||||
## How to install
|
## How to install
|
||||||
|
|
||||||
### 1. Install dependencies
|
### 1. Install dependencies
|
||||||
|
|
||||||
* [`pre-commit`](http://pre-commit.com/#install)
|
* [`pre-commit`](https://pre-commit.com/#install)
|
||||||
* [`terraform-docs`](https://github.com/segmentio/terraform-docs) (required for `terraform_docs` hooks)
|
* [`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.
|
||||||
* GNU `awk` (required for `terraform_docs` hooks in Terraform 0.12)
|
* [`TFLint`](https://github.com/terraform-linters/tflint) required for `terraform_tflint` hook.
|
||||||
* [`TFLint`](https://github.com/wata727/tflint) (required for `terraform_tflint` hook)
|
|
||||||
|
|
||||||
##### MacOS
|
##### MacOS
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
brew install pre-commit awk terraform-docs tflint
|
brew install pre-commit gawk terraform-docs tflint
|
||||||
```
|
```
|
||||||
|
|
||||||
##### Ubuntu
|
##### Ubuntu
|
||||||
|
|
@ -23,7 +22,7 @@ brew install pre-commit awk terraform-docs tflint
|
||||||
sudo apt install python-pip3 gawk &&\
|
sudo apt install python-pip3 gawk &&\
|
||||||
pip3 install pre-commit
|
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/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
|
### 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
|
git init
|
||||||
cat <<EOF > .pre-commit-config.yaml
|
cat <<EOF > .pre-commit-config.yaml
|
||||||
- repo: git://github.com/antonbabenko/pre-commit-terraform
|
- repo: git://github.com/antonbabenko/pre-commit-terraform
|
||||||
rev: v1.19.0
|
rev: <VERSION> # Get the latest from: https://github.com/antonbabenko/pre-commit-terraform/releases
|
||||||
hooks:
|
hooks:
|
||||||
- id: terraform_fmt
|
- id: terraform_fmt
|
||||||
- id: terraform_docs
|
- id: terraform_docs
|
||||||
|
|
@ -59,7 +58,7 @@ pre-commit run -a
|
||||||
|
|
||||||
## Available Hooks
|
## 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 |
|
| 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` | 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_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_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. |
|
| `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.
|
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. 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
|
## Notes about terraform_tflint hooks
|
||||||
|
|
||||||
1. `terraform_tflint` supports custom arguments so you can enable module inspection, deep check mode etc.
|
1. `terraform_tflint` supports custom arguments so you can enable module inspection, deep check mode etc.
|
||||||
|
|
|
||||||
|
|
@ -25,19 +25,38 @@ main() {
|
||||||
esac
|
esac
|
||||||
done
|
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
|
if [[ ! $(command -v terraform-docs) ]]; then
|
||||||
which awk 2>&1 >/dev/null || ( echo "awk is required for terraform-docs hack to work with Terraform 0.12"; exit 1)
|
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")
|
tmp_file_awk=$(mktemp "${TMPDIR:-/tmp}/terraform-docs-XXXXXXXXXX")
|
||||||
terraform_docs_awk "$tmp_file_awk"
|
terraform_docs_awk "$tmp_file_awk"
|
||||||
terraform_docs "$tmp_file_awk" "$args" "$files"
|
terraform_docs "$tmp_file_awk" "$args" "$files"
|
||||||
rm -f "$tmp_file_awk"
|
rm -f "$tmp_file_awk"
|
||||||
else
|
|
||||||
terraform_docs "0" "$args" "$files"
|
|
||||||
fi
|
|
||||||
|
|
||||||
|
else # Using terraform 0.11 and no awk script is needed for that
|
||||||
|
|
||||||
|
terraform_docs "0" "$args" "$files"
|
||||||
|
|
||||||
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
terraform_docs() {
|
terraform_docs() {
|
||||||
|
|
@ -76,7 +95,7 @@ terraform_docs() {
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [[ "$terraform_docs_awk_file" == "0" ]]; then
|
if [[ "$terraform_docs_awk_file" == "0" ]]; then
|
||||||
terraform-docs md $args ./ > "$tmp_file"
|
terraform-docs md "$args" ./ > "$tmp_file"
|
||||||
else
|
else
|
||||||
# Can't append extension for mktemp, so renaming instead
|
# Can't append extension for mktemp, so renaming instead
|
||||||
tmp_file_docs=$(mktemp "${TMPDIR:-/tmp}/terraform-docs-XXXXXXXXXX")
|
tmp_file_docs=$(mktemp "${TMPDIR:-/tmp}/terraform-docs-XXXXXXXXXX")
|
||||||
|
|
@ -84,7 +103,7 @@ terraform_docs() {
|
||||||
tmp_file_docs_tf="$tmp_file_docs.tf"
|
tmp_file_docs_tf="$tmp_file_docs.tf"
|
||||||
|
|
||||||
awk -f "$terraform_docs_awk_file" ./*.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"
|
rm -f "$tmp_file_docs_tf"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
|
@ -103,24 +122,19 @@ terraform_docs() {
|
||||||
terraform_docs_awk() {
|
terraform_docs_awk() {
|
||||||
readonly output_file=$1
|
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`
|
# 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.
|
# 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/
|
||||||
# https://github.com/segmentio/terraform-docs/issues/62
|
# 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
|
# Script was originally found here: https://github.com/cloudposse/build-harness/blob/master/bin/terraform-docs.awk
|
||||||
|
|
||||||
{
|
{
|
||||||
if ( $0 ~ /\{/ ) {
|
if ( $0 ~ /\{/ ) {
|
||||||
braceCnt++
|
braceCnt++
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( $0 ~ /\}/ ) {
|
if ( $0 ~ /\}/ ) {
|
||||||
braceCnt--
|
braceCnt--
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
# ----------------------------------------------------------------------------------------------
|
# ----------------------------------------------------------------------------------------------
|
||||||
# variable|output "..." {
|
# variable|output "..." {
|
||||||
# ----------------------------------------------------------------------------------------------
|
# ----------------------------------------------------------------------------------------------
|
||||||
|
|
@ -142,8 +156,6 @@ terraform_docs_awk() {
|
||||||
# Print variable|output line
|
# Print variable|output line
|
||||||
print $0
|
print $0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
# ----------------------------------------------------------------------------------------------
|
# ----------------------------------------------------------------------------------------------
|
||||||
# default = ...
|
# default = ...
|
||||||
# ----------------------------------------------------------------------------------------------
|
# ----------------------------------------------------------------------------------------------
|
||||||
|
|
@ -177,8 +189,6 @@ terraform_docs_awk() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
# ----------------------------------------------------------------------------------------------
|
# ----------------------------------------------------------------------------------------------
|
||||||
# type = ...
|
# type = ...
|
||||||
# ----------------------------------------------------------------------------------------------
|
# ----------------------------------------------------------------------------------------------
|
||||||
|
|
@ -211,7 +221,6 @@ terraform_docs_awk() {
|
||||||
} else {
|
} else {
|
||||||
type = $3
|
type = $3
|
||||||
}
|
}
|
||||||
|
|
||||||
# legacy quoted types: "string", "list", and "map"
|
# legacy quoted types: "string", "list", and "map"
|
||||||
if (type ~ /^[[:space:]]*"(.*?)"[[:space:]]*$/) {
|
if (type ~ /^[[:space:]]*"(.*?)"[[:space:]]*$/) {
|
||||||
print " type = " type
|
print " type = " type
|
||||||
|
|
@ -229,8 +238,6 @@ terraform_docs_awk() {
|
||||||
blockTypeCnt -= gsub(/\}/, "")
|
blockTypeCnt -= gsub(/\}/, "")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
# ----------------------------------------------------------------------------------------------
|
# ----------------------------------------------------------------------------------------------
|
||||||
# description = ...
|
# description = ...
|
||||||
# ----------------------------------------------------------------------------------------------
|
# ----------------------------------------------------------------------------------------------
|
||||||
|
|
@ -240,8 +247,6 @@ terraform_docs_awk() {
|
||||||
print $0
|
print $0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
# ----------------------------------------------------------------------------------------------
|
# ----------------------------------------------------------------------------------------------
|
||||||
# value = ...
|
# value = ...
|
||||||
# ----------------------------------------------------------------------------------------------
|
# ----------------------------------------------------------------------------------------------
|
||||||
|
|
@ -251,8 +256,6 @@ terraform_docs_awk() {
|
||||||
# print $0
|
# print $0
|
||||||
# }
|
# }
|
||||||
#}
|
#}
|
||||||
|
|
||||||
|
|
||||||
# ----------------------------------------------------------------------------------------------
|
# ----------------------------------------------------------------------------------------------
|
||||||
# Newlines, comments, everything else
|
# Newlines, comments, everything else
|
||||||
# ----------------------------------------------------------------------------------------------
|
# ----------------------------------------------------------------------------------------------
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue