Skip to content

Commit caa01c3

Browse files
authored
feat: Allow env vars expansion in --args section for all hooks (antonbabenko#363)
1 parent 95ca356 commit caa01c3

14 files changed

+68
-0
lines changed

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ If you are using `pre-commit-terraform` already or want to support its developme
3636
* [4. Run](#4-run)
3737
* [Available Hooks](#available-hooks)
3838
* [Hooks usage notes and examples](#hooks-usage-notes-and-examples)
39+
* [All hooks: Usage of environment variables in `--args`](#all-hooks-usage-of-environment-variables-in---args)
3940
* [checkov (deprecated) and terraform_checkov](#checkov-deprecated-and-terraform_checkov)
4041
* [infracost_breakdown](#infracost_breakdown)
4142
* [terraform_docs](#terraform_docs)
@@ -238,6 +239,24 @@ Check the [source file](https://github.com/antonbabenko/pre-commit-terraform/blo
238239

239240
## Hooks usage notes and examples
240241

242+
### All hooks: Usage of environment variables in `--args`
243+
244+
> All, except deprecated hooks: `checkov`, `terraform_docs_replace`
245+
246+
You can use environment variables for the `--args` section.
247+
Note: You _must_ use the `${ENV_VAR}` definition, `$ENV_VAR` will not expand.
248+
249+
Config example:
250+
251+
```yaml
252+
- id: terraform_tflint
253+
args:
254+
- --args=--config=${CONFIG_NAME}.${CONFIG_EXT}
255+
- --args=--module
256+
```
257+
258+
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 --module`.
259+
241260
### checkov (deprecated) and terraform_checkov
242261

243262
> `checkov` hook is deprecated, please use `terraform_checkov`.

hooks/_common.sh

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,43 @@ function common::parse_cmdline {
5555
done
5656
}
5757

58+
#######################################################################
59+
# Expand environment variables definition into their values in '--args'.
60+
# Support expansion only for ${ENV_VAR} vars, not $ENV_VAR.
61+
# Globals (modify):
62+
# ARGS (array) arguments that configure wrapped tool behavior
63+
#######################################################################
64+
function common::parse_and_export_env_vars {
65+
local arg_idx
66+
67+
for arg_idx in "${!ARGS[@]}"; do
68+
local arg="${ARGS[$arg_idx]}"
69+
70+
# Repeat until all env vars will be expanded
71+
while true; do
72+
# Check if at least 1 env var exists in `$arg`
73+
# shellcheck disable=SC2016 # '${' should not be expanded
74+
if [[ "$arg" =~ .*'${'[A-Z_][A-Z0-9_]+?'}'.* ]]; then
75+
# Get `ENV_VAR` from `.*${ENV_VAR}.*`
76+
local env_var_name=${arg#*$\{}
77+
env_var_name=${env_var_name%%\}*}
78+
local env_var_value="${!env_var_name}"
79+
# shellcheck disable=SC2016 # '${' should not be expanded
80+
common::colorify "green" 'Found ${'"$env_var_name"'} in: '"'$arg'"
81+
# Replace env var name with its value.
82+
# `$arg` will be checked in `if` conditional, `$ARGS` will be used in the next functions.
83+
# shellcheck disable=SC2016 # '${' should not be expanded
84+
arg=${arg/'${'$env_var_name'}'/$env_var_value}
85+
ARGS[$arg_idx]=$arg
86+
# shellcheck disable=SC2016 # '${' should not be expanded
87+
common::colorify "green" 'After ${'"$env_var_name"'} expansion: '"'$arg'\n"
88+
continue
89+
fi
90+
break
91+
done
92+
done
93+
}
94+
5895
#######################################################################
5996
# This is a workaround to improve performance when all files are passed
6097
# See: https://github.com/antonbabenko/pre-commit-terraform/issues/309

hooks/infracost_breakdown.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ readonly SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)"
1313
function main {
1414
common::initialize "$SCRIPT_DIR"
1515
common::parse_cmdline "$@"
16+
common::parse_and_export_env_vars
1617
# shellcheck disable=SC2153 # False positive
1718
infracost_breakdown_ "${HOOK_CONFIG[*]}" "${ARGS[*]}"
1819
}

hooks/terraform_checkov.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ readonly SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)"
1313
function main {
1414
common::initialize "$SCRIPT_DIR"
1515
common::parse_cmdline "$@"
16+
common::parse_and_export_env_vars
1617
# shellcheck disable=SC2153 # False positive
1718
common::per_dir_hook "${ARGS[*]}" "$HOOK_ID" "${FILES[@]}"
1819
}

hooks/terraform_docs.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ readonly SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)"
1313
function main {
1414
common::initialize "$SCRIPT_DIR"
1515
common::parse_cmdline "$@"
16+
common::parse_and_export_env_vars
1617
# Support for setting relative PATH to .terraform-docs.yml config.
1718
# shellcheck disable=SC2178 # It's the simplest syntax for that case
1819
ARGS=${ARGS[*]/--config=/--config=$(pwd)\/}

hooks/terraform_fmt.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ readonly SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)"
1313
function main {
1414
common::initialize "$SCRIPT_DIR"
1515
common::parse_cmdline "$@"
16+
common::parse_and_export_env_vars
1617
# shellcheck disable=SC2153 # False positive
1718
terraform_fmt_ "${ARGS[*]}" "${FILES[@]}"
1819
}

hooks/terraform_providers_lock.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ readonly SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)"
1313
function main {
1414
common::initialize "$SCRIPT_DIR"
1515
common::parse_cmdline "$@"
16+
common::parse_and_export_env_vars
1617
# shellcheck disable=SC2153 # False positive
1718
common::per_dir_hook "${ARGS[*]}" "$HOOK_ID" "${FILES[@]}"
1819
}

hooks/terraform_tflint.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ readonly SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)"
1313
function main {
1414
common::initialize "$SCRIPT_DIR"
1515
common::parse_cmdline "$@"
16+
common::parse_and_export_env_vars
1617
# Support for setting PATH to repo root.
1718
# shellcheck disable=SC2178 # It's the simplest syntax for that case
1819
ARGS=${ARGS[*]/__GIT_WORKING_DIR__/$(pwd)\/}

hooks/terraform_tfsec.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ readonly SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)"
1212
function main {
1313
common::initialize "$SCRIPT_DIR"
1414
common::parse_cmdline "$@"
15+
common::parse_and_export_env_vars
1516
# Support for setting PATH to repo root.
1617
# shellcheck disable=SC2178 # It's the simplest syntax for that case
1718
ARGS=${ARGS[*]/__GIT_WORKING_DIR__/$(pwd)\/}

hooks/terraform_validate.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export AWS_DEFAULT_REGION=${AWS_DEFAULT_REGION:-us-east-1}
1616
function main {
1717
common::initialize "$SCRIPT_DIR"
1818
parse_cmdline_ "$@"
19+
common::parse_and_export_env_vars
1920
terraform_validate_
2021
}
2122

0 commit comments

Comments
 (0)