Skip to content

Commit 9830420

Browse files
authored
feat: Add support of __GIT_WORKING_DIR__ placeholder for all hooks (#945)
Move `__GIT_WORKING_DIR__` permutation feature over right into `common::parse_cmdline` function so that it is available to all hooks that utilize `common::parse_cmdline` function. Resolves #944
1 parent 18cead2 commit 9830420

File tree

6 files changed

+51
-91
lines changed

6 files changed

+51
-91
lines changed

README.md

Lines changed: 44 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ If you want to support the development of `pre-commit-terraform` and [many other
5252
* [Hooks usage notes and examples](#hooks-usage-notes-and-examples)
5353
* [Known limitations](#known-limitations)
5454
* [All hooks: Usage of environment variables in `--args`](#all-hooks-usage-of-environment-variables-in---args)
55+
* [All hooks: Usage of `__GIT_WORKING_DIR__` placeholder in `--args`](#all-hooks-usage-of-__git_working_dir__-placeholder-in---args)
5556
* [All hooks: Set env vars inside hook at runtime](#all-hooks-set-env-vars-inside-hook-at-runtime)
5657
* [All hooks: Disable color output](#all-hooks-disable-color-output)
5758
* [All hooks: Log levels](#all-hooks-log-levels)
@@ -366,6 +367,24 @@ Config example:
366367
367368
If for config above set up `export CONFIG_NAME=.tflint; export CONFIG_EXT=hcl` before `pre-commit run`, args will be expanded to `--config=.tflint.hcl --call-module-type="all"`.
368369

370+
### All hooks: Usage of `__GIT_WORKING_DIR__` placeholder in `--args`
371+
372+
373+
> All, except deprecated hooks: `checkov`, `terraform_docs_replace`
374+
375+
You can use `__GIT_WORKING_DIR__` placeholder in `--args`. It will be replaced
376+
by the Git working directory (repo root) at run time.
377+
378+
For instance, if you have multiple directories and want to run
379+
`terraform_tflint` in all of them while sharing a single config file — use the
380+
`__GIT_WORKING_DIR__` placeholder in the file path. For example:
381+
382+
```yaml
383+
- id: terraform_tflint
384+
args:
385+
- --args=--config=__GIT_WORKING_DIR__/.tflint.hcl
386+
```
387+
369388
### All hooks: Set env vars inside hook at runtime
370389

371390
> All, except deprecated hooks: `checkov`, `terraform_docs_replace`
@@ -478,34 +497,26 @@ If you don't see code above in your `pre-commit-config.yaml` or logs - you don't
478497

479498
Note that `terraform_checkov` runs recursively during `-d .` usage. That means, for example, if you change `.tf` file in repo root, all existing `.tf` files in the repo will be checked.
480499

481-
1. You can specify custom arguments. E.g.:
482-
483-
```yaml
484-
- id: terraform_checkov
485-
args:
486-
- --args=--quiet
487-
- --args=--skip-check CKV2_AWS_8
488-
```
489-
490-
Check all available arguments [here](https://www.checkov.io/2.Basics/CLI%20Command%20Reference.html).
500+
You can specify custom arguments. E.g.:
491501

492-
For deprecated hook you need to specify each argument separately:
502+
```yaml
503+
- id: terraform_checkov
504+
args:
505+
- --args=--quiet
506+
- --args=--skip-check CKV2_AWS_8
507+
```
493508

494-
```yaml
495-
- id: checkov
496-
args: [
497-
"-d", ".",
498-
"--skip-check", "CKV2_AWS_8",
499-
]
500-
```
509+
Check all available arguments [here](https://www.checkov.io/2.Basics/CLI%20Command%20Reference.html).
501510

502-
2. When you have multiple directories and want to run `terraform_checkov` in all of them and share a single config file - use the `__GIT_WORKING_DIR__` placeholder. It will be replaced by `terraform_checkov` hooks with the Git working directory (repo root) at run time. For example:
511+
For deprecated hook you need to specify each argument separately:
503512

504-
```yaml
505-
- id: terraform_checkov
506-
args:
507-
- --args=--config-file __GIT_WORKING_DIR__/.checkov.yml
508-
```
513+
```yaml
514+
- id: checkov
515+
args: [
516+
"-d", ".",
517+
"--skip-check", "CKV2_AWS_8",
518+
]
519+
```
509520

510521
### infracost_breakdown
511522

@@ -702,15 +713,15 @@ To replicate functionality in `terraform_docs` hook:
702713

703714
### terraform_fmt
704715

705-
1. `terraform_fmt` supports custom arguments so you can pass [supported flags](https://www.terraform.io/docs/cli/commands/fmt.html#usage). Eg:
716+
`terraform_fmt` supports custom arguments so you can pass [supported flags](https://www.terraform.io/docs/cli/commands/fmt.html#usage). Eg:
706717

707-
```yaml
708-
- id: terraform_fmt
709-
args:
710-
- --args=-no-color
711-
- --args=-diff
712-
- --args=-write=false
713-
```
718+
```yaml
719+
- id: terraform_fmt
720+
args:
721+
- --args=-no-color
722+
- --args=-diff
723+
- --args=-write=false
724+
```
714725

715726
### terraform_providers_lock
716727

@@ -834,23 +845,14 @@ To replicate functionality in `terraform_docs` hook:
834845
- --args=--enable-rule=terraform_documented_variables
835846
```
836847

837-
2. When you have multiple directories and want to run `tflint` in all of them and share a single config file, it is impractical to hard-code the path to the `.tflint.hcl` file. The solution is to use the `__GIT_WORKING_DIR__` placeholder which will be replaced by `terraform_tflint` hooks with the Git working directory (repo root) at run time. For example:
838-
839-
```yaml
840-
- id: terraform_tflint
841-
args:
842-
- --args=--config=__GIT_WORKING_DIR__/.tflint.hcl
843-
```
844-
845-
3. By default, pre-commit-terraform performs directory switching into the terraform modules for you. If you want to delegate the directory changing to the binary - this will allow tflint to determine the full paths for error/warning messages, rather than just module relative paths. *Note: this requires `tflint>=0.44.0`.* For example:
848+
2. By default, pre-commit-terraform performs directory switching into the terraform modules for you. If you want to delegate the directory changing to the binary - this will allow tflint to determine the full paths for error/warning messages, rather than just module relative paths. *Note: this requires `tflint>=0.44.0`.* For example:
846849

847850
```yaml
848851
- id: terraform_tflint
849852
args:
850853
- --hook-config=--delegate-chdir
851854
```
852855

853-
854856
### terraform_tfsec (deprecated)
855857

856858
**DEPRECATED**. [tfsec was replaced by trivy](https://github.com/aquasecurity/tfsec/discussions/1994), so please use [`terraform_trivy`](#terraform_trivy).
@@ -894,22 +896,6 @@ To replicate functionality in `terraform_docs` hook:
894896
-e aws-s3-enable-bucket-logging,aws-s3-specify-public-access-block
895897
```
896898

897-
4. When you have multiple directories and want to run `tfsec` in all of them and share a single config file - use the `__GIT_WORKING_DIR__` placeholder. It will be replaced by `terraform_tfsec` hooks with Git working directory (repo root) at run time. For example:
898-
899-
```yaml
900-
- id: terraform_tfsec
901-
args:
902-
- --args=--config-file=__GIT_WORKING_DIR__/.tfsec.json
903-
```
904-
905-
Otherwise, will be used files that located in sub-folders:
906-
907-
```yaml
908-
- id: terraform_tfsec
909-
args:
910-
- --args=--config-file=.tfsec.json
911-
```
912-
913899
### terraform_trivy
914900

915901
1. `terraform_trivy` will consume modified files that pre-commit
@@ -951,14 +937,6 @@ To replicate functionality in `terraform_docs` hook:
951937
- --args=--skip-dirs="**/.terraform"
952938
```
953939

954-
4. When you have multiple directories and want to run `trivy` in all of them and share a single config file - use the `__GIT_WORKING_DIR__` placeholder. It will be replaced by `terraform_trivy` hooks with Git working directory (repo root) at run time. For example:
955-
956-
```yaml
957-
- id: terraform_trivy
958-
args:
959-
- --args=--ignorefile=__GIT_WORKING_DIR__/.trivyignore
960-
```
961-
962940
### terraform_validate
963941

964942
> [!IMPORTANT]
@@ -1123,14 +1101,6 @@ If the generated name is incorrect, set them by providing the `module-repo-short
11231101

11241102
See the `terrascan run -h` command line help for available options.
11251103

1126-
To pass the config file path, use the `__GIT_WORKING_DIR__` placeholder. It will be replaced with the Git working directory (repo root) at run time. For example:
1127-
1128-
```yaml
1129-
- id: terrascan
1130-
args:
1131-
- --args=--config-path=__GIT_WORKING_DIR__/.terrascan.toml
1132-
```
1133-
11341104
2. Use the `--args=--verbose` parameter to see the rule ID in the scanning output. Useful to skip validations.
11351105
3. Use `--skip-rules="ruleID1,ruleID2"` parameter to skip one or more rules globally while scanning (e.g.: `--args=--skip-rules="ruleID1,ruleID2"`).
11361106
4. Use the syntax `#ts:skip=RuleID optional_comment` inside a resource to skip the rule for that resource.

hooks/_common.sh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,9 @@ function common::parse_cmdline {
8383
# into a proper array, where each element is a standalone array slice
8484
# with quoted elements being treated as a standalone slice of array as well.
8585
while read -r -d '' ARG; do
86-
ARGS+=("$ARG")
86+
# Also replace any occurrence of `__GIT_WORKING_DIR__` with
87+
# actual path to Git working dir (repo root)
88+
ARGS+=("${ARG//__GIT_WORKING_DIR__/$PWD}")
8789
done < <(echo "$1" | xargs printf '%s\0')
8890
shift
8991
;;

hooks/terraform_checkov.sh

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,13 @@ function main {
1212
common::parse_cmdline "$@"
1313
common::export_provided_env_vars "${ENV_VARS[@]}"
1414
common::parse_and_export_env_vars
15-
# Support for setting PATH to repo root.
16-
for i in "${!ARGS[@]}"; do
17-
ARGS[i]=${ARGS[i]/__GIT_WORKING_DIR__/$(pwd)\/}
18-
done
1915

2016
# Suppress checkov color
2117
if [ "$PRE_COMMIT_COLOR" = "never" ]; then
2218
export ANSI_COLORS_DISABLED=true
2319
fi
2420

21+
# shellcheck disable=SC2153 # ARGS is set in common::parse_cmdline
2522
common::per_dir_hook "$HOOK_ID" "${#ARGS[@]}" "${ARGS[@]}" "${FILES[@]}"
2623
}
2724

hooks/terraform_tflint.sh

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,13 @@ function main {
1313
common::parse_cmdline "$@"
1414
common::export_provided_env_vars "${ENV_VARS[@]}"
1515
common::parse_and_export_env_vars
16-
# Support for setting PATH to repo root.
17-
for i in "${!ARGS[@]}"; do
18-
ARGS[i]=${ARGS[i]/__GIT_WORKING_DIR__/$(pwd)\/}
19-
done
16+
2017
# JFYI: tflint color already suppressed via PRE_COMMIT_COLOR=never
2118

2219
# Run `tflint --init` for check that plugins installed.
2320
# It should run once on whole repo.
2421
{
22+
# shellcheck disable=SC2153 # ARGS is set in common::parse_cmdline
2523
TFLINT_INIT=$(tflint --init "${ARGS[@]}" 2>&1) 2> /dev/null &&
2624
common::colorify "green" "Command 'tflint --init' successfully done:" &&
2725
echo -e "${TFLINT_INIT}\n\n\n"

hooks/terraform_tfsec.sh

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,6 @@ function main {
1212
common::parse_cmdline "$@"
1313
common::export_provided_env_vars "${ENV_VARS[@]}"
1414
common::parse_and_export_env_vars
15-
# Support for setting PATH to repo root.
16-
for i in "${!ARGS[@]}"; do
17-
ARGS[i]=${ARGS[i]/__GIT_WORKING_DIR__/$(pwd)\/}
18-
done
1915

2016
# Suppress tfsec color
2117
if [ "$PRE_COMMIT_COLOR" = "never" ]; then

hooks/terraform_trivy.sh

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,8 @@ function main {
1212
common::parse_cmdline "$@"
1313
common::export_provided_env_vars "${ENV_VARS[@]}"
1414
common::parse_and_export_env_vars
15-
# Support for setting PATH to repo root.
16-
for i in "${!ARGS[@]}"; do
17-
ARGS[i]=${ARGS[i]/__GIT_WORKING_DIR__/$(pwd)\/}
18-
done
1915

16+
# shellcheck disable=SC2153 # ARGS is set in common::parse_cmdline
2017
common::per_dir_hook "$HOOK_ID" "${#ARGS[@]}" "${ARGS[@]}" "${FILES[@]}"
2118
}
2219

0 commit comments

Comments
 (0)