Skip to content

Commit

Permalink
Merge branch 'allow-setting-tf-path' of github.com:den-is/pre-commit-…
Browse files Browse the repository at this point in the history
…terraform into allow-setting-tf-path
  • Loading branch information
den-is committed May 21, 2024
2 parents 38f7dc2 + 3660e75 commit 35e7f1b
Showing 1 changed file with 18 additions and 20 deletions.
38 changes: 18 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ If you are using `pre-commit-terraform` already or want to support its developme
* [Table of content](#table-of-content)
* [How to install](#how-to-install)
* [1. Install dependencies](#1-install-dependencies)
* [1.1 Custom Terraform binaries and OpenTofu support](#11-custom-terraform-binaries-and-opentofu-support)
* [2. Install the pre-commit hook globally](#2-install-the-pre-commit-hook-globally)
* [3. Add configs and hooks](#3-add-configs-and-hooks)
* [4. Run](#4-run)
Expand Down Expand Up @@ -88,13 +87,13 @@ If you are using `pre-commit-terraform` already or want to support its developme

#### 1.1 Custom Terraform binaries and OpenTofu support

It is possible to set custom path to `terraform` binary.
It is possible to set custom path to `terraform` binary.
This makes it possible to use [OpenTofu](https://opentofu.org) binary `tofu` instead of `terraform`.

How binary discovery works and how you can redefine it:

1. Check if set per hook configuration `--hook-config=--tf-path=<path_to_binary_or_binary_name>`
2. Check if `PCT_TFPATH=<path_to_binary_or_binary_name>` environment variable exist
2. Check if `PCT_TFPATH=<path_to_binary_or_binary_name>` environment variable exist
3. Check if `TERRAGRUNT_TFPATH=<path_to_binary_or_binary_name>` environment variable set
4. Check if `terraform` binary can be found in the user's $PATH
5. Check if `tofu` binary can be found in the user's $PATH
Expand All @@ -113,7 +112,7 @@ All available tags [here](https://github.com/antonbabenko/pre-commit-terraform/p
**Build from scratch**:

> [!IMPORTANT]
> To build image you need to have [`docker buildx`](https://docs.docker.com/build/install-buildx/) enabled as default builder.
> To build image you need to have [`docker buildx`](https://docs.docker.com/build/install-buildx/) enabled as default builder.
> Otherwise - provide `TARGETOS` and `TARGETARCH` as additional `--build-arg`'s to `docker build`.
When hooks-related `--build-arg`s are not specified, only the latest version of `pre-commit` and `terraform` will be installed.
Expand Down Expand Up @@ -218,7 +217,7 @@ Otherwise, you can follow [this gist](https://gist.github.com/etiennejeanneaurev

Ensure your PATH environment variable looks for `bash.exe` in `C:\Program Files\Git\bin` (the one present in `C:\Windows\System32\bash.exe` does not work with `pre-commit.exe`)

For `checkov`, you may need to also set your `PYTHONPATH` environment variable with the path to your Python modules.
For `checkov`, you may need to also set your `PYTHONPATH` environment variable with the path to your Python modules.
E.g. `C:\Users\USERNAME\AppData\Local\Programs\Python\Python39\Lib\site-packages`

</details>
Expand Down Expand Up @@ -371,10 +370,10 @@ Less verbose log levels will be implemented in [#562](https://github.com/antonba

### Many hooks: Parallelism

> All, except deprecated hooks: `checkov`, `terraform_docs_replace` and hooks which can't be paralleled this way: `infracost_breakdown`, `terraform_wrapper_module_for_each`.
> All, except deprecated hooks: `checkov`, `terraform_docs_replace` and hooks which can't be paralleled this way: `infracost_breakdown`, `terraform_wrapper_module_for_each`.
> Also, there's a chance that parallelism have no effect on `terragrunt_fmt` and `terragrunt_validate` hooks
By default, parallelism is set to `number of logical CPUs - 1`.
By default, parallelism is set to `number of logical CPUs - 1`.
If you'd like to disable parallelism, set it to `1`

```yaml
Expand Down Expand Up @@ -430,7 +429,7 @@ args:
- --hook-config=--parallelism-ci-cpu-cores=N
```

If you don't see code above in your `pre-commit-config.yaml` or logs - you don't need it.
If you don't see code above in your `pre-commit-config.yaml` or logs - you don't need it.
`--parallelism-ci-cpu-cores` used only in edge cases and is ignored in other situations. Check out its usage in [hooks/_common.sh](hooks/_common.sh)

### checkov (deprecated) and terraform_checkov
Expand Down Expand Up @@ -578,7 +577,7 @@ Unlike most other hooks, this hook triggers once if there are any changed files
* create a documentation file
* extend existing documentation file by appending markers to the end of the file (see item 1 above)
* use different filename for the documentation (default is `README.md`)
* use the same insertion markers as `terraform-docs` by default. It will be default in `v2.0`.
* use the same insertion markers as `terraform-docs` by default. It will be default in `v2.0`.
To migrate to `terraform-docs` insertion markers, run in repo root:

```bash
Expand All @@ -603,7 +602,7 @@ Unlike most other hooks, this hook triggers once if there are any changed files
- --args=--config=.terraform-docs.yml
```

> **Warning**
> **Warning**
> Avoid use `recursive.enabled: true` in config file, that can cause unexpected behavior.
5. If you need some exotic settings, it can be done too. I.e. this one generates HCL files:
Expand Down Expand Up @@ -754,7 +753,7 @@ To replicate functionality in `terraform_docs` hook:
3. `terraform_providers_lock` support passing custom arguments to its `terraform init`:
> **Warning**
> **Warning**
> DEPRECATION NOTICE: This is available only in `no-mode` mode, which will be removed in v2.0. Please provide this keys to [`terraform_validate`](#terraform_validate) hook, which, to take effect, should be called before `terraform_providers_lock`
```yaml
Expand Down Expand Up @@ -935,10 +934,10 @@ To replicate functionality in `terraform_docs` hook:
- --hook-config=--retry-once-with-cleanup=true # Boolean. true or false
```
> **Important**
> **Important**
> The flag requires additional dependency to be installed: `jq`.
> **Note**
> **Note**
> Reinit can be very slow and require downloading data from remote Terraform registries, and not all of that downloaded data or meta-data is currently being cached by Terraform.
When `--retry-once-with-cleanup=true`, in each failed directory the cached modules and providers from the `.terraform` directory will be deleted, before retrying once more. To avoid unnecessary deletion of this directory, the cleanup and retry will only happen if Terraform produces any of the following error messages:
Expand All @@ -949,7 +948,7 @@ To replicate functionality in `terraform_docs` hook:
* "Module not installed"
* "Could not load plugin"
> **Warning**
> **Warning**
> When using `--retry-once-with-cleanup=true`, problematic `.terraform/modules/` and `.terraform/providers/` directories will be recursively deleted without prompting for consent. Other files and directories will not be affected, such as the `.terraform/environment` file.
**Option 2**
Expand All @@ -968,7 +967,7 @@ To replicate functionality in `terraform_docs` hook:
`terraform_validate` hook will try to reinitialize them before running the `terraform validate` command.

> **Caution**
> **Caution**
> If you use Terraform workspaces, DO NOT use this option ([details](https://github.com/antonbabenko/pre-commit-terraform/issues/203#issuecomment-918791847)). Consider the first option, or wait for [`force-init`](https://github.com/antonbabenko/pre-commit-terraform/issues/224) option implementation.

1. `terraform_validate` in a repo with Terraform module, written using Terraform 0.15+ and which uses provider `configuration_aliases` ([Provider Aliases Within Modules](https://www.terraform.io/language/modules/develop/providers#provider-aliases-within-modules)), errors out.
Expand Down Expand Up @@ -1020,7 +1019,7 @@ To replicate functionality in `terraform_docs` hook:
[...]
```

> **Tip**
> **Tip**
> The latter method will leave an "aliased-providers.tf.json" file in your repo. You will either want to automate a way to clean this up or add it to your `.gitignore` or both.
### terraform_wrapper_module_for_each
Expand All @@ -1044,8 +1043,8 @@ Sample configuration:
- --args=--verbose # Verbose output
```

**If you use hook inside Docker:**
The `terraform_wrapper_module_for_each` hook attempts to determine the module's short name to be inserted into the generated `README.md` files for the `source` URLs. Since the container uses a bind mount at a static location, it can cause this short name to be incorrect.
**If you use hook inside Docker:**
The `terraform_wrapper_module_for_each` hook attempts to determine the module's short name to be inserted into the generated `README.md` files for the `source` URLs. Since the container uses a bind mount at a static location, it can cause this short name to be incorrect.
If the generated name is incorrect, set them by providing the `module-repo-shortname` option to the hook:

```yaml
Expand Down Expand Up @@ -1095,7 +1094,7 @@ If the generated name is incorrect, set them by providing the `module-repo-short
- --args=--version 2.5.0 # Will be pined to specified version
```

Check [`tfupdate` usage instructions](https://github.com/minamijoyo/tfupdate#usage) for other available options and usage examples.
Check [`tfupdate` usage instructions](https://github.com/minamijoyo/tfupdate#usage) for other available options and usage examples.
No need to pass `--recursive .` as it is added automatically.

### terragrunt_providers_lock
Expand Down Expand Up @@ -1260,4 +1259,3 @@ MIT licensed. See [LICENSE](LICENSE) for full details.
* Russia has [illegally annexed Crimea in 2014](https://en.wikipedia.org/wiki/Annexation_of_Crimea_by_the_Russian_Federation) and [brought the war in Donbas](https://en.wikipedia.org/wiki/War_in_Donbas) followed by [full-scale invasion of Ukraine in 2022](https://en.wikipedia.org/wiki/2022_Russian_invasion_of_Ukraine).
* Russia has brought sorrow and devastations to millions of Ukrainians, killed hundreds of innocent people, damaged thousands of buildings, and forced several million people to flee.
* [Putin khuylo!](https://en.wikipedia.org/wiki/Putin_khuylo!)

0 comments on commit 35e7f1b

Please sign in to comment.