Skip to content

Commit

Permalink
Simple base
Browse files Browse the repository at this point in the history
  • Loading branch information
spouzols committed Feb 24, 2020
1 parent 83173c4 commit e5cd715
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 0 deletions.
22 changes: 22 additions & 0 deletions README.md
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.
64 changes: 64 additions & 0 deletions tfw
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[@]}"}

0 comments on commit e5cd715

Please sign in to comment.