feat: Allow terraform_providers_lock specify terraform init args (#406)

This commit is contained in:
Maksym Vlasov 2022-07-05 15:25:30 +03:00 committed by Anton Babenko
commit 32b232f039
4 changed files with 57 additions and 23 deletions

View file

@ -515,6 +515,15 @@ Example:
`terraform_providers_lock` hook will try to reinitialize directories before running the `terraform providers lock` command. `terraform_providers_lock` hook will try to reinitialize directories before running the `terraform providers lock` command.
5. `terraform_providers_lock` support passing custom arguments to its `terraform init`:
```yaml
- id: terraform_providers_lock
args:
- --init-args=-upgrade
```
### terraform_tflint ### terraform_tflint
1. `terraform_tflint` supports custom arguments so you can enable module inspection, deep check mode, etc. 1. `terraform_tflint` supports custom arguments so you can enable module inspection, deep check mode, etc.

View file

@ -25,6 +25,7 @@ function common::initialize {
# Globals (init and populate): # Globals (init and populate):
# ARGS (array) arguments that configure wrapped tool behavior # ARGS (array) arguments that configure wrapped tool behavior
# HOOK_CONFIG (array) arguments that configure hook behavior # HOOK_CONFIG (array) arguments that configure hook behavior
# INIT_ARGS (array) arguments for `terraform init` command
# FILES (array) filenames to check # FILES (array) filenames to check
# Arguments: # Arguments:
# $@ (array) all specified in `hooks.[].args` in # $@ (array) all specified in `hooks.[].args` in
@ -34,6 +35,8 @@ function common::parse_cmdline {
# common global arrays. # common global arrays.
# Populated via `common::parse_cmdline` and can be used inside hooks' functions # Populated via `common::parse_cmdline` and can be used inside hooks' functions
ARGS=() HOOK_CONFIG=() FILES=() ARGS=() HOOK_CONFIG=() FILES=()
# Used inside `common::terraform_init` function
INIT_ARGS=()
local argv local argv
argv=$(getopt -o a:,h: --long args:,hook-config: -- "$@") || return argv=$(getopt -o a:,h: --long args:,hook-config: -- "$@") || return
@ -51,6 +54,11 @@ function common::parse_cmdline {
HOOK_CONFIG+=("$1;") HOOK_CONFIG+=("$1;")
shift shift
;; ;;
-i | --init-args)
shift
INIT_ARGS+=("$1")
shift
;;
--) --)
shift shift
# shellcheck disable=SC2034 # Variable is used # shellcheck disable=SC2034 # Variable is used
@ -230,3 +238,34 @@ function common::colorify {
echo -e "${COLOR}${TEXT}${RESET}" echo -e "${COLOR}${TEXT}${RESET}"
} }
#######################################################################
# Run terraform init command
# Arguments:
# command_name (string) command that will tun after successful init
# dir_path (string) PATH to dir relative to git repo root.
# Can be used in error logging
# Globals (init and populate):
# INIT_ARGS (array) arguments for `terraform init` command
# Outputs:
# If failed - print out terraform init output
#######################################################################
function common::terraform_init {
local -r command_name=$1
local -r dir_path=$2
local exit_code=0
local init_output
if [ ! -d .terraform ]; then
init_output=$(terraform init -backend=false "${INIT_ARGS[@]}" 2>&1)
exit_code=$?
if [ $exit_code -ne 0 ]; then
common::colorify "red" "'terraform init' failed, '$command_name' skipped: $dir_path"
echo -e "$init_output\n\n"
fi
fi
return $exit_code
}

View file

@ -30,23 +30,19 @@ function per_dir_hook_unique_part {
local -r args="$1" local -r args="$1"
local -r dir_path="$2" local -r dir_path="$2"
if [ ! -d ".terraform" ]; then local exit_code
init_output=$(terraform init -backend=false 2>&1)
init_code=$?
if [ $init_code -ne 0 ]; then common::terraform_init 'terraform providers lock' "$dir_path" || {
common::colorify "red" "Init before validation failed: $dir_path" exit_code=$?
common::colorify "red" "$init_output" return $exit_code
exit $init_code }
fi
fi
# pass the arguments to hook # pass the arguments to hook
# shellcheck disable=SC2068 # hook fails when quoting is used ("$arg[@]") # shellcheck disable=SC2068 # hook fails when quoting is used ("$arg[@]")
terraform providers lock ${args[@]} terraform providers lock ${args[@]}
# return exit code to common::per_dir_hook # return exit code to common::per_dir_hook
local exit_code=$? exit_code=$?
return $exit_code return $exit_code
} }

View file

@ -88,8 +88,6 @@ function parse_cmdline_ {
# args (string with array) arguments that configure wrapped tool behavior # args (string with array) arguments that configure wrapped tool behavior
# dir_path (string) PATH to dir relative to git repo root. # dir_path (string) PATH to dir relative to git repo root.
# Can be used in error logging # Can be used in error logging
# Globals:
# INIT_ARGS (array) arguments for `terraform init` command`
# ENVS (array) environment variables that will be used with # ENVS (array) environment variables that will be used with
# `terraform` commands # `terraform` commands
# Outputs: # Outputs:
@ -100,19 +98,12 @@ function per_dir_hook_unique_part {
local -r dir_path="$2" local -r dir_path="$2"
local exit_code local exit_code
local init_output
local validate_output local validate_output
if [ ! -d .terraform ]; then common::terraform_init 'terraform validate' "$dir_path" || {
init_output=$(terraform init -backend=false "${INIT_ARGS[@]}" 2>&1)
exit_code=$? exit_code=$?
return $exit_code
if [ $exit_code -ne 0 ]; then }
common::colorify "yellow" "'terraform init' failed, 'terraform validate' skipped: $dir_path"
echo "$init_output"
return $exit_code
fi
fi
# pass the arguments to hook # pass the arguments to hook
# shellcheck disable=SC2068 # hook fails when quoting is used ("$arg[@]") # shellcheck disable=SC2068 # hook fails when quoting is used ("$arg[@]")
@ -129,7 +120,6 @@ function per_dir_hook_unique_part {
} }
# global arrays # global arrays
declare -a INIT_ARGS
declare -a ENVS declare -a ENVS
[ "${BASH_SOURCE[0]}" != "$0" ] || main "$@" [ "${BASH_SOURCE[0]}" != "$0" ] || main "$@"