Skip to content

Commit 32b232f

Browse files
MaxymVlasovantonbabenko
authored andcommitted
feat: Allow terraform_providers_lock specify terraform init args (antonbabenko#406)
1 parent 0f25122 commit 32b232f

File tree

4 files changed

+57
-23
lines changed

4 files changed

+57
-23
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,15 @@ Example:
515515
516516
`terraform_providers_lock` hook will try to reinitialize directories before running the `terraform providers lock` command.
517517

518+
5. `terraform_providers_lock` support passing custom arguments to its `terraform init`:
519+
520+
```yaml
521+
- id: terraform_providers_lock
522+
args:
523+
- --init-args=-upgrade
524+
```
525+
526+
518527
### terraform_tflint
519528

520529
1. `terraform_tflint` supports custom arguments so you can enable module inspection, deep check mode, etc.

hooks/_common.sh

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ function common::initialize {
2525
# Globals (init and populate):
2626
# ARGS (array) arguments that configure wrapped tool behavior
2727
# HOOK_CONFIG (array) arguments that configure hook behavior
28+
# INIT_ARGS (array) arguments for `terraform init` command
2829
# FILES (array) filenames to check
2930
# Arguments:
3031
# $@ (array) all specified in `hooks.[].args` in
@@ -34,6 +35,8 @@ function common::parse_cmdline {
3435
# common global arrays.
3536
# Populated via `common::parse_cmdline` and can be used inside hooks' functions
3637
ARGS=() HOOK_CONFIG=() FILES=()
38+
# Used inside `common::terraform_init` function
39+
INIT_ARGS=()
3740

3841
local argv
3942
argv=$(getopt -o a:,h: --long args:,hook-config: -- "$@") || return
@@ -51,6 +54,11 @@ function common::parse_cmdline {
5154
HOOK_CONFIG+=("$1;")
5255
shift
5356
;;
57+
-i | --init-args)
58+
shift
59+
INIT_ARGS+=("$1")
60+
shift
61+
;;
5462
--)
5563
shift
5664
# shellcheck disable=SC2034 # Variable is used
@@ -230,3 +238,34 @@ function common::colorify {
230238

231239
echo -e "${COLOR}${TEXT}${RESET}"
232240
}
241+
242+
#######################################################################
243+
# Run terraform init command
244+
# Arguments:
245+
# command_name (string) command that will tun after successful init
246+
# dir_path (string) PATH to dir relative to git repo root.
247+
# Can be used in error logging
248+
# Globals (init and populate):
249+
# INIT_ARGS (array) arguments for `terraform init` command
250+
# Outputs:
251+
# If failed - print out terraform init output
252+
#######################################################################
253+
function common::terraform_init {
254+
local -r command_name=$1
255+
local -r dir_path=$2
256+
257+
local exit_code=0
258+
local init_output
259+
260+
if [ ! -d .terraform ]; then
261+
init_output=$(terraform init -backend=false "${INIT_ARGS[@]}" 2>&1)
262+
exit_code=$?
263+
264+
if [ $exit_code -ne 0 ]; then
265+
common::colorify "red" "'terraform init' failed, '$command_name' skipped: $dir_path"
266+
echo -e "$init_output\n\n"
267+
fi
268+
fi
269+
270+
return $exit_code
271+
}

hooks/terraform_providers_lock.sh

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,23 +30,19 @@ function per_dir_hook_unique_part {
3030
local -r args="$1"
3131
local -r dir_path="$2"
3232

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

37-
if [ $init_code -ne 0 ]; then
38-
common::colorify "red" "Init before validation failed: $dir_path"
39-
common::colorify "red" "$init_output"
40-
exit $init_code
41-
fi
42-
fi
35+
common::terraform_init 'terraform providers lock' "$dir_path" || {
36+
exit_code=$?
37+
return $exit_code
38+
}
4339

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

4844
# return exit code to common::per_dir_hook
49-
local exit_code=$?
45+
exit_code=$?
5046
return $exit_code
5147
}
5248

hooks/terraform_validate.sh

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,6 @@ function parse_cmdline_ {
8888
# args (string with array) arguments that configure wrapped tool behavior
8989
# dir_path (string) PATH to dir relative to git repo root.
9090
# Can be used in error logging
91-
# Globals:
92-
# INIT_ARGS (array) arguments for `terraform init` command`
9391
# ENVS (array) environment variables that will be used with
9492
# `terraform` commands
9593
# Outputs:
@@ -100,19 +98,12 @@ function per_dir_hook_unique_part {
10098
local -r dir_path="$2"
10199

102100
local exit_code
103-
local init_output
104101
local validate_output
105102

106-
if [ ! -d .terraform ]; then
107-
init_output=$(terraform init -backend=false "${INIT_ARGS[@]}" 2>&1)
103+
common::terraform_init 'terraform validate' "$dir_path" || {
108104
exit_code=$?
109-
110-
if [ $exit_code -ne 0 ]; then
111-
common::colorify "yellow" "'terraform init' failed, 'terraform validate' skipped: $dir_path"
112-
echo "$init_output"
113-
return $exit_code
114-
fi
115-
fi
105+
return $exit_code
106+
}
116107

117108
# pass the arguments to hook
118109
# shellcheck disable=SC2068 # hook fails when quoting is used ("$arg[@]")
@@ -129,7 +120,6 @@ function per_dir_hook_unique_part {
129120
}
130121

131122
# global arrays
132-
declare -a INIT_ARGS
133123
declare -a ENVS
134124

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

0 commit comments

Comments
 (0)