diff --git a/README.md b/README.md index e47dd774e..88287ee2e 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 * [Hooks usage notes and examples](#hooks-usage-notes-and-examples) * [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) * [checkov (deprecated) and terraform_checkov](#checkov-deprecated-and-terraform_checkov) * [infracost_breakdown](#infracost_breakdown) * [terraform_docs](#terraform_docs) @@ -300,6 +301,16 @@ Config example: - --envs=AWS_SECRET_ACCESS_KEY="asecretkey" ``` +### All hooks: Disable color output + +> All, except deprecated hooks: `checkov`, `terraform_docs_replace` + +To disable color output for all hooks, set `PRE_COMMIT_COLOR=never` var. Eg: + +```bash +PRE_COMMIT_COLOR=never pre-commit run +``` + ### checkov (deprecated) and terraform_checkov > `checkov` hook is deprecated, please use `terraform_checkov`. @@ -422,7 +433,6 @@ Unlike most other hooks, this hook triggers once if there are any changed files * `.projects[].diff.totalHourlyCost` - show the difference in hourly cost for the existing infra and tf plan * `.projects[].diff.totalMonthlyCost` - show the difference in monthly cost for the existing infra and tf plan * `.diffTotalHourlyCost` (for Infracost version 0.9.12 or newer) or `[.projects[].diff.totalMonthlyCost | select (.!=null) | tonumber] | add` (for Infracost older than 0.9.12) - * To disable hook color output, set `PRE_COMMIT_COLOR=never` env var. 4. **Docker usage**. In `docker build` or `docker run` command: * You need to provide [Infracost API key](https://www.infracost.io/docs/integrations/environment_variables/#infracost_api_key) via `-e INFRACOST_API_KEY=`. By default, it is saved in `~/.config/infracost/credentials.yml` diff --git a/hooks/_common.sh b/hooks/_common.sh index 77cf83257..b7dba192b 100644 --- a/hooks/_common.sh +++ b/hooks/_common.sh @@ -267,6 +267,11 @@ function common::terraform_init { local exit_code=0 local init_output + # Suppress terraform init color + if [ "$PRE_COMMIT_COLOR" = "never" ]; then + TF_INIT_ARGS+=("-no-color") + fi + if [ ! -d .terraform ]; then init_output=$(terraform init -backend=false "${TF_INIT_ARGS[@]}" 2>&1) exit_code=$? diff --git a/hooks/infracost_breakdown.sh b/hooks/infracost_breakdown.sh index aecc4be0d..2f3812338 100755 --- a/hooks/infracost_breakdown.sh +++ b/hooks/infracost_breakdown.sh @@ -35,7 +35,7 @@ function infracost_breakdown_ { # Get hook settings IFS=";" read -r -a checks <<< "$hook_config" - + # Suppress infracost color if [ "$PRE_COMMIT_COLOR" = "never" ]; then args+=("--no-color") fi diff --git a/hooks/terraform_checkov.sh b/hooks/terraform_checkov.sh index 13008e8cf..bd37f9778 100755 --- a/hooks/terraform_checkov.sh +++ b/hooks/terraform_checkov.sh @@ -15,6 +15,10 @@ function main { # Support for setting PATH to repo root. # shellcheck disable=SC2178 # It's the simplest syntax for that case ARGS=${ARGS[*]/__GIT_WORKING_DIR__/$(pwd)\/} + # Suppress checkov color + if [ "$PRE_COMMIT_COLOR" = "never" ]; then + export ANSI_COLORS_DISABLED=true + fi # shellcheck disable=SC2128 # It's the simplest syntax for that case common::per_dir_hook "$ARGS" "$HOOK_ID" "${FILES[@]}" } diff --git a/hooks/terraform_docs.sh b/hooks/terraform_docs.sh index 5b43deb87..fb4d44176 100755 --- a/hooks/terraform_docs.sh +++ b/hooks/terraform_docs.sh @@ -2,9 +2,6 @@ set -eo pipefail # globals variables -# hook ID, see `- id` for details in .pre-commit-hooks.yaml file -# shellcheck disable=SC2034 # Unused var. -readonly HOOK_ID='terraform_docs' # shellcheck disable=SC2155 # No way to assign to readonly variable in separate lines readonly SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)" # shellcheck source=_common.sh @@ -90,7 +87,7 @@ function terraform_docs_ { function terraform_docs { local -r terraform_docs_awk_file="$1" local -r hook_config="$2" - local -r args="$3" + local args="$3" shift 3 local -a -r files=("$@") @@ -136,10 +133,29 @@ function terraform_docs { esac done - # # Override formatter if no config file set - # - [[ "$args" != *"--config="* ]] && local tf_docs_formatter="md" + if [[ "$args" != *"--config"* ]]; then + local tf_docs_formatter="md" + + # Suppress terraform_docs color + else + + local config_file=${args#*--config} + config_file=${config_file#*=} + config_file=${config_file% *} + + local config_file_no_color + config_file_no_color="$config_file$(date +%s).yml" + + if [ "$PRE_COMMIT_COLOR" = "never" ] && + [[ $(grep -e '^formatter:' "$config_file") == *"pretty"* ]] && + [[ $(grep ' color: ' "$config_file") != *"false"* ]]; then + + cp "$config_file" "$config_file_no_color" + echo -e "settings:\n color: false" >> "$config_file_no_color" + args=${args/$config_file/$config_file_no_color} + fi + fi local dir_path for dir_path in $(echo "${paths[*]}" | tr ' ' '\n' | sort -u); do @@ -212,6 +228,9 @@ function terraform_docs { popd > /dev/null done + + # Cleanup + [ -e "$config_file_no_color" ] && rm -f "$config_file_no_color" } ####################################################################### diff --git a/hooks/terraform_fmt.sh b/hooks/terraform_fmt.sh index b89266cde..637bd9e14 100755 --- a/hooks/terraform_fmt.sh +++ b/hooks/terraform_fmt.sh @@ -12,6 +12,12 @@ function main { common::parse_cmdline "$@" common::export_provided_env_vars "${ENVS[@]}" common::parse_and_export_env_vars + + # Suppress terraform fmt color + if [ "$PRE_COMMIT_COLOR" = "never" ]; then + ARGS+=("-no-color") + fi + # shellcheck disable=SC2153 # False positive common::per_dir_hook "${ARGS[*]}" "$HOOK_ID" "${FILES[@]}" } diff --git a/hooks/terraform_providers_lock.sh b/hooks/terraform_providers_lock.sh index 4edeeabf8..5b84ba30d 100755 --- a/hooks/terraform_providers_lock.sh +++ b/hooks/terraform_providers_lock.sh @@ -13,6 +13,8 @@ function main { common::parse_cmdline "$@" common::export_provided_env_vars "${ENVS[@]}" common::parse_and_export_env_vars + # JFYI: suppress color for `terraform providers lock` is N/A` + # shellcheck disable=SC2153 # False positive common::per_dir_hook "${ARGS[*]}" "$HOOK_ID" "${FILES[@]}" } diff --git a/hooks/terraform_tflint.sh b/hooks/terraform_tflint.sh index 8d5707e97..192786ee9 100755 --- a/hooks/terraform_tflint.sh +++ b/hooks/terraform_tflint.sh @@ -16,7 +16,7 @@ function main { # Support for setting PATH to repo root. # shellcheck disable=SC2178 # It's the simplest syntax for that case ARGS=${ARGS[*]/__GIT_WORKING_DIR__/$(pwd)\/} - # shellcheck disable=SC2128 # It's the simplest syntax for that case + # JFYI: tflint color already suppressed via PRE_COMMIT_COLOR=never # Run `tflint --init` for check that plugins installed. # It should run once on whole repo. @@ -30,7 +30,7 @@ function main { echo "${TFLINT_INIT}" return ${exit_code} } - + # shellcheck disable=SC2128 # It's the simplest syntax for that case common::per_dir_hook "$ARGS" "$HOOK_ID" "${FILES[@]}" } diff --git a/hooks/terraform_tfsec.sh b/hooks/terraform_tfsec.sh index fdbb436f8..6538dc7b5 100755 --- a/hooks/terraform_tfsec.sh +++ b/hooks/terraform_tfsec.sh @@ -15,6 +15,13 @@ function main { # Support for setting PATH to repo root. # shellcheck disable=SC2178 # It's the simplest syntax for that case ARGS=${ARGS[*]/__GIT_WORKING_DIR__/$(pwd)\/} + + # Suppress tfsec color + if [ "$PRE_COMMIT_COLOR" = "never" ]; then + # shellcheck disable=SC2178,SC2128 # It's the simplest syntax for that case + ARGS+=" --no-color" + fi + # shellcheck disable=SC2128 # It's the simplest syntax for that case common::per_dir_hook "$ARGS" "$HOOK_ID" "${FILES[@]}" } diff --git a/hooks/terraform_validate.sh b/hooks/terraform_validate.sh index be5f1e067..6a3e41ad0 100755 --- a/hooks/terraform_validate.sh +++ b/hooks/terraform_validate.sh @@ -15,6 +15,11 @@ function main { common::parse_cmdline "$@" common::export_provided_env_vars "${ENVS[@]}" common::parse_and_export_env_vars + + # Suppress terraform validate color + if [ "$PRE_COMMIT_COLOR" = "never" ]; then + ARGS+=("-no-color") + fi # shellcheck disable=SC2153 # False positive common::per_dir_hook "${ARGS[*]}" "$HOOK_ID" "${FILES[@]}" } diff --git a/hooks/terraform_wrapper_module_for_each.sh b/hooks/terraform_wrapper_module_for_each.sh index 905fa8de9..1fd45c30b 100755 --- a/hooks/terraform_wrapper_module_for_each.sh +++ b/hooks/terraform_wrapper_module_for_each.sh @@ -12,6 +12,7 @@ function main { common::parse_cmdline "$@" common::export_provided_env_vars "${ENVS[@]}" common::parse_and_export_env_vars + # JFYI: suppress color for `hcledit` is N/A` check_dependencies diff --git a/hooks/terragrunt_fmt.sh b/hooks/terragrunt_fmt.sh index 5ef51570d..52ded2653 100755 --- a/hooks/terragrunt_fmt.sh +++ b/hooks/terragrunt_fmt.sh @@ -12,6 +12,8 @@ function main { common::parse_cmdline "$@" common::export_provided_env_vars "${ENVS[@]}" common::parse_and_export_env_vars + # JFYI: terragrunt hclfmt color already suppressed via PRE_COMMIT_COLOR=never + # shellcheck disable=SC2153 # False positive common::per_dir_hook "${ARGS[*]}" "$HOOK_ID" "${FILES[@]}" } diff --git a/hooks/terragrunt_validate.sh b/hooks/terragrunt_validate.sh index ae9be6b17..552dce842 100755 --- a/hooks/terragrunt_validate.sh +++ b/hooks/terragrunt_validate.sh @@ -12,6 +12,8 @@ function main { common::parse_cmdline "$@" common::export_provided_env_vars "${ENVS[@]}" common::parse_and_export_env_vars + # JFYI: terragrunt validate color already suppressed via PRE_COMMIT_COLOR=never + # shellcheck disable=SC2153 # False positive common::per_dir_hook "${ARGS[*]}" "$HOOK_ID" "${FILES[@]}" } diff --git a/hooks/terrascan.sh b/hooks/terrascan.sh index ca8d87452..63ab66dfd 100755 --- a/hooks/terrascan.sh +++ b/hooks/terrascan.sh @@ -12,6 +12,8 @@ function main { common::parse_cmdline "$@" common::export_provided_env_vars "${ENVS[@]}" common::parse_and_export_env_vars + # JFYI: terrascan color already suppressed via PRE_COMMIT_COLOR=never + # shellcheck disable=SC2153 # False positive common::per_dir_hook "${ARGS[*]}" "$HOOK_ID" "${FILES[@]}" } diff --git a/hooks/tfupdate.sh b/hooks/tfupdate.sh index f720fbb3d..080d2e05e 100755 --- a/hooks/tfupdate.sh +++ b/hooks/tfupdate.sh @@ -12,6 +12,8 @@ function main { common::parse_cmdline "$@" common::export_provided_env_vars "${ENVS[@]}" common::parse_and_export_env_vars + # JFYI: suppress color for `tfupdate` is N/A` + # shellcheck disable=SC2153 # False positive common::per_dir_hook "${ARGS[*]}" "$HOOK_ID" "${FILES[@]}" }