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: Allow terraform_providers_lock specify terraform init args #406

Merged
merged 2 commits into from
Jul 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,15 @@ Example:

`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

1. `terraform_tflint` supports custom arguments so you can enable module inspection, deep check mode, etc.
Expand Down
39 changes: 39 additions & 0 deletions hooks/_common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ function common::initialize {
# Globals (init and populate):
# ARGS (array) arguments that configure wrapped tool behavior
# HOOK_CONFIG (array) arguments that configure hook behavior
# INIT_ARGS (array) arguments for `terraform init` command
# FILES (array) filenames to check
# Arguments:
# $@ (array) all specified in `hooks.[].args` in
Expand All @@ -28,6 +29,8 @@ function common::parse_cmdline {
# common global arrays.
# Populated via `common::parse_cmdline` and can be used inside hooks' functions
ARGS=() HOOK_CONFIG=() FILES=()
# Used inside `common::terraform_init` function
INIT_ARGS=()

local argv
argv=$(getopt -o a:,h: --long args:,hook-config: -- "$@") || return
Expand All @@ -45,6 +48,11 @@ function common::parse_cmdline {
HOOK_CONFIG+=("$1;")
shift
;;
-i | --init-args)
shift
INIT_ARGS+=("$1")
shift
;;
--)
shift
# shellcheck disable=SC2034 # Variable is used
Expand Down Expand Up @@ -224,3 +232,34 @@ function common::colorify {

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
}
16 changes: 6 additions & 10 deletions hooks/terraform_providers_lock.sh
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,19 @@ function per_dir_hook_unique_part {
local -r args="$1"
local -r dir_path="$2"

if [ ! -d ".terraform" ]; then
init_output=$(terraform init -backend=false 2>&1)
init_code=$?
local exit_code

if [ $init_code -ne 0 ]; then
common::colorify "red" "Init before validation failed: $dir_path"
common::colorify "red" "$init_output"
exit $init_code
fi
fi
common::terraform_init 'terraform providers lock' "$dir_path" || {
exit_code=$?
return $exit_code
}

# pass the arguments to hook
# shellcheck disable=SC2068 # hook fails when quoting is used ("$arg[@]")
terraform providers lock ${args[@]}

# return exit code to common::per_dir_hook
local exit_code=$?
exit_code=$?
return $exit_code
}

Expand Down
16 changes: 3 additions & 13 deletions hooks/terraform_validate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,6 @@ function parse_cmdline_ {
# 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
# Globals:
# INIT_ARGS (array) arguments for `terraform init` command`
# ENVS (array) environment variables that will be used with
# `terraform` commands
# Outputs:
Expand All @@ -103,19 +101,12 @@ function per_dir_hook_unique_part {
local -r dir_path="$2"

local exit_code
local init_output
local validate_output

if [ ! -d .terraform ]; then
init_output=$(terraform init -backend=false "${INIT_ARGS[@]}" 2>&1)
common::terraform_init 'terraform validate' "$dir_path" || {
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
return $exit_code
}

# pass the arguments to hook
# shellcheck disable=SC2068 # hook fails when quoting is used ("$arg[@]")
Expand All @@ -132,7 +123,6 @@ function per_dir_hook_unique_part {
}

# global arrays
declare -a INIT_ARGS
declare -a ENVS

[ "${BASH_SOURCE[0]}" != "$0" ] || main "$@"