|
1 | 1 | # Pre-commit hooks |
2 | 2 |
|
3 | | -This repo defines Git pre-commit hooks intended for use with [pre-commit](http://pre-commit.com/). The currently |
| 3 | +This repo defines Git pre-commit hooks intended for use with [pre-commit](https://pre-commit.com/). The currently |
4 | 4 | supported hooks are: |
5 | 5 |
|
6 | | -* **terraform-fmt**: Automatically run `terraform fmt` on all Terraform code (`*.tf` files). |
| 6 | +* **terraform-fmt**: Checks that all Terraform files (`*.tf`) are properly formatted (`terraform fmt --check -diff`). |
| 7 | +* **terraform-validate**: Runs `terraform init -backend=false` and then `terraform validate`. |
| 8 | + > Notes: directories requiring a private registry and lacking credentials are marked as **skipped** (do not fail the commit). Both hooks ignore `.terraform/` and `examples/`. |
7 | 9 |
|
8 | 10 | ## General Usage |
9 | 11 |
|
10 | 12 | In each of your repos, add a file called `.pre-commit-config.yaml` with the following contents: |
11 | 13 |
|
12 | 14 | ```yaml |
13 | 15 | repos: |
14 | | - - repo: https://github.com/craftech-io/pre-commit |
15 | | - rev: <VERSION> # Get the latest from: https://github.com/craftech-io/pre-commit/releases |
| 16 | + - repo: [email protected]:craftech-io/pre-commit.git # or https://github.com/craftech-io/pre-commit.git |
| 17 | + rev: <VERSION> |
16 | 18 | hooks: |
17 | 19 | - id: terraform-fmt |
| 20 | + - id: terraform-validate |
| 21 | + verbose: true |
18 | 22 | ``` |
19 | 23 |
|
20 | | -Next, have every developer: |
| 24 | +Next, have every developer: |
21 | 25 |
|
22 | | -1. Install [pre-commit](http://pre-commit.com/). E.g. `brew install pre-commit`. |
23 | | -1. Run `pre-commit install` in the repo. |
| 26 | +1. Install [pre-commit](https://pre-commit.com/#install). |
| 27 | + - macOS: `brew install pre-commit` |
| 28 | + - Linux: `pipx install pre-commit` (or `pip install --user pre-commit`) |
| 29 | +2. Run `pre-commit install` in the repo. |
24 | 30 |
|
25 | | -That’s it! Now every time you commit a code change (`.tf` file), the hooks in the `hooks:` config will execute. |
| 31 | +That's it! Now every time you commit a code change (`.tf` file), the hooks in the `hooks:` config will execute. |
| 32 | +If any hook fails, the commit is aborted; if all pass, the commit succeeds. |
26 | 33 |
|
27 | 34 | ## Running Against All Files At Once |
28 | 35 |
|
| 36 | +### Example: Formatting and validating all files |
29 | 37 |
|
30 | | -### Example: Formatting all files |
31 | | - |
32 | | -If you'd like to format all of your code at once (rather than one file at a time), you can run: |
| 38 | +If you'd like to run the hooks across the whole repo (useful the first time), you can run: |
33 | 39 |
|
34 | 40 | ```bash |
| 41 | +# Check formatting for all Terraform files |
35 | 42 | pre-commit run terraform-fmt --all-files |
| 43 | +
|
| 44 | +# Validate all Terraform directories |
| 45 | +pre-commit run terraform-validate --all-files |
| 46 | +
|
| 47 | +# Or run every configured hook across the repo |
| 48 | +pre-commit run --all-files |
36 | 49 | ``` |
37 | 50 |
|
38 | | -## License |
| 51 | +> Tip: for detailed output on demand, use `-v`, e.g. `pre-commit run -v terraform-validate --all-files`. |
39 | 52 |
|
40 | | -This code is released under the Apache 2.0 License. Please see [LICENSE](LICENSE) for more details. |
| 53 | +## License |
41 | 54 |
|
| 55 | +This code is released under the Apache 2.0 License. Please see [LICENSE](LICENSE) for more details. |
0 commit comments