diff --git a/src/gitlab-tofu.sh b/src/gitlab-tofu.sh index aaf5dcf8ecf6d1928f017f88a54256fbe2e78512..dd1b8f84bf65417b4d04f7386776cd58c5ad6adf 100644 --- a/src/gitlab-tofu.sh +++ b/src/gitlab-tofu.sh @@ -16,6 +16,7 @@ # GITLAB_TOFU_PLAN_NAME: the name of the plan cache and json files. Defaults to `plan`. # GITLAB_TOFU_PLAN_CACHE: if set is the full path of the plan cache file. Defaults to `<root>/$GITLAB_TOFU_PLAN_NAME.cache` # GITLAB_TOFU_PLAN_JSON: if set is the full path of the plan json file. Defaults to `<root>/$GITLAB_TOFU_PLAN_NAME.json` +# GITLAB_TOFU_IMPLICIT_INIT: if set to true will perform an implicit tofu init before any command that require it. Defaults to `true`. # # Respected OpenTofu Environment Variables: # > these are variables that are @@ -201,10 +202,10 @@ terraform_authenticate_private_registry() { fi } -# If TF_IMPLICIT_INIT is not set, we set it to `true`. +# If GITLAB_TOFU_IMPLICIT_INIT is not set, we set it to `true`. # If set to `true` it will call `terraform init` prior # to calling the wrapper `terraform` commands. -TF_IMPLICIT_INIT=${TF_IMPLICIT_INIT:-true} +GITLAB_TOFU_IMPLICIT_INIT=${GITLAB_TOFU_IMPLICIT_INIT:-true} # Allows users to continue the actual command in case init failed TF_IGNORE_INIT_ERRORS=${TF_IGNORE_INIT_ERRORS:-false} @@ -236,7 +237,7 @@ if [ $sourced -eq 0 ]; then case "${1}" in "apply") - $TF_IMPLICIT_INIT && terraform_init + $GITLAB_TOFU_IMPLICIT_INIT && terraform_init if [ "$GITLAB_TOFU_APPLY_NO_PLAN" = false ]; then tofu "${tf_chdir_opt}" "${@}" -input=false -auto-approve "${GITLAB_TOFU_PLAN_CACHE}" else @@ -245,7 +246,7 @@ if [ $sourced -eq 0 ]; then fi ;; "destroy") - $TF_IMPLICIT_INIT && terraform_init + $GITLAB_TOFU_IMPLICIT_INIT && terraform_init tofu "${tf_chdir_opt}" "${@}" -auto-approve ;; "fmt") @@ -262,7 +263,7 @@ if [ $sourced -eq 0 ]; then plan_args='-detailed-exitcode' fi - $TF_IMPLICIT_INIT && terraform_init + $GITLAB_TOFU_IMPLICIT_INIT && terraform_init # shellcheck disable=SC2086 tofu "${tf_chdir_opt}" "${@}" -input=false -out="${GITLAB_TOFU_PLAN_CACHE}" ${var_file_args} ${plan_args} && ret=$? || ret=$? @@ -283,17 +284,17 @@ if [ $sourced -eq 0 ]; then tofu "${tf_chdir_opt}" show -json "${GITLAB_TOFU_PLAN_CACHE}" | jq -r "${JQ_PLAN}" > "${GITLAB_TOFU_PLAN_JSON}" ;; "validate") - $TF_IMPLICIT_INIT && terraform_init -backend=false + $GITLAB_TOFU_IMPLICIT_INIT && terraform_init -backend=false # shellcheck disable=SC2086 tofu "${tf_chdir_opt}" "${@}" ${var_file_args} ;; "test") - $TF_IMPLICIT_INIT && terraform_init -backend=false + $GITLAB_TOFU_IMPLICIT_INIT && terraform_init -backend=false # shellcheck disable=SC2086 tofu "${tf_chdir_opt}" "${@}" ${var_file_args} ;; "graph") - $TF_IMPLICIT_INIT && terraform_init + $GITLAB_TOFU_IMPLICIT_INIT && terraform_init # shellcheck disable=SC2086 tofu "${tf_chdir_opt}" "${@}" ${var_file_args} ;; diff --git a/tests/unit/gitlab-tofu.bats b/tests/unit/gitlab-tofu.bats index 9e35b2012daf6f6f63a0d487ba72f083347ee38e..e655ab9faa05712152b1ecc301765042e093c819 100644 --- a/tests/unit/gitlab-tofu.bats +++ b/tests/unit/gitlab-tofu.bats @@ -110,28 +110,28 @@ EOF } @test "gitlab-tofu validate without implicit init" { - export TF_IMPLICIT_INIT=false + export GITLAB_TOFU_IMPLICIT_INIT=false run ! gitlab-tofu validate -no-color assert_output --partial 'This module is not yet installed' } @test "gitlab-tofu plan without implicit init" { - export TF_IMPLICIT_INIT=false + export GITLAB_TOFU_IMPLICIT_INIT=false run ! gitlab-tofu plan -no-color assert_output --partial 'Error: Backend initialization required' } @test "gitlab-tofu apply without implicit init" { - export TF_IMPLICIT_INIT=false + export GITLAB_TOFU_IMPLICIT_INIT=false run ! gitlab-tofu apply -no-color assert_output --partial 'Error: Failed to load ' } @test "gitlab-tofu destroy without implicit init" { - export TF_IMPLICIT_INIT=false + export GITLAB_TOFU_IMPLICIT_INIT=false run ! gitlab-tofu destroy -no-color assert_output --partial 'Error: Backend initialization required'