Skip to content

Commit b12f0c6

Browse files
MaxymVlasovantonbabenko
authored andcommitted
feat: Suppress color for all hooks if PRE_COMMIT_COLOR=never set (antonbabenko#409)
1 parent d490231 commit b12f0c6

15 files changed

+78
-11
lines changed

README.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ If you are using `pre-commit-terraform` already or want to support its developme
3838
* [Hooks usage notes and examples](#hooks-usage-notes-and-examples)
3939
* [All hooks: Usage of environment variables in `--args`](#all-hooks-usage-of-environment-variables-in---args)
4040
* [All hooks: Set env vars inside hook at runtime](#all-hooks-set-env-vars-inside-hook-at-runtime)
41+
* [All hooks: Disable color output](#all-hooks-disable-color-output)
4142
* [checkov (deprecated) and terraform_checkov](#checkov-deprecated-and-terraform_checkov)
4243
* [infracost_breakdown](#infracost_breakdown)
4344
* [terraform_docs](#terraform_docs)
@@ -300,6 +301,16 @@ Config example:
300301
- --envs=AWS_SECRET_ACCESS_KEY="asecretkey"
301302
```
302303

304+
### All hooks: Disable color output
305+
306+
> All, except deprecated hooks: `checkov`, `terraform_docs_replace`
307+
308+
To disable color output for all hooks, set `PRE_COMMIT_COLOR=never` var. Eg:
309+
310+
```bash
311+
PRE_COMMIT_COLOR=never pre-commit run
312+
```
313+
303314
### checkov (deprecated) and terraform_checkov
304315

305316
> `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
422433
* `.projects[].diff.totalHourlyCost` - show the difference in hourly cost for the existing infra and tf plan
423434
* `.projects[].diff.totalMonthlyCost` - show the difference in monthly cost for the existing infra and tf plan
424435
* `.diffTotalHourlyCost` (for Infracost version 0.9.12 or newer) or `[.projects[].diff.totalMonthlyCost | select (.!=null) | tonumber] | add` (for Infracost older than 0.9.12)
425-
* To disable hook color output, set `PRE_COMMIT_COLOR=never` env var.
426436

427437
4. **Docker usage**. In `docker build` or `docker run` command:
428438
* 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`

hooks/_common.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,11 @@ function common::terraform_init {
267267
local exit_code=0
268268
local init_output
269269

270+
# Suppress terraform init color
271+
if [ "$PRE_COMMIT_COLOR" = "never" ]; then
272+
TF_INIT_ARGS+=("-no-color")
273+
fi
274+
270275
if [ ! -d .terraform ]; then
271276
init_output=$(terraform init -backend=false "${TF_INIT_ARGS[@]}" 2>&1)
272277
exit_code=$?

hooks/infracost_breakdown.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ function infracost_breakdown_ {
3535

3636
# Get hook settings
3737
IFS=";" read -r -a checks <<< "$hook_config"
38-
38+
# Suppress infracost color
3939
if [ "$PRE_COMMIT_COLOR" = "never" ]; then
4040
args+=("--no-color")
4141
fi

hooks/terraform_checkov.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ function main {
1515
# Support for setting PATH to repo root.
1616
# shellcheck disable=SC2178 # It's the simplest syntax for that case
1717
ARGS=${ARGS[*]/__GIT_WORKING_DIR__/$(pwd)\/}
18+
# Suppress checkov color
19+
if [ "$PRE_COMMIT_COLOR" = "never" ]; then
20+
export ANSI_COLORS_DISABLED=true
21+
fi
1822
# shellcheck disable=SC2128 # It's the simplest syntax for that case
1923
common::per_dir_hook "$ARGS" "$HOOK_ID" "${FILES[@]}"
2024
}

hooks/terraform_docs.sh

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@
22
set -eo pipefail
33

44
# globals variables
5-
# hook ID, see `- id` for details in .pre-commit-hooks.yaml file
6-
# shellcheck disable=SC2034 # Unused var.
7-
readonly HOOK_ID='terraform_docs'
85
# shellcheck disable=SC2155 # No way to assign to readonly variable in separate lines
96
readonly SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)"
107
# shellcheck source=_common.sh
@@ -90,7 +87,7 @@ function terraform_docs_ {
9087
function terraform_docs {
9188
local -r terraform_docs_awk_file="$1"
9289
local -r hook_config="$2"
93-
local -r args="$3"
90+
local args="$3"
9491
shift 3
9592
local -a -r files=("$@")
9693

@@ -136,10 +133,29 @@ function terraform_docs {
136133
esac
137134
done
138135

139-
#
140136
# Override formatter if no config file set
141-
#
142-
[[ "$args" != *"--config="* ]] && local tf_docs_formatter="md"
137+
if [[ "$args" != *"--config"* ]]; then
138+
local tf_docs_formatter="md"
139+
140+
# Suppress terraform_docs color
141+
else
142+
143+
local config_file=${args#*--config}
144+
config_file=${config_file#*=}
145+
config_file=${config_file% *}
146+
147+
local config_file_no_color
148+
config_file_no_color="$config_file$(date +%s).yml"
149+
150+
if [ "$PRE_COMMIT_COLOR" = "never" ] &&
151+
[[ $(grep -e '^formatter:' "$config_file") == *"pretty"* ]] &&
152+
[[ $(grep ' color: ' "$config_file") != *"false"* ]]; then
153+
154+
cp "$config_file" "$config_file_no_color"
155+
echo -e "settings:\n color: false" >> "$config_file_no_color"
156+
args=${args/$config_file/$config_file_no_color}
157+
fi
158+
fi
143159

144160
local dir_path
145161
for dir_path in $(echo "${paths[*]}" | tr ' ' '\n' | sort -u); do
@@ -212,6 +228,9 @@ function terraform_docs {
212228

213229
popd > /dev/null
214230
done
231+
232+
# Cleanup
233+
[ -e "$config_file_no_color" ] && rm -f "$config_file_no_color"
215234
}
216235

217236
#######################################################################

hooks/terraform_fmt.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ function main {
1212
common::parse_cmdline "$@"
1313
common::export_provided_env_vars "${ENVS[@]}"
1414
common::parse_and_export_env_vars
15+
16+
# Suppress terraform fmt color
17+
if [ "$PRE_COMMIT_COLOR" = "never" ]; then
18+
ARGS+=("-no-color")
19+
fi
20+
1521
# shellcheck disable=SC2153 # False positive
1622
common::per_dir_hook "${ARGS[*]}" "$HOOK_ID" "${FILES[@]}"
1723
}

hooks/terraform_providers_lock.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ function main {
1313
common::parse_cmdline "$@"
1414
common::export_provided_env_vars "${ENVS[@]}"
1515
common::parse_and_export_env_vars
16+
# JFYI: suppress color for `terraform providers lock` is N/A`
17+
1618
# shellcheck disable=SC2153 # False positive
1719
common::per_dir_hook "${ARGS[*]}" "$HOOK_ID" "${FILES[@]}"
1820
}

hooks/terraform_tflint.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ function main {
1616
# Support for setting PATH to repo root.
1717
# shellcheck disable=SC2178 # It's the simplest syntax for that case
1818
ARGS=${ARGS[*]/__GIT_WORKING_DIR__/$(pwd)\/}
19-
# shellcheck disable=SC2128 # It's the simplest syntax for that case
19+
# JFYI: tflint color already suppressed via PRE_COMMIT_COLOR=never
2020

2121
# Run `tflint --init` for check that plugins installed.
2222
# It should run once on whole repo.
@@ -30,7 +30,7 @@ function main {
3030
echo "${TFLINT_INIT}"
3131
return ${exit_code}
3232
}
33-
33+
# shellcheck disable=SC2128 # It's the simplest syntax for that case
3434
common::per_dir_hook "$ARGS" "$HOOK_ID" "${FILES[@]}"
3535
}
3636

hooks/terraform_tfsec.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@ function main {
1515
# Support for setting PATH to repo root.
1616
# shellcheck disable=SC2178 # It's the simplest syntax for that case
1717
ARGS=${ARGS[*]/__GIT_WORKING_DIR__/$(pwd)\/}
18+
19+
# Suppress tfsec color
20+
if [ "$PRE_COMMIT_COLOR" = "never" ]; then
21+
# shellcheck disable=SC2178,SC2128 # It's the simplest syntax for that case
22+
ARGS+=" --no-color"
23+
fi
24+
1825
# shellcheck disable=SC2128 # It's the simplest syntax for that case
1926
common::per_dir_hook "$ARGS" "$HOOK_ID" "${FILES[@]}"
2027
}

hooks/terraform_validate.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ function main {
1515
common::parse_cmdline "$@"
1616
common::export_provided_env_vars "${ENVS[@]}"
1717
common::parse_and_export_env_vars
18+
19+
# Suppress terraform validate color
20+
if [ "$PRE_COMMIT_COLOR" = "never" ]; then
21+
ARGS+=("-no-color")
22+
fi
1823
# shellcheck disable=SC2153 # False positive
1924
common::per_dir_hook "${ARGS[*]}" "$HOOK_ID" "${FILES[@]}"
2025
}

0 commit comments

Comments
 (0)