-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
86 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
Minimalist Terraform wrapper to help with Terraform version and variables per workspace. | ||
|
||
# Requirements | ||
|
||
OSX, bash compatible shell, [tfenv](https://github.com/tfutils/tfenv). | ||
|
||
# Install | ||
|
||
Checkout this repo, give the `tfw` script `+x` permission and add the directory to your `PATH` variable | ||
or add a symbolic link for example in your `/usr/local/bin`: | ||
|
||
```sh | ||
chmod +x tfw | ||
ln -s `pwd`/tfw /usr/local/bin/tfw | ||
``` | ||
|
||
# Use | ||
|
||
Create a `workspaces/<workspace>.tfvars` file for your workspaces in your root module. | ||
|
||
Use `tfw` instead of `terraform`. It runs `tfenv use min-required` first then will forward command to `terraform` | ||
while adding a `-var-file=workspaces/<workspace>.tfvars` option when needed. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
#!/usr/bin/env bash | ||
|
||
set -Eeuo pipefail | ||
|
||
TFENV_CMD=tfenv | ||
TF_CMD=terraform | ||
TERRAFORM_WORKSPACE_FILE=.terraform/environment | ||
TERRAFORM_WORKSPACE_VARS= | ||
TFW_WORKSPACES_DIR=workspaces | ||
|
||
haveVars=0 | ||
|
||
if [ "$($TFENV_CMD 2>&1 | head -c 5)" != "tfenv" ]; then | ||
(>&2 echo "Could not confirm tfenv is available.") | ||
(>&2 echo "Please install tfenv and make sure to have it in your PATH environment variable.") | ||
exit 1 | ||
fi | ||
|
||
$TFENV_CMD use min-required | ||
|
||
command= | ||
newArgs=() | ||
|
||
if [[ $# -gt 0 ]]; then | ||
command="$1" | ||
case $command in | ||
plan|apply|destroy|console|refresh|import) | ||
haveVars=1 | ||
;; | ||
*) | ||
newArgs+=( "$@" ) | ||
;; | ||
esac | ||
fi | ||
|
||
if [ -f $TERRAFORM_WORKSPACE_FILE ]; then | ||
TERRAFORM_WORKSPACE=$(cat $TERRAFORM_WORKSPACE_FILE) | ||
echo "Workspace: $TERRAFORM_WORKSPACE" | ||
if [[ haveVars -eq 1 ]]; then | ||
TERRAFORM_WORKSPACE_VARS_FILE="$TFW_WORKSPACES_DIR/$TERRAFORM_WORKSPACE.tfvars" | ||
if [ -f "$TERRAFORM_WORKSPACE_VARS_FILE" ]; then | ||
TERRAFORM_WORKSPACE_VARS="-var-file=$TERRAFORM_WORKSPACE_VARS_FILE" | ||
else | ||
(>&2 echo "ERROR: No variables file found: $TERRAFORM_WORKSPACE_VARS_FILE") | ||
exit 3 | ||
fi | ||
newArgs+=( "$command" ) | ||
if [ "$TERRAFORM_WORKSPACE_VARS" != "" ]; then | ||
newArgs+=( "$TERRAFORM_WORKSPACE_VARS" ) | ||
fi | ||
newArgs+=( "${@:2}" ) | ||
fi | ||
else | ||
if [[ haveVars -eq 1 ]]; then | ||
(>&2 echo "No workspace detected.") | ||
exit 2 | ||
fi | ||
fi | ||
|
||
echo "" | ||
echo ">>" "$TF_CMD" ${newArgs[@]+"${newArgs[@]}"} | ||
echo "" | ||
|
||
$TF_CMD ${newArgs[@]+"${newArgs[@]}"} |