From 05a987a8a3d94a7286819fbe2e8f908029fe8f9a Mon Sep 17 00:00:00 2001
From: Timo Furrer <tfurrer@gitlab.com>
Date: Wed, 13 Nov 2024 09:57:39 +0100
Subject: [PATCH] Support env variable to force sourcing `gitlab-tofu`

Changelog: added
---
 .gitlab/README.md.template | 20 ++++++++++++++++++++
 README.md                  | 20 ++++++++++++++++++++
 src/gitlab-tofu.sh         |  6 ++++--
 3 files changed, 44 insertions(+), 2 deletions(-)

diff --git a/.gitlab/README.md.template b/.gitlab/README.md.template
index 28a0c49..f173729 100644
--- a/.gitlab/README.md.template
+++ b/.gitlab/README.md.template
@@ -280,6 +280,26 @@ plan:
     - apk add jq
 ```
 
+### Source `gitlab-tofu` script to run custom commands later
+
+The `gitlab-tofu` script can be sourced instead of executed.
+This allows you to setup the shell and run your own `tofu` or
+`gitlab-tofu` commands.
+
+You can use the following snipped either in your script,
+directly in the shell and also in `script` keywords of your
+pipeline job:
+
+```shell
+. $(which gitlab-tofu)
+```
+
+There is a slight chance when doing this in a more exotic environment
+or shell that `gitlab-tofu` is not able to detect that it is sourced
+and will try to execute a `tofu` command.
+In this case you can set the `GITLAB_TOFU_SOURCE` environment variable
+to `true` before sourcing `gitlab-tofu`.
+
 ### Best Practices
 
 This section is a collection of *some* best practices.
diff --git a/README.md b/README.md
index 06023aa..c2e88f4 100644
--- a/README.md
+++ b/README.md
@@ -295,6 +295,26 @@ plan:
     - apk add jq
 ```
 
+### Source `gitlab-tofu` script to run custom commands later
+
+The `gitlab-tofu` script can be sourced instead of executed.
+This allows you to setup the shell and run your own `tofu` or
+`gitlab-tofu` commands.
+
+You can use the following snipped either in your script,
+directly in the shell and also in `script` keywords of your
+pipeline job:
+
+```shell
+. $(which gitlab-tofu)
+```
+
+There is a slight chance when doing this in a more exotic environment
+or shell that `gitlab-tofu` is not able to detect that it is sourced
+and will try to execute a `tofu` command.
+In this case you can set the `GITLAB_TOFU_SOURCE` environment variable
+to `true` before sourcing `gitlab-tofu`.
+
 ### Best Practices
 
 This section is a collection of *some* best practices.
diff --git a/src/gitlab-tofu.sh b/src/gitlab-tofu.sh
index 5ffb0a1..af20851 100644
--- a/src/gitlab-tofu.sh
+++ b/src/gitlab-tofu.sh
@@ -2,7 +2,7 @@
 
 # set some shell options
 set -o errexit
- 
+
 if [ "${DEBUG_OUTPUT}" = "true" ]; then
     set -o xtrace
 fi
@@ -24,7 +24,9 @@ fi
 # Evaluate if this script is being sourced or executed directly.
 # See https://stackoverflow.com/a/28776166
 sourced=0
-if [ -n "$ZSH_VERSION" ]; then
+if [ "$GITLAB_TOFU_SOURCE" = 'true' ]; then
+  sourced=1
+elif [ -n "$ZSH_VERSION" ]; then
   case $ZSH_EVAL_CONTEXT in *:file) sourced=1;; esac
 elif [ -n "$KSH_VERSION" ]; then
   # shellcheck disable=SC2296
-- 
GitLab