From e2760caf8d70875ca85d25b11a972e1f207217f8 Mon Sep 17 00:00:00 2001 From: Anton Babenko Date: Tue, 11 Dec 2018 20:21:49 +0100 Subject: [PATCH] Added followup after #25 --- .pre-commit-config.yaml | 2 +- .pre-commit-hooks.yaml | 9 ++++++ README.md | 71 +++++++++++++++++++++++++++++++++-------- hooks.yaml | 9 ++++++ 4 files changed, 77 insertions(+), 14 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 70a21ad98..1c444a95a 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: v1.2.3 + rev: v2.0.0 hooks: - id: check-yaml - id: end-of-file-fixer diff --git a/.pre-commit-hooks.yaml b/.pre-commit-hooks.yaml index 2fda38ba1..65436d109 100644 --- a/.pre-commit-hooks.yaml +++ b/.pre-commit-hooks.yaml @@ -10,6 +10,15 @@ name: Terraform docs description: Inserts input and output documentation into README.md (using terraform-docs). entry: terraform_docs.sh + args: [--args=--with-aggregate-type-defaults] + language: script + files: (\.tf)$ + exclude: \.terraform\/.*$ + +- id: terraform_docs_without_aggregate_type_defaults + name: Terraform docs (without aggregate type defaults) + description: Inserts input and output documentation into README.md (using terraform-docs). + entry: terraform_docs.sh language: script files: (\.tf)$ exclude: \.terraform\/.*$ diff --git a/README.md b/README.md index ce242a35c..e3e7699ef 100644 --- a/README.md +++ b/README.md @@ -1,29 +1,74 @@ -# pre-commit-terraform hook +# 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/2018.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) -Several [pre-commit](http://pre-commit.com/) hooks to keep Terraform configurations (both `*.tf` and `*.tfvars`) in a good shape: -* `terraform_fmt` - Rewrites all Terraform configuration files to a canonical format. -* `terraform_validate_no_variables` - Validates all Terraform configuration files without checking whether all required variables were set. -* `terraform_validate_with_variables` - Validates all Terraform configuration files and checks whether all required variables were specified. -* `terraform_docs` - Inserts input and output documentation into `README.md`. +## How to install -## Notes about hooks +### Step 1 -1. `terraform_validate_no_variables` and `terraform_validate_with_variables` will not work if variables are being set dynamically (eg, when using [Terragrunt](https://github.com/gruntwork-io/terragrunt)). Use `terragrunt validate` command instead. +On MacOSX install the pre-commit package + +```bash +brew install pre-commit +``` -1. `terraform_docs` will insert/update documentation generated by [terraform-docs](https://github.com/segmentio/terraform-docs) between markers - `` and `` if they are present in `README.md`. Make sure that `terraform-docs` is installed. +For other operating systems check the [official documentation](http://pre-commit.com/#install) -## Example +### Step 2 -`.pre-commit-config.yaml`: +Step into the repository you want to have the pre-commit hooks installed and run: -```yaml +```bash +cat < .pre-commit-config.yaml - repo: git://github.com/antonbabenko/pre-commit-terraform - rev: v1.7.3 + rev: v1.7.4 hooks: - id: terraform_fmt - id: terraform_docs +EOF +``` + +### Step 3 + +Install the pre-commit hook + +```bash +pre-commit install +``` + +### Step 4 + +After pre-commit hook has been installed you can run it manually on all files in the repository + +```bash +pre-commit run -a ``` +## Available Hooks + +There are several [pre-commit](http://pre-commit.com/) hooks to keep Terraform configurations (both `*.tf` and `*.tfvars`) in a good shape: +* `terraform_fmt` - Rewrites all Terraform configuration files to a canonical format. +* `terraform_validate_no_variables` - Validates all Terraform configuration files without checking whether all required variables were set. +* `terraform_validate_with_variables` - Validates all Terraform configuration files and checks whether all required variables were specified. +* `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. + +Check the [source file](https://github.com/antonbabenko/pre-commit-terraform/blob/master/.pre-commit-hooks.yaml) to know arguments used for each hook. + +## Notes about hooks + +1. `terraform_validate_no_variables` and `terraform_validate_with_variables` will not work if variables are being set dynamically (eg, when using [Terragrunt](https://github.com/gruntwork-io/terragrunt)). Use `terragrunt validate` command instead. + +1. `terraform_docs` and `terraform_docs_without_aggregate_type_defaults` will insert/update documentation generated by [terraform-docs](https://github.com/segmentio/terraform-docs) between markers - `` and `` if they are present in `README.md`. Make sure that `terraform-docs` is installed. + +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. + Enjoy the clean and documented code! + +## Authors + +This repository is managed by [Anton Babenko](https://github.com/antonbabenko) with help from [these awesome contributors](https://github.com/antonbabenko/pre-commit-terraform/graphs/contributors). + +## License + +MIT licensed. See LICENSE for full details. diff --git a/hooks.yaml b/hooks.yaml index 2fda38ba1..65436d109 100644 --- a/hooks.yaml +++ b/hooks.yaml @@ -10,6 +10,15 @@ name: Terraform docs description: Inserts input and output documentation into README.md (using terraform-docs). entry: terraform_docs.sh + args: [--args=--with-aggregate-type-defaults] + language: script + files: (\.tf)$ + exclude: \.terraform\/.*$ + +- id: terraform_docs_without_aggregate_type_defaults + name: Terraform docs (without aggregate type defaults) + description: Inserts input and output documentation into README.md (using terraform-docs). + entry: terraform_docs.sh language: script files: (\.tf)$ exclude: \.terraform\/.*$