From 0b00fd916f7f6996f047a39a5facd3163fb4ba6c Mon Sep 17 00:00:00 2001 From: MaxymVlasov Date: Thu, 7 Mar 2024 19:23:47 +0200 Subject: [PATCH 1/9] feat: add trace logs for debbuging proposes --- hooks/_common.sh | 2 ++ 1 file changed, 2 insertions(+) diff --git a/hooks/_common.sh b/hooks/_common.sh index d6dbbb272..52b6b17ab 100644 --- a/hooks/_common.sh +++ b/hooks/_common.sh @@ -1,6 +1,8 @@ #!/usr/bin/env bash set -eo pipefail +[[ $PCT_LOG == trace ]] && set -x # Enable Trace logs + # Hook ID, based on hook filename. # Hook filename MUST BE same with `- id` in .pre-commit-hooks.yaml file # shellcheck disable=SC2034 # Unused var. From 1cfaff9d06f110542326234419e27181c03c66e6 Mon Sep 17 00:00:00 2001 From: MaxymVlasov Date: Thu, 7 Mar 2024 20:55:28 +0200 Subject: [PATCH 2/9] Add more useful logging --- hooks/_common.sh | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/hooks/_common.sh b/hooks/_common.sh index 52b6b17ab..51940829b 100644 --- a/hooks/_common.sh +++ b/hooks/_common.sh @@ -1,8 +1,24 @@ #!/usr/bin/env bash set -eo pipefail -[[ $PCT_LOG == trace ]] && set -x # Enable Trace logs - +if [[ $PCT_LOG == trace ]]; then + + # FUNC_TRACE="${FUNCNAME[*]:1}" # Get functions in trace. Skip first two functions that used for logging + # func_trace=${FUNC_TRACE// / <- } # Replace ' ' to ' <- '. + 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 + # ${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. From 919a99b176f93c83e95e67a071569e75cb2698f7 Mon Sep 17 00:00:00 2001 From: MaxymVlasov Date: Thu, 7 Mar 2024 20:59:03 +0200 Subject: [PATCH 3/9] Remove not needed comments --- hooks/_common.sh | 2 -- 1 file changed, 2 deletions(-) diff --git a/hooks/_common.sh b/hooks/_common.sh index 51940829b..23ab69684 100644 --- a/hooks/_common.sh +++ b/hooks/_common.sh @@ -3,8 +3,6 @@ set -eo pipefail if [[ $PCT_LOG == trace ]]; then - # FUNC_TRACE="${FUNCNAME[*]:1}" # Get functions in trace. Skip first two functions that used for logging - # func_trace=${FUNC_TRACE// / <- } # Replace ' ' to ' <- '. echo "BASH path: '$BASH'" echo "BASH_VERSION: $BASH_VERSION" echo "BASHOPTS: $BASHOPTS" From 42b6ffb7d6aa8ba782fcacc5a05923a40a1a7743 Mon Sep 17 00:00:00 2001 From: MaxymVlasov Date: Tue, 12 Mar 2024 20:46:26 +0200 Subject: [PATCH 4/9] fix regex for non-linux bash'es --- hooks/_common.sh | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/hooks/_common.sh b/hooks/_common.sh index 23ab69684..68ce2b195 100644 --- a/hooks/_common.sh +++ b/hooks/_common.sh @@ -127,8 +127,7 @@ function common::parse_and_export_env_vars { # Repeat until all env vars will be expanded 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%%\}*} From 3418a679e14fd67a1b0637088805e30da6ab40b2 Mon Sep 17 00:00:00 2001 From: MaxymVlasov Date: Tue, 12 Mar 2024 20:48:47 +0200 Subject: [PATCH 5/9] f --- hooks/_common.sh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/hooks/_common.sh b/hooks/_common.sh index 68ce2b195..2b258d1c7 100644 --- a/hooks/_common.sh +++ b/hooks/_common.sh @@ -127,7 +127,8 @@ function common::parse_and_export_env_vars { # Repeat until all env vars will be expanded while true; do # Check if at least 1 env var exists in `$arg` - if [[ "$arg" =~ \${[A-Z_][A-Z0-9_]+} ]]; then + # shellcheck disable=SC2016 # '${' should not be expanded + 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%%\}*} From e76ffce8e949b9269edfd9101b72526a307401c3 Mon Sep 17 00:00:00 2001 From: MaxymVlasov Date: Tue, 12 Mar 2024 20:50:49 +0200 Subject: [PATCH 6/9] Allow one-symbol ENVs --- hooks/_common.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hooks/_common.sh b/hooks/_common.sh index 2b258d1c7..182eb3acb 100644 --- a/hooks/_common.sh +++ b/hooks/_common.sh @@ -128,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%%\}*} From a99c2bd75d812f85d99c61cccbf667895a517b53 Mon Sep 17 00:00:00 2001 From: MaxymVlasov Date: Tue, 12 Mar 2024 21:11:38 +0200 Subject: [PATCH 7/9] Add docs about log levels --- README.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/README.md b/README.md index 2b32d3f8f..5f5b3393c 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 implement in #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`. From c71b65619d78da05ea3be5f374fa3d679ed5c1c4 Mon Sep 17 00:00:00 2001 From: MaxymVlasov Date: Tue, 12 Mar 2024 21:14:49 +0200 Subject: [PATCH 8/9] docs fix --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 5f5b3393c..9c7b4128c 100644 --- a/README.md +++ b/README.md @@ -352,7 +352,7 @@ For example: PCT_LOG=trace pre-commit run -a ``` -Less verbose log levels will be implement in #562 +Less verbose log levels will be implemented in [#562](https://github.com/antonbabenko/pre-commit-terraform/issues/562). ### Many hooks: Parallelism From 8604cee001db3684ca19239160a8a7da46298732 Mon Sep 17 00:00:00 2001 From: Maksym Vlasov Date: Wed, 13 Mar 2024 22:45:56 +0200 Subject: [PATCH 9/9] Apply suggestions from code review Co-authored-by: George L. Yermulnik --- hooks/_common.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hooks/_common.sh b/hooks/_common.sh index 182eb3acb..e6d610af3 100644 --- a/hooks/_common.sh +++ b/hooks/_common.sh @@ -8,7 +8,7 @@ if [[ $PCT_LOG == trace ]]; then echo "BASHOPTS: $BASHOPTS" echo "OSTYPE: $OSTYPE" - # ${FUNCNAME[*]} - reverse functions trace. Each new function call append to start + # ${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