Skip to content

Instantly share code, notes, and snippets.

@johnl
Last active August 4, 2020 09:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johnl/b87d53ef738fb76b79298a3435bdf6fb to your computer and use it in GitHub Desktop.
Save johnl/b87d53ef738fb76b79298a3435bdf6fb to your computer and use it in GitHub Desktop.
a script to run the right version of terraform for the current repository
#!/bin/bash
# this script expects to find each terraform version binary available in the path
# with the filename format terraform_0.x.x
#
# requires jq
# doesn't currently support reading states in terraform workspaces
function die {
echo $1
exit 1
}
if test -e .terraform_version ; then
tfver=$(cat .terraform_version | grep -Eo '[0-9.]+')
else
test -e terraform.tfstate || die "no terraform.tfstate file found, can't find version"
tfver=$(jq -r .terraform_version terraform.tfstate | grep -Eo '[0-9.]+')
fi
exec terraform_$tfver $@
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment