Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 10 commits into from
Jul 6, 2022
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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](#all-hooks-disable-color)
MaxymVlasov marked this conversation as resolved.
Show resolved Hide resolved
* [checkov (deprecated) and terraform_checkov](#checkov-deprecated-and-terraform_checkov)
* [infracost_breakdown](#infracost_breakdown)
* [terraform_docs](#terraform_docs)
Expand Down Expand Up @@ -300,6 +301,16 @@ Config example:
- --envs=AWS_SECRET_ACCESS_KEY="asecretkey"
```

### All hooks: Disable color
MaxymVlasov marked this conversation as resolved.
Show resolved Hide resolved

> 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`.
Expand Down Expand Up @@ -422,7 +433,7 @@ 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.

MaxymVlasov marked this conversation as resolved.
Show resolved Hide resolved

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`
Expand Down
5 changes: 5 additions & 0 deletions hooks/_common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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=$?
Expand Down
2 changes: 1 addition & 1 deletion hooks/infracost_breakdown.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
yermulnik marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
4 changes: 4 additions & 0 deletions hooks/terraform_checkov.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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[@]}"
}
Expand Down
34 changes: 27 additions & 7 deletions hooks/terraform_docs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
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'

MaxymVlasov marked this conversation as resolved.
Show resolved Hide resolved
# 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
Expand Down Expand Up @@ -90,7 +88,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=("$@")

Expand Down Expand Up @@ -136,10 +134,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
Expand Down Expand Up @@ -212,6 +229,9 @@ function terraform_docs {

popd > /dev/null
done

# Cleanup
rm -f "$config_file_no_color"
MaxymVlasov marked this conversation as resolved.
Show resolved Hide resolved
}

#######################################################################
Expand Down
6 changes: 6 additions & 0 deletions hooks/terraform_fmt.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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[@]}"
}
Expand Down
2 changes: 2 additions & 0 deletions hooks/terraform_providers_lock.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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[@]}"
}
Expand Down
4 changes: 2 additions & 2 deletions hooks/terraform_tflint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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[@]}"
}

Expand Down
7 changes: 7 additions & 0 deletions hooks/terraform_tfsec.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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="${ARGS} --no-color"
MaxymVlasov marked this conversation as resolved.
Show resolved Hide resolved
fi

# shellcheck disable=SC2128 # It's the simplest syntax for that case
common::per_dir_hook "$ARGS" "$HOOK_ID" "${FILES[@]}"
}
Expand Down
5 changes: 5 additions & 0 deletions hooks/terraform_validate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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[@]}"
}
Expand Down
1 change: 1 addition & 0 deletions hooks/terraform_wrapper_module_for_each.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 2 additions & 0 deletions hooks/terragrunt_fmt.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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[@]}"
}
Expand Down
2 changes: 2 additions & 0 deletions hooks/terragrunt_validate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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[@]}"
}
Expand Down
2 changes: 2 additions & 0 deletions hooks/terrascan.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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[@]}"
}
Expand Down
2 changes: 2 additions & 0 deletions hooks/tfupdate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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[@]}"
}
Expand Down