From 0baa04e599ae60980a77e4e1d4bbdc8956539b35 Mon Sep 17 00:00:00 2001
From: Adar Nimrod <nimrod@shore.co.il>
Date: Thu, 26 Dec 2019 19:04:25 +0200
Subject: [PATCH] Validate Terraform according to workspace.

Use the variable file matching the workspace when validating. Also when
validating and formatting, show the diff.
---
 .bashrc | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)

diff --git a/.bashrc b/.bashrc
index 31675f1..56fc58a 100644
--- a/.bashrc
+++ b/.bashrc
@@ -123,7 +123,7 @@ alias listen_tcp='nc -vlk 0.0.0.0'
 alias listen_udp='nc -uvlk 0.0.0.0'
 alias listen_unix='nc -Uvlk'
 alias tfa='terraform apply tfplan'
-alias tfvf='terraform validate && terraform fmt'
+alias tfvf='tfv && terraform fmt -diff'
 
 if ! command -v notify-send > /dev/null
 then
@@ -174,6 +174,16 @@ tfi () {
     fi
 }
 
+tfv () {
+    workspace="$(terraform workspace show)"
+    if [ "$workspace" = "default" ] || [ ! -f "$workspace.tfvars" ]
+    then
+        terraform validate "$@"
+    else
+        terraform validate -var-file "$workspace.tfvars" "$@"
+    fi
+}
+
 genpass () {
     bytes="${1:-32}"
     head --bytes="$bytes" /dev/urandom | base64 --wrap=0
-- 
GitLab