From fe5fea976bb0fe1ae472fab23e35869407c21557 Mon Sep 17 00:00:00 2001
From: Timo Furrer <tfurrer@gitlab.com>
Date: Wed, 13 Nov 2024 15:07:33 +0100
Subject: [PATCH] Rename `TF_PLAN_CACHE` to `GITLAB_TOFU_PLAN_CACHE`

Changelog: changed
---
 src/gitlab-tofu.sh          | 15 ++++++++-------
 templates/destroy.yml       |  2 +-
 tests/unit/gitlab-tofu.bats |  8 ++++----
 3 files changed, 13 insertions(+), 12 deletions(-)

diff --git a/src/gitlab-tofu.sh b/src/gitlab-tofu.sh
index c553ecc..9790356 100644
--- a/src/gitlab-tofu.sh
+++ b/src/gitlab-tofu.sh
@@ -14,6 +14,7 @@
 # GITLAB_TOFU_SOURCE: forces this script in source-mode. Required when source auto-detection fails.
 # GITLAB_TOFU_APPLY_NO_PLAN: if set to true, the apply command does not use a plan cache file.
 # 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`
 #
 # Respected OpenTofu Environment Variables:
 # > these are variables that are
@@ -128,9 +129,9 @@ if [ -n "${TF_ROOT}" ]; then
 fi
 
 
-# If TF_PLAN_CACHE is not set then use either the plan.cache file within TF_ROOT if set, or plan.cache in CWD
-if [ -z "${TF_PLAN_CACHE}" ]; then
-  TF_PLAN_CACHE="${default_tf_plan_cache:-${GITLAB_TOFU_PLAN_NAME}.cache}"
+# If GITLAB_TOFU_PLAN_CACHE is not set then use either the plan.cache file within TF_ROOT if set, or plan.cache in CWD
+if [ -z "${GITLAB_TOFU_PLAN_CACHE}" ]; then
+  GITLAB_TOFU_PLAN_CACHE="${default_tf_plan_cache:-${GITLAB_TOFU_PLAN_NAME}.cache}"
 fi
 
 # If TF_PLAN_JSON is not set then use either the plan.json file within TF_ROOT if set, or plan.json in CWD
@@ -234,7 +235,7 @@ if [ $sourced -eq 0 ]; then
     "apply")
       $TF_IMPLICIT_INIT && terraform_init
       if [ "$GITLAB_TOFU_APPLY_NO_PLAN" = false ]; then
-        tofu "${TF_CHDIR_OPT}" "${@}" -input=false -auto-approve "${TF_PLAN_CACHE}"
+        tofu "${TF_CHDIR_OPT}" "${@}" -input=false -auto-approve "${GITLAB_TOFU_PLAN_CACHE}"
       else
         # shellcheck disable=SC2086
         tofu "${TF_CHDIR_OPT}" "${@}" -input=false -auto-approve ${var_file_args}
@@ -260,11 +261,11 @@ if [ $sourced -eq 0 ]; then
 
       $TF_IMPLICIT_INIT && terraform_init
       # shellcheck disable=SC2086
-      tofu "${TF_CHDIR_OPT}" "${@}" -input=false -out="${TF_PLAN_CACHE}" ${var_file_args} ${plan_args} && ret=$? || ret=$?
+      tofu "${TF_CHDIR_OPT}" "${@}" -input=false -out="${GITLAB_TOFU_PLAN_CACHE}" ${var_file_args} ${plan_args} && ret=$? || ret=$?
 
       if [ "${GITLAB_TOFU_PLAN_WITH_JSON}" = 'true' ]; then
         if [ "$ret" -eq 0 ] || [ "$ret" -eq 2 ]; then
-          if ! tofu "${TF_CHDIR_OPT}" show -json "${TF_PLAN_CACHE}" | jq -r "${JQ_PLAN}" > "${TF_PLAN_JSON}"; then
+          if ! tofu "${TF_CHDIR_OPT}" show -json "${GITLAB_TOFU_PLAN_CACHE}" | jq -r "${JQ_PLAN}" > "${TF_PLAN_JSON}"; then
             exit $?
           fi
 
@@ -276,7 +277,7 @@ if [ $sourced -eq 0 ]; then
       exit "$ret"
     ;;
     "plan-json")
-      tofu "${TF_CHDIR_OPT}" show -json "${TF_PLAN_CACHE}" | jq -r "${JQ_PLAN}" > "${TF_PLAN_JSON}"
+      tofu "${TF_CHDIR_OPT}" show -json "${GITLAB_TOFU_PLAN_CACHE}" | jq -r "${JQ_PLAN}" > "${TF_PLAN_JSON}"
     ;;
     "validate")
       $TF_IMPLICIT_INIT && terraform_init -backend=false
diff --git a/templates/destroy.yml b/templates/destroy.yml
index e69b2d4..f1540b2 100644
--- a/templates/destroy.yml
+++ b/templates/destroy.yml
@@ -75,7 +75,7 @@ spec:
       description: 'Whether a plan file should be used.'
     plan_name:
       default: 'destroy-plan'
-      description: 'The name of the plan file to use. Will be used for TF_PLAN_CACHE and TF_PLAN_JSON.'
+      description: 'The name of the plan file to use. Will be used for the cache and json file.'
     var_file:
       default: ''
       type: string
diff --git a/tests/unit/gitlab-tofu.bats b/tests/unit/gitlab-tofu.bats
index 5b00ef1..9e35b20 100644
--- a/tests/unit/gitlab-tofu.bats
+++ b/tests/unit/gitlab-tofu.bats
@@ -82,9 +82,9 @@ EOF
 }
 
 @test "gitlab-tofu plan" {
-  export TF_PLAN_CACHE="test-plan.cache"
+  export GITLAB_TOFU_PLAN_CACHE="test-plan.cache"
   gitlab-tofu plan
-  if [ ! -f "$TF_ROOT/$TF_PLAN_CACHE" ]; then
+  if [ ! -f "$TF_ROOT/$GITLAB_TOFU_PLAN_CACHE" ]; then
     echo "expected to find a plan.cache file"
     exit 1
   fi
@@ -97,13 +97,13 @@ EOF
 }
 
 @test "gitlab-tofu apply" {
-  export TF_PLAN_CACHE="test-plan.cache"
+  export GITLAB_TOFU_PLAN_CACHE="test-plan.cache"
   gitlab-tofu plan
   gitlab-tofu apply
 }
 
 @test "gitlab-tofu destroy" {
-  export TF_PLAN_CACHE="test-plan.cache"
+  export GITLAB_TOFU_PLAN_CACHE="test-plan.cache"
   gitlab-tofu plan
   gitlab-tofu apply
   gitlab-tofu destroy
-- 
GitLab