From e99563cd5c6e37836cc77f7589f2f9e4699f2177 Mon Sep 17 00:00:00 2001
From: Adar Nimrod <nimrod@shore.co.il>
Date: Tue, 14 Sep 2021 13:31:34 +0300
Subject: [PATCH] Include <workspace>/*.tfvars files with tf.

The tf wrapper adds found tfvars files under a directory with the
workspace name. So you can have multiple files with nicer separation
inside a directory if you need it (also symlinks so you can keep things
DRY). I'm a little conflicted on this change because it makes the script
more complex and I like pointing out that the wrapper is just ~20 lines
of simple shell scripting. Finally, the invocation will fail with
filenames that have spaces in them.
---
 Documents/bin/tf | 15 +++++++++++++--
 1 file changed, 13 insertions(+), 2 deletions(-)

diff --git a/Documents/bin/tf b/Documents/bin/tf
index 9f3de6e..426473e 100755
--- a/Documents/bin/tf
+++ b/Documents/bin/tf
@@ -5,7 +5,7 @@ set -eu
 # the workspace name (on commands that support that).
 
 workspace="$(terraform workspace show)"
-if [ ! -f "$workspace.tfvars" ]
+if [ ! -f "$workspace.tfvars" ] && [ ! -d "$workspace" ]
 then
     exec terraform "$@"
 fi
@@ -15,7 +15,18 @@ do
     if [ "$arg" = "plan" ] || [ "$arg" = "console" ] || [ "$arg" = "import" ] ||
         [ "$arg" = "refresh" ]
     then
+        additional_args=''
+        if [ -f "$workspace.tfvars" ]
+        then
+            additional_args="$additional_args -var-files=$workspace.tfvars"
+        fi
+        if [ -d "$workspace" ]
+        then
+            additional_args="$additional_args $(find "$workspace" -type f -name '*.tfvars' -exec printf '-var-file=%s ' {} \;)"
+        fi
         exec terraform "$@" "-var-file=$workspace.tfvars"
     fi
 done
-exec terraform "$@"
+
+# shellcheck disable=SC2086
+exec terraform "$@" $additional_args
-- 
GitLab