From b875dbda0eccc5807550791c9dc8741d4bc9e4fb Mon Sep 17 00:00:00 2001
From: Adar Nimrod <nimrod@shore.co.il>
Date: Mon, 26 Feb 2024 10:00:52 +0200
Subject: [PATCH] Add the tf-next function.

To easily iterate and apply the same changes to multiple workspaces.
---
 .bashrc.d/terraform | 14 ++++++++++++++
 1 file changed, 14 insertions(+)

diff --git a/.bashrc.d/terraform b/.bashrc.d/terraform
index 70ebaee..31eec50 100644
--- a/.bashrc.d/terraform
+++ b/.bashrc.d/terraform
@@ -9,3 +9,17 @@ alias tfar='tf apply tfplan && tf plan -out tfplan -refresh-only && tf apply tfp
 alias tfp='tf init -backend=false && tf plan -out tfplan'
 alias tfr='tf apply -auto-approve -refresh-only'
 alias tfvf='tf init -backend=false && tf validate && tf fmt -diff'
+
+tfp-next () {
+    current="$(tf workspace show)"
+    next="$(tf workspace list | cut -c3- | grep --line-regexp --after-context=1 "$current" | tail +2)"
+    tf apply tfplan
+    tf apply -auto-approve -refresh-only
+    if [ -z "$next" ]
+    then
+        red "Last workspace in the cycle."
+        return
+    fi
+    tf workspace select "$next"
+    tf plan -out tfplan "$@"
+}
-- 
GitLab