From 70a499615f49bf15cb683ffb4ab31275f4465710 Mon Sep 17 00:00:00 2001 From: Maksym Vlasov Date: Wed, 13 Mar 2024 22:47:41 +0200 Subject: [PATCH] fix(non-linux): Bash environment variables in arguments not expanded + Add `trace` log level (#645) --------- Co-authored-by: George L. Yermulnik --- README.md | 13 +++++++++++++ hooks/_common.sh | 18 +++++++++++++++++- 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 2b32d3f8f..9c7b4128c 100644 --- a/README.md +++ b/README.md @@ -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) @@ -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`. diff --git a/hooks/_common.sh b/hooks/_common.sh index d6dbbb272..e6d610af3 100644 --- a/hooks/_common.sh +++ b/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. @@ -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%%\}*}