Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(non-linux): Bash environment variables in arguments not expanded + Add trace log level #645

Merged
merged 10 commits into from
Mar 13, 2024
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ If you are using `pre-commit-terraform` already or want to support its developme
* [All hooks: Usage of environment variables in `--args`](#all-hooks-usage-of-environment-variables-in---args)
* [All hooks: Set env vars inside hook at runtime](#all-hooks-set-env-vars-inside-hook-at-runtime)
* [All hooks: Disable color output](#all-hooks-disable-color-output)
* [All hooks: Log levels](#all-hooks-log-levels)
* [Many hooks: Parallelism](#many-hooks-parallelism)
* [checkov (deprecated) and terraform\_checkov](#checkov-deprecated-and-terraform_checkov)
* [infracost\_breakdown](#infracost_breakdown)
Expand Down Expand Up @@ -341,6 +342,18 @@ To disable color output for all hooks, set `PRE_COMMIT_COLOR=never` var. Eg:
PRE_COMMIT_COLOR=never pre-commit run
```

### All hooks: Log levels

In case you need to debug hooks, you can set `PCT_LOG=trace`.

For example:

```bash
PCT_LOG=trace pre-commit run -a
```

Less verbose log levels will be implemented in [#562](https://github.com/antonbabenko/pre-commit-terraform/issues/562).

### 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`.
Expand Down
18 changes: 17 additions & 1 deletion hooks/_common.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,22 @@
#!/usr/bin/env bash
set -eo pipefail

if [[ $PCT_LOG == trace ]]; then

echo "BASH path: '$BASH'"
echo "BASH_VERSION: $BASH_VERSION"
echo "BASHOPTS: $BASHOPTS"
echo "OSTYPE: $OSTYPE"

# ${FUNCNAME[*]} - reverse functions trace. Each new function call append to start
MaxymVlasov marked this conversation as resolved.
Show resolved Hide resolved
# ${BASH_SOURCE##*/} - get filename
# $LINENO - get line number
export PS4='\e[2m
trace: ${FUNCNAME[*]}
${BASH_SOURCE##*/}:$LINENO: \e[0m'

set -x
fi
# Hook ID, based on hook filename.
# Hook filename MUST BE same with `- id` in .pre-commit-hooks.yaml file
# shellcheck disable=SC2034 # Unused var.
Expand Down Expand Up @@ -112,7 +128,7 @@ function common::parse_and_export_env_vars {
while true; do
# Check if at least 1 env var exists in `$arg`
# shellcheck disable=SC2016 # '${' should not be expanded
if [[ "$arg" =~ .*'${'[A-Z_][A-Z0-9_]+?'}'.* ]]; then
if [[ "$arg" =~ '${'[A-Z_][A-Z0-9_]*'}' ]]; then
# Get `ENV_VAR` from `.*${ENV_VAR}.*`
local env_var_name=${arg#*$\{}
env_var_name=${env_var_name%%\}*}
Expand Down
Loading