Skip to content

Commit

Permalink
fix(non-linux): Bash environment variables in arguments not expanded …
Browse files Browse the repository at this point in the history
…+ Add `trace` log level (#645)

---------

Co-authored-by: George L. Yermulnik <yz@yz.kiev.ua>
  • Loading branch information
MaxymVlasov and yermulnik committed Mar 13, 2024
1 parent 051dcd5 commit a2a2990
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
13 changes: 13 additions & 0 deletions README.md
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
@@ -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[*]} - function calls in reversed order. Each new function call is appended to the beginning
# ${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

0 comments on commit a2a2990

Please sign in to comment.