Skip to content

Commit 57c52a9

Browse files
committed
Add terraform_fmt usage instructions and how-to debug script with args
1 parent 71f7c34 commit 57c52a9

File tree

2 files changed

+55
-40
lines changed

2 files changed

+55
-40
lines changed

.github/CONTRIBUTING.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@ pre-commit try-repo -a ~/pre-commit-terraform # run all existing checks from rep
2828

2929
Running `pre-commit` with `try-repo` ignores all arguments specified in `.pre-commit-config.yaml`.
3030

31+
If you need to test hook with arguments, follow [pre-commit doc](https://pre-commit.com/#arguments-pattern-in-hooks) to test hooks.
32+
33+
For example, to test that the [`terraform_fmt`](../README.md#terraform_fmt) hook works fine with arguments:
34+
35+
```bash
36+
/tmp/pre-commit-terraform/terraform_fmt.sh --args=-diff --args=-write=false test-dir/main.tf test-dir/vars.tf
37+
```
38+
3139
## Run hook performance test
3240

3341
To check is your improvement not violate performance, we have dummy execution time tests.

README.md

Lines changed: 47 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Want to Contribute? Check [open issues](https://github.com/antonbabenko/pre-comm
1414
* [checkov](#checkov)
1515
* [terraform_docs](#terraform_docs)
1616
* [terraform_docs_replace](#terraform_docs_replace)
17+
* [terraform_fmt](#terraform_fmt)
1718
* [terraform_providers_lock](#terraform_providers_lock)
1819
* [terraform_tflint](#terraform_tflint)
1920
* [terraform_tfsec](#terraform_tfsec)
@@ -183,8 +184,8 @@ There are several [pre-commit](https://pre-commit.com/) hooks to keep Terraform
183184
| `terraform_docs_replace` | Runs `terraform-docs` and pipes the output directly to README.md |
184185
| `terraform_docs_without_aggregate_type_defaults` | Inserts input and output documentation into `README.md` without aggregate type defaults. Hook notes same as for [terraform_docs](#terraform_docs) |
185186
| `terraform_docs` | Inserts input and output documentation into `README.md`. Recommended. [Hook notes](#terraform_docs) |
186-
| `terraform_fmt` | Rewrites all Terraform configuration files to a canonical format. [Hook notes](#terraform_docs) |
187-
| `terraform_providers_lock` | Updates provider signatures in [dependency lock files](https://www.terraform.io/docs/cli/commands/providers/lock.html). [Hook notes](#terraform_providers_lock)
187+
| `terraform_fmt` | Rewrites all Terraform configuration files to a canonical format. [Hook notes](#terraform_fmt) |
188+
| `terraform_providers_lock` | Updates provider signatures in [dependency lock files](https://www.terraform.io/docs/cli/commands/providers/lock.html). [Hook notes](#terraform_providers_lock) |
188189
| `terraform_tflint` | Validates all Terraform configuration files with [TFLint](https://github.com/terraform-linters/tflint). [Available TFLint rules](https://github.com/terraform-linters/tflint/tree/master/docs/rules#rules). [Hook notes](#terraform_tflint). |
189190
| `terraform_tfsec` | [TFSec](https://github.com/liamg/tfsec) static analysis of terraform templates to spot potential security issues. [Hook notes](#terraform_tfsec) |
190191
| `terraform_validate` | Validates all Terraform configuration files. [Hook notes](#terraform_validate) |
@@ -243,6 +244,50 @@ Example:
243244
- --dest=TEST.md
244245
```
245246
247+
### terraform_fmt
248+
249+
1. `terraform_fmt` supports custom arguments so you can pass [supported flags](https://www.terraform.io/docs/cli/commands/fmt.html#usage). Eg:
250+
251+
```yaml
252+
- id: terraform_fmt
253+
args:
254+
- --args=-no-color
255+
- --args=-diff
256+
- --args=-write=false
257+
```
258+
259+
### terraform_providers_lock
260+
261+
1. The hook requires Terraform 0.14 or later.
262+
2. The hook invokes two operations that can be really slow:
263+
* `terraform init` (in case `.terraform` directory is not initialised)
264+
* `terraform providers lock`.
265+
266+
Both operations require downloading data from remote Terraform registries, and not all of that downloaded data or meta-data is currently being cached by Terraform.
267+
268+
3. `terraform_providers_lock` supports custom arguments:
269+
270+
```yaml
271+
- id: terraform_providers_lock
272+
args:
273+
- '--args=-platform=windows_amd64'
274+
- '--args=-platform=darwin_amd64'
275+
```
276+
277+
4. It may happen that Terraform working directory (`.terraform`) already exists but not in the best condition (eg, not initialized modules, wrong version of Terraform, etc). To solve this problem you can find and delete all `.terraform` directories in your repository:
278+
279+
```bash
280+
echo "
281+
function rm_terraform {
282+
find . -name ".terraform*" -print0 | xargs -0 rm -r
283+
}
284+
" >>~/.bashrc
285+
286+
# Reload shell and use `rm_terraform` command in repo root
287+
```
288+
289+
`terraform_providers_lock` hook will try to reinitialize them before running `terraform providers lock` command.
290+
246291
### terraform_tflint
247292
248293
1. `terraform_tflint` supports custom arguments so you can enable module inspection, deep check mode etc.
@@ -344,44 +389,6 @@ Example:
344389
345390
**Warning:** If you use Terraform workspaces, DO NOT use this workaround ([details](https://github.com/antonbabenko/pre-commit-terraform/issues/203#issuecomment-918791847)). Wait to [`force-init`](https://github.com/antonbabenko/pre-commit-terraform/issues/224) option implementation
346391
347-
### terraform_providers_lock
348-
349-
1. The hook requires Terraform 0.14 or later.
350-
351-
1. The hook invokes two operations that can be really slow:
352-
`terraform init` (in case `.terraform` directory is not initialised)
353-
and `terraform providers lock`. Both operations require downloading
354-
data from remote Terraform registries, and not all of that
355-
downloaded data or meta-data is currently being cached by Terraform.
356-
357-
1. `terraform_providers_lock` supports custom arguments.
358-
359-
Example:
360-
361-
```yaml
362-
hooks:
363-
- id: terraform_providers_lock
364-
args: ['--args=-platform=windows_amd64']
365-
```
366-
367-
In order to pass multiple args, try the following:
368-
369-
```yaml
370-
- id: terraform_providers_lock
371-
args:
372-
- '--args=-platform=windows_amd64'
373-
- '--args=-platform=darwin_amd64'
374-
```
375-
376-
1. It may happen that Terraform working directory (`.terraform`) already exists but is outdated
377-
(e.g. not initialized modules, wrong version of Terraform, etc).
378-
To solve this problem you can find and delete all `.terraform` directories in your repository using this command:
379-
380-
```shell
381-
find . -type d -name .terraform -prune -print -exec rm -rf {} \;
382-
```
383-
384-
`terraform_providers_lock` hook will try to reinitialize them before running `terraform providers lock` command.
385392
386393
## Authors
387394

0 commit comments

Comments
 (0)