From 96447edabcbf0c75bee61d8c837013b6ea24c8be Mon Sep 17 00:00:00 2001 From: MaxymVlasov Date: Mon, 4 Jul 2022 21:26:53 +0300 Subject: [PATCH 1/4] feat: Allow `terraform_providers_lock` specify `terraform init` args --- README.md | 9 +++++++ hooks/_common.sh | 39 +++++++++++++++++++++++++++++++ hooks/terraform_providers_lock.sh | 16 +++++-------- hooks/terraform_validate.sh | 16 +++---------- 4 files changed, 57 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index 416533aa4..b0a10bad1 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/hooks/_common.sh b/hooks/_common.sh index 09debf5da..98f6ea494 100644 --- a/hooks/_common.sh +++ b/hooks/_common.sh @@ -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 @@ -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 @@ -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 @@ -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 command_name=$1 + local 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 +} diff --git a/hooks/terraform_providers_lock.sh b/hooks/terraform_providers_lock.sh index 9b02800ba..f8a700c24 100755 --- a/hooks/terraform_providers_lock.sh +++ b/hooks/terraform_providers_lock.sh @@ -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 } diff --git a/hooks/terraform_validate.sh b/hooks/terraform_validate.sh index 78a0da8f8..3dc2ac759 100755 --- a/hooks/terraform_validate.sh +++ b/hooks/terraform_validate.sh @@ -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: @@ -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[@]") @@ -132,7 +123,6 @@ function per_dir_hook_unique_part { } # global arrays -declare -a INIT_ARGS declare -a ENVS [ "${BASH_SOURCE[0]}" != "$0" ] || main "$@" From d7f695b759e87b91aef835868b356fd4b377f490 Mon Sep 17 00:00:00 2001 From: MaxymVlasov Date: Mon, 4 Jul 2022 21:53:20 +0300 Subject: [PATCH 2/4] fix: Deprecate `--init-args` as not self-descriptive, add `--tf-init-args` in `terraform_validate` --- README.md | 4 ++-- hooks/_common.sh | 13 +++++++------ hooks/terraform_validate.sh | 7 ++++--- 3 files changed, 13 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index b0a10bad1..6037c82ce 100644 --- a/README.md +++ b/README.md @@ -520,7 +520,7 @@ Example: ```yaml - id: terraform_providers_lock args: - - --init-args=-upgrade + - --tf-init-args=-upgrade ``` @@ -629,7 +629,7 @@ Example: ```yaml - id: terraform_validate args: - - --init-args=-lockfile=readonly + - --tf-init-args=-lockfile=readonly ``` 4. It may happen that Terraform working directory (`.terraform`) already exists but not in the best condition (eg, not initialized modules, wrong version of Terraform, etc.). To solve this problem, you can find and delete all `.terraform` directories in your repository: diff --git a/hooks/_common.sh b/hooks/_common.sh index 98f6ea494..fc664d759 100644 --- a/hooks/_common.sh +++ b/hooks/_common.sh @@ -19,7 +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 +# TF_INIT_ARGS (array) arguments for `terraform init` command # FILES (array) filenames to check # Arguments: # $@ (array) all specified in `hooks.[].args` in @@ -30,7 +30,7 @@ function common::parse_cmdline { # Populated via `common::parse_cmdline` and can be used inside hooks' functions ARGS=() HOOK_CONFIG=() FILES=() # Used inside `common::terraform_init` function - INIT_ARGS=() + TF_INIT_ARGS=() local argv argv=$(getopt -o a:,h: --long args:,hook-config: -- "$@") || return @@ -48,9 +48,10 @@ function common::parse_cmdline { HOOK_CONFIG+=("$1;") shift ;; - -i | --init-args) + # TODO: Planned breaking change: remove `--init-args` as not self-descriptive + -i | --init-args | --tf-init-args) shift - INIT_ARGS+=("$1") + TF_INIT_ARGS+=("$1") shift ;; --) @@ -240,7 +241,7 @@ function common::colorify { # 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 +# TF_INIT_ARGS (array) arguments for `terraform init` command # Outputs: # If failed - print out terraform init output ####################################################################### @@ -252,7 +253,7 @@ function common::terraform_init { local init_output if [ ! -d .terraform ]; then - init_output=$(terraform init -backend=false "${INIT_ARGS[@]}" 2>&1) + init_output=$(terraform init -backend=false "${TF_INIT_ARGS[@]}" 2>&1) exit_code=$? if [ $exit_code -ne 0 ]; then diff --git a/hooks/terraform_validate.sh b/hooks/terraform_validate.sh index 3dc2ac759..55a4703ce 100755 --- a/hooks/terraform_validate.sh +++ b/hooks/terraform_validate.sh @@ -37,7 +37,7 @@ function main { # Globals (init and populate): # ARGS (array) arguments that configure wrapped tool behavior # HOOK_CONFIG (array) arguments that configure hook behavior -# INIT_ARGS (array) arguments to `terraform init` command +# TF_INIT_ARGS (array) arguments to `terraform init` command # ENVS (array) environment variables that will be used with # `terraform` commands # FILES (array) filenames to check @@ -62,9 +62,10 @@ function parse_cmdline_ { HOOK_CONFIG+=("$1;") shift ;; - -i | --init-args) + # TODO: Planned breaking change: remove `--init-args` as not self-descriptive + -i | --init-args | --tf-init-args) shift - INIT_ARGS+=("$1") + TF_INIT_ARGS+=("$1") shift ;; -e | --envs) From ae13e3fba7c11ac67e67d4fbe002a6ef87447618 Mon Sep 17 00:00:00 2001 From: Maksym Vlasov Date: Tue, 5 Jul 2022 15:28:11 +0300 Subject: [PATCH 3/4] Apply suggestions from code review --- hooks/_common.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/hooks/_common.sh b/hooks/_common.sh index 87e4f0fdc..19c51b44d 100644 --- a/hooks/_common.sh +++ b/hooks/_common.sh @@ -252,8 +252,8 @@ function common::colorify { # If failed - print out terraform init output ####################################################################### function common::terraform_init { - local command_name=$1 - local dir_path=$2 + local -r command_name=$1 + local -r dir_path=$2 local exit_code=0 local init_output From 73b90ea5d7717c0f0b4c35e4c44fa3c1fd4ab3ae Mon Sep 17 00:00:00 2001 From: MaxymVlasov Date: Tue, 5 Jul 2022 15:43:33 +0300 Subject: [PATCH 4/4] fix parse_cmdline --- hooks/_common.sh | 2 +- hooks/terraform_validate.sh | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/hooks/_common.sh b/hooks/_common.sh index 19c51b44d..1dcd82d61 100644 --- a/hooks/_common.sh +++ b/hooks/_common.sh @@ -39,7 +39,7 @@ function common::parse_cmdline { TF_INIT_ARGS=() local argv - argv=$(getopt -o a:,h: --long args:,hook-config: -- "$@") || return + argv=$(getopt -o a:,h:,i: --long args:,hook-config:,init-args:,tf-init-args: -- "$@") || return eval "set -- $argv" for argv; do diff --git a/hooks/terraform_validate.sh b/hooks/terraform_validate.sh index 37ee3b80d..8e1eff08b 100755 --- a/hooks/terraform_validate.sh +++ b/hooks/terraform_validate.sh @@ -44,7 +44,7 @@ function main { ####################################################################### function parse_cmdline_ { declare argv - argv=$(getopt -o e:i:a: --long envs:,init-args:,args: -- "$@") || return + argv=$(getopt -o e:i:a:h: --long envs:,tf-init-args:,init-args:,args: -- "$@") || return eval "set -- $argv" for argv; do