mirror of
https://github.com/tofuutils/pre-commit-opentofu.git
synced 2025-10-15 17:38:54 +02:00
feat: Suppress color for all hooks if PRE_COMMIT_COLOR=never set (#409)
This commit is contained in:
parent
d4902313ce
commit
b12f0c662c
15 changed files with 78 additions and 11 deletions
12
README.md
12
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)
|
* [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: 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: 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)
|
* [checkov (deprecated) and terraform_checkov](#checkov-deprecated-and-terraform_checkov)
|
||||||
* [infracost_breakdown](#infracost_breakdown)
|
* [infracost_breakdown](#infracost_breakdown)
|
||||||
* [terraform_docs](#terraform_docs)
|
* [terraform_docs](#terraform_docs)
|
||||||
|
|
@ -300,6 +301,16 @@ Config example:
|
||||||
- --envs=AWS_SECRET_ACCESS_KEY="asecretkey"
|
- --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 (deprecated) and terraform_checkov
|
||||||
|
|
||||||
> `checkov` hook is deprecated, please use `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.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
|
* `.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)
|
* `.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:
|
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=<your token>`. By default, it is saved in `~/.config/infracost/credentials.yml`
|
* You need to provide [Infracost API key](https://www.infracost.io/docs/integrations/environment_variables/#infracost_api_key) via `-e INFRACOST_API_KEY=<your token>`. By default, it is saved in `~/.config/infracost/credentials.yml`
|
||||||
|
|
|
||||||
|
|
@ -267,6 +267,11 @@ function common::terraform_init {
|
||||||
local exit_code=0
|
local exit_code=0
|
||||||
local init_output
|
local init_output
|
||||||
|
|
||||||
|
# Suppress terraform init color
|
||||||
|
if [ "$PRE_COMMIT_COLOR" = "never" ]; then
|
||||||
|
TF_INIT_ARGS+=("-no-color")
|
||||||
|
fi
|
||||||
|
|
||||||
if [ ! -d .terraform ]; then
|
if [ ! -d .terraform ]; then
|
||||||
init_output=$(terraform init -backend=false "${TF_INIT_ARGS[@]}" 2>&1)
|
init_output=$(terraform init -backend=false "${TF_INIT_ARGS[@]}" 2>&1)
|
||||||
exit_code=$?
|
exit_code=$?
|
||||||
|
|
|
||||||
|
|
@ -35,7 +35,7 @@ function infracost_breakdown_ {
|
||||||
|
|
||||||
# Get hook settings
|
# Get hook settings
|
||||||
IFS=";" read -r -a checks <<< "$hook_config"
|
IFS=";" read -r -a checks <<< "$hook_config"
|
||||||
|
# Suppress infracost color
|
||||||
if [ "$PRE_COMMIT_COLOR" = "never" ]; then
|
if [ "$PRE_COMMIT_COLOR" = "never" ]; then
|
||||||
args+=("--no-color")
|
args+=("--no-color")
|
||||||
fi
|
fi
|
||||||
|
|
|
||||||
|
|
@ -15,6 +15,10 @@ function main {
|
||||||
# Support for setting PATH to repo root.
|
# Support for setting PATH to repo root.
|
||||||
# shellcheck disable=SC2178 # It's the simplest syntax for that case
|
# shellcheck disable=SC2178 # It's the simplest syntax for that case
|
||||||
ARGS=${ARGS[*]/__GIT_WORKING_DIR__/$(pwd)\/}
|
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
|
# shellcheck disable=SC2128 # It's the simplest syntax for that case
|
||||||
common::per_dir_hook "$ARGS" "$HOOK_ID" "${FILES[@]}"
|
common::per_dir_hook "$ARGS" "$HOOK_ID" "${FILES[@]}"
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,9 +2,6 @@
|
||||||
set -eo pipefail
|
set -eo pipefail
|
||||||
|
|
||||||
# globals variables
|
# 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
|
# shellcheck disable=SC2155 # No way to assign to readonly variable in separate lines
|
||||||
readonly SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)"
|
readonly SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)"
|
||||||
# shellcheck source=_common.sh
|
# shellcheck source=_common.sh
|
||||||
|
|
@ -90,7 +87,7 @@ function terraform_docs_ {
|
||||||
function terraform_docs {
|
function terraform_docs {
|
||||||
local -r terraform_docs_awk_file="$1"
|
local -r terraform_docs_awk_file="$1"
|
||||||
local -r hook_config="$2"
|
local -r hook_config="$2"
|
||||||
local -r args="$3"
|
local args="$3"
|
||||||
shift 3
|
shift 3
|
||||||
local -a -r files=("$@")
|
local -a -r files=("$@")
|
||||||
|
|
||||||
|
|
@ -136,10 +133,29 @@ function terraform_docs {
|
||||||
esac
|
esac
|
||||||
done
|
done
|
||||||
|
|
||||||
#
|
|
||||||
# Override formatter if no config file set
|
# Override formatter if no config file set
|
||||||
#
|
if [[ "$args" != *"--config"* ]]; then
|
||||||
[[ "$args" != *"--config="* ]] && local tf_docs_formatter="md"
|
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
|
local dir_path
|
||||||
for dir_path in $(echo "${paths[*]}" | tr ' ' '\n' | sort -u); do
|
for dir_path in $(echo "${paths[*]}" | tr ' ' '\n' | sort -u); do
|
||||||
|
|
@ -212,6 +228,9 @@ function terraform_docs {
|
||||||
|
|
||||||
popd > /dev/null
|
popd > /dev/null
|
||||||
done
|
done
|
||||||
|
|
||||||
|
# Cleanup
|
||||||
|
[ -e "$config_file_no_color" ] && rm -f "$config_file_no_color"
|
||||||
}
|
}
|
||||||
|
|
||||||
#######################################################################
|
#######################################################################
|
||||||
|
|
|
||||||
|
|
@ -12,6 +12,12 @@ function main {
|
||||||
common::parse_cmdline "$@"
|
common::parse_cmdline "$@"
|
||||||
common::export_provided_env_vars "${ENVS[@]}"
|
common::export_provided_env_vars "${ENVS[@]}"
|
||||||
common::parse_and_export_env_vars
|
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
|
# shellcheck disable=SC2153 # False positive
|
||||||
common::per_dir_hook "${ARGS[*]}" "$HOOK_ID" "${FILES[@]}"
|
common::per_dir_hook "${ARGS[*]}" "$HOOK_ID" "${FILES[@]}"
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -13,6 +13,8 @@ function main {
|
||||||
common::parse_cmdline "$@"
|
common::parse_cmdline "$@"
|
||||||
common::export_provided_env_vars "${ENVS[@]}"
|
common::export_provided_env_vars "${ENVS[@]}"
|
||||||
common::parse_and_export_env_vars
|
common::parse_and_export_env_vars
|
||||||
|
# JFYI: suppress color for `terraform providers lock` is N/A`
|
||||||
|
|
||||||
# shellcheck disable=SC2153 # False positive
|
# shellcheck disable=SC2153 # False positive
|
||||||
common::per_dir_hook "${ARGS[*]}" "$HOOK_ID" "${FILES[@]}"
|
common::per_dir_hook "${ARGS[*]}" "$HOOK_ID" "${FILES[@]}"
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -16,7 +16,7 @@ function main {
|
||||||
# Support for setting PATH to repo root.
|
# Support for setting PATH to repo root.
|
||||||
# shellcheck disable=SC2178 # It's the simplest syntax for that case
|
# shellcheck disable=SC2178 # It's the simplest syntax for that case
|
||||||
ARGS=${ARGS[*]/__GIT_WORKING_DIR__/$(pwd)\/}
|
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.
|
# Run `tflint --init` for check that plugins installed.
|
||||||
# It should run once on whole repo.
|
# It should run once on whole repo.
|
||||||
|
|
@ -30,7 +30,7 @@ function main {
|
||||||
echo "${TFLINT_INIT}"
|
echo "${TFLINT_INIT}"
|
||||||
return ${exit_code}
|
return ${exit_code}
|
||||||
}
|
}
|
||||||
|
# shellcheck disable=SC2128 # It's the simplest syntax for that case
|
||||||
common::per_dir_hook "$ARGS" "$HOOK_ID" "${FILES[@]}"
|
common::per_dir_hook "$ARGS" "$HOOK_ID" "${FILES[@]}"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -15,6 +15,13 @@ function main {
|
||||||
# Support for setting PATH to repo root.
|
# Support for setting PATH to repo root.
|
||||||
# shellcheck disable=SC2178 # It's the simplest syntax for that case
|
# shellcheck disable=SC2178 # It's the simplest syntax for that case
|
||||||
ARGS=${ARGS[*]/__GIT_WORKING_DIR__/$(pwd)\/}
|
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
|
# shellcheck disable=SC2128 # It's the simplest syntax for that case
|
||||||
common::per_dir_hook "$ARGS" "$HOOK_ID" "${FILES[@]}"
|
common::per_dir_hook "$ARGS" "$HOOK_ID" "${FILES[@]}"
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -15,6 +15,11 @@ function main {
|
||||||
common::parse_cmdline "$@"
|
common::parse_cmdline "$@"
|
||||||
common::export_provided_env_vars "${ENVS[@]}"
|
common::export_provided_env_vars "${ENVS[@]}"
|
||||||
common::parse_and_export_env_vars
|
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
|
# shellcheck disable=SC2153 # False positive
|
||||||
common::per_dir_hook "${ARGS[*]}" "$HOOK_ID" "${FILES[@]}"
|
common::per_dir_hook "${ARGS[*]}" "$HOOK_ID" "${FILES[@]}"
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -12,6 +12,7 @@ function main {
|
||||||
common::parse_cmdline "$@"
|
common::parse_cmdline "$@"
|
||||||
common::export_provided_env_vars "${ENVS[@]}"
|
common::export_provided_env_vars "${ENVS[@]}"
|
||||||
common::parse_and_export_env_vars
|
common::parse_and_export_env_vars
|
||||||
|
# JFYI: suppress color for `hcledit` is N/A`
|
||||||
|
|
||||||
check_dependencies
|
check_dependencies
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -12,6 +12,8 @@ function main {
|
||||||
common::parse_cmdline "$@"
|
common::parse_cmdline "$@"
|
||||||
common::export_provided_env_vars "${ENVS[@]}"
|
common::export_provided_env_vars "${ENVS[@]}"
|
||||||
common::parse_and_export_env_vars
|
common::parse_and_export_env_vars
|
||||||
|
# JFYI: terragrunt hclfmt color already suppressed via PRE_COMMIT_COLOR=never
|
||||||
|
|
||||||
# shellcheck disable=SC2153 # False positive
|
# shellcheck disable=SC2153 # False positive
|
||||||
common::per_dir_hook "${ARGS[*]}" "$HOOK_ID" "${FILES[@]}"
|
common::per_dir_hook "${ARGS[*]}" "$HOOK_ID" "${FILES[@]}"
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -12,6 +12,8 @@ function main {
|
||||||
common::parse_cmdline "$@"
|
common::parse_cmdline "$@"
|
||||||
common::export_provided_env_vars "${ENVS[@]}"
|
common::export_provided_env_vars "${ENVS[@]}"
|
||||||
common::parse_and_export_env_vars
|
common::parse_and_export_env_vars
|
||||||
|
# JFYI: terragrunt validate color already suppressed via PRE_COMMIT_COLOR=never
|
||||||
|
|
||||||
# shellcheck disable=SC2153 # False positive
|
# shellcheck disable=SC2153 # False positive
|
||||||
common::per_dir_hook "${ARGS[*]}" "$HOOK_ID" "${FILES[@]}"
|
common::per_dir_hook "${ARGS[*]}" "$HOOK_ID" "${FILES[@]}"
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -12,6 +12,8 @@ function main {
|
||||||
common::parse_cmdline "$@"
|
common::parse_cmdline "$@"
|
||||||
common::export_provided_env_vars "${ENVS[@]}"
|
common::export_provided_env_vars "${ENVS[@]}"
|
||||||
common::parse_and_export_env_vars
|
common::parse_and_export_env_vars
|
||||||
|
# JFYI: terrascan color already suppressed via PRE_COMMIT_COLOR=never
|
||||||
|
|
||||||
# shellcheck disable=SC2153 # False positive
|
# shellcheck disable=SC2153 # False positive
|
||||||
common::per_dir_hook "${ARGS[*]}" "$HOOK_ID" "${FILES[@]}"
|
common::per_dir_hook "${ARGS[*]}" "$HOOK_ID" "${FILES[@]}"
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -12,6 +12,8 @@ function main {
|
||||||
common::parse_cmdline "$@"
|
common::parse_cmdline "$@"
|
||||||
common::export_provided_env_vars "${ENVS[@]}"
|
common::export_provided_env_vars "${ENVS[@]}"
|
||||||
common::parse_and_export_env_vars
|
common::parse_and_export_env_vars
|
||||||
|
# JFYI: suppress color for `tfupdate` is N/A`
|
||||||
|
|
||||||
# shellcheck disable=SC2153 # False positive
|
# shellcheck disable=SC2153 # False positive
|
||||||
common::per_dir_hook "${ARGS[*]}" "$HOOK_ID" "${FILES[@]}"
|
common::per_dir_hook "${ARGS[*]}" "$HOOK_ID" "${FILES[@]}"
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue