Skip to content

Commit

Permalink
fix: Add --env-vars, deprecate --envs (antonbabenko#410)
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxymVlasov authored and antonbabenko committed Jul 12, 2022
1 parent b12f0c6 commit 2b35cad
Show file tree
Hide file tree
Showing 15 changed files with 24 additions and 22 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -296,9 +296,9 @@ Config example:
```yaml
- id: terraform_validate
args:
- --envs=AWS_DEFAULT_REGION="us-west-2"
- --envs=AWS_ACCESS_KEY_ID="anaccesskey"
- --envs=AWS_SECRET_ACCESS_KEY="asecretkey"
- --env-vars=AWS_DEFAULT_REGION="us-west-2"
- --env-vars=AWS_ACCESS_KEY_ID="anaccesskey"
- --env-vars=AWS_SECRET_ACCESS_KEY="asecretkey"
```

### All hooks: Disable color output
Expand Down
12 changes: 7 additions & 5 deletions hooks/_common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ function common::initialize {
# ARGS (array) arguments that configure wrapped tool behavior
# HOOK_CONFIG (array) arguments that configure hook behavior
# TF_INIT_ARGS (array) arguments for `terraform init` command
# ENVS (array) environment variables will be available
# ENV_VARS (array) environment variables will be available
# for all 3rd-party tools executed by a hook.
# FILES (array) filenames to check
# Arguments:
Expand All @@ -40,10 +40,11 @@ function common::parse_cmdline {
# Used inside `common::terraform_init` function
TF_INIT_ARGS=()
# Used inside `common::export_provided_env_vars` function
ENVS=()
ENV_VARS=()

local argv
argv=$(getopt -o a:,h:,i:,e: --long args:,hook-config:,init-args:,tf-init-args:,envs: -- "$@") || return
# TODO: Planned breaking change: remove `init-args`, `envs` as not self-descriptive
argv=$(getopt -o a:,h:,i:,e: --long args:,hook-config:,init-args:,tf-init-args:,envs:,env-vars: -- "$@") || return
eval "set -- $argv"

for argv; do
Expand All @@ -64,9 +65,10 @@ function common::parse_cmdline {
TF_INIT_ARGS+=("$1")
shift
;;
-e | --envs)
# TODO: Planned breaking change: remove `--envs` as not self-descriptive
-e | --envs | --env-vars)
shift
ENVS+=("$1")
ENV_VARS+=("$1")
shift
;;
--)
Expand Down
2 changes: 1 addition & 1 deletion hooks/infracost_breakdown.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ readonly SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)"
function main {
common::initialize "$SCRIPT_DIR"
common::parse_cmdline "$@"
common::export_provided_env_vars "${ENVS[@]}"
common::export_provided_env_vars "${ENV_VARS[@]}"
common::parse_and_export_env_vars
# shellcheck disable=SC2153 # False positive
infracost_breakdown_ "${HOOK_CONFIG[*]}" "${ARGS[*]}"
Expand Down
2 changes: 1 addition & 1 deletion hooks/terraform_checkov.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ readonly SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)"
function main {
common::initialize "$SCRIPT_DIR"
common::parse_cmdline "$@"
common::export_provided_env_vars "${ENVS[@]}"
common::export_provided_env_vars "${ENV_VARS[@]}"
common::parse_and_export_env_vars
# Support for setting PATH to repo root.
# shellcheck disable=SC2178 # It's the simplest syntax for that case
Expand Down
2 changes: 1 addition & 1 deletion hooks/terraform_docs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ readonly SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)"
function main {
common::initialize "$SCRIPT_DIR"
common::parse_cmdline "$@"
common::export_provided_env_vars "${ENVS[@]}"
common::export_provided_env_vars "${ENV_VARS[@]}"
common::parse_and_export_env_vars
# Support for setting relative PATH to .terraform-docs.yml config.
# shellcheck disable=SC2178 # It's the simplest syntax for that case
Expand Down
2 changes: 1 addition & 1 deletion hooks/terraform_fmt.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ readonly SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)"
function main {
common::initialize "$SCRIPT_DIR"
common::parse_cmdline "$@"
common::export_provided_env_vars "${ENVS[@]}"
common::export_provided_env_vars "${ENV_VARS[@]}"
common::parse_and_export_env_vars

# Suppress terraform fmt color
Expand Down
2 changes: 1 addition & 1 deletion hooks/terraform_providers_lock.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ readonly SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)"
function main {
common::initialize "$SCRIPT_DIR"
common::parse_cmdline "$@"
common::export_provided_env_vars "${ENVS[@]}"
common::export_provided_env_vars "${ENV_VARS[@]}"
common::parse_and_export_env_vars
# JFYI: suppress color for `terraform providers lock` is N/A`

Expand Down
2 changes: 1 addition & 1 deletion hooks/terraform_tflint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ readonly SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)"
function main {
common::initialize "$SCRIPT_DIR"
common::parse_cmdline "$@"
common::export_provided_env_vars "${ENVS[@]}"
common::export_provided_env_vars "${ENV_VARS[@]}"
common::parse_and_export_env_vars
# Support for setting PATH to repo root.
# shellcheck disable=SC2178 # It's the simplest syntax for that case
Expand Down
2 changes: 1 addition & 1 deletion hooks/terraform_tfsec.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ readonly SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)"
function main {
common::initialize "$SCRIPT_DIR"
common::parse_cmdline "$@"
common::export_provided_env_vars "${ENVS[@]}"
common::export_provided_env_vars "${ENV_VARS[@]}"
common::parse_and_export_env_vars
# Support for setting PATH to repo root.
# shellcheck disable=SC2178 # It's the simplest syntax for that case
Expand Down
4 changes: 2 additions & 2 deletions hooks/terraform_validate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export AWS_DEFAULT_REGION=${AWS_DEFAULT_REGION:-us-east-1}
function main {
common::initialize "$SCRIPT_DIR"
common::parse_cmdline "$@"
common::export_provided_env_vars "${ENVS[@]}"
common::export_provided_env_vars "${ENV_VARS[@]}"
common::parse_and_export_env_vars

# Suppress terraform validate color
Expand All @@ -34,7 +34,7 @@ function main {
# args (string with array) arguments that configure wrapped tool behavior
# dir_path (string) PATH to dir relative to git repo root.
# Can be used in error logging
# ENVS (array) environment variables that will be used with
# ENV_VARS (array) environment variables that will be used with
# `terraform` commands
# Outputs:
# If failed - print out hook checks status
Expand Down
2 changes: 1 addition & 1 deletion hooks/terraform_wrapper_module_for_each.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ readonly SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)"
function main {
common::initialize "$SCRIPT_DIR"
common::parse_cmdline "$@"
common::export_provided_env_vars "${ENVS[@]}"
common::export_provided_env_vars "${ENV_VARS[@]}"
common::parse_and_export_env_vars
# JFYI: suppress color for `hcledit` is N/A`

Expand Down
2 changes: 1 addition & 1 deletion hooks/terragrunt_fmt.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ readonly SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)"
function main {
common::initialize "$SCRIPT_DIR"
common::parse_cmdline "$@"
common::export_provided_env_vars "${ENVS[@]}"
common::export_provided_env_vars "${ENV_VARS[@]}"
common::parse_and_export_env_vars
# JFYI: terragrunt hclfmt color already suppressed via PRE_COMMIT_COLOR=never

Expand Down
2 changes: 1 addition & 1 deletion hooks/terragrunt_validate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ readonly SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)"
function main {
common::initialize "$SCRIPT_DIR"
common::parse_cmdline "$@"
common::export_provided_env_vars "${ENVS[@]}"
common::export_provided_env_vars "${ENV_VARS[@]}"
common::parse_and_export_env_vars
# JFYI: terragrunt validate color already suppressed via PRE_COMMIT_COLOR=never

Expand Down
2 changes: 1 addition & 1 deletion hooks/terrascan.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ readonly SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)"
function main {
common::initialize "$SCRIPT_DIR"
common::parse_cmdline "$@"
common::export_provided_env_vars "${ENVS[@]}"
common::export_provided_env_vars "${ENV_VARS[@]}"
common::parse_and_export_env_vars
# JFYI: terrascan color already suppressed via PRE_COMMIT_COLOR=never

Expand Down
2 changes: 1 addition & 1 deletion hooks/tfupdate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ readonly SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)"
function main {
common::initialize "$SCRIPT_DIR"
common::parse_cmdline "$@"
common::export_provided_env_vars "${ENVS[@]}"
common::export_provided_env_vars "${ENV_VARS[@]}"
common::parse_and_export_env_vars
# JFYI: suppress color for `tfupdate` is N/A`

Expand Down

0 comments on commit 2b35cad

Please sign in to comment.