feat: Suppress color for all hooks if PRE_COMMIT_COLOR=never set (#409)

This commit is contained in:
Maksym Vlasov 2022-07-06 15:34:13 +03:00 committed by Anton Babenko
commit b12f0c662c
15 changed files with 78 additions and 11 deletions

View file

@ -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=$?

View file

@ -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

View file

@ -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[@]}"
}

View file

@ -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"
}
#######################################################################

View file

@ -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[@]}"
}

View file

@ -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[@]}"
}

View file

@ -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[@]}"
}

View file

@ -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[@]}"
}

View file

@ -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[@]}"
}

View file

@ -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

View file

@ -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[@]}"
}

View file

@ -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[@]}"
}

View file

@ -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[@]}"
}

View file

@ -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[@]}"
}