Skip to content

Instantly share code, notes, and snippets.

@reegnz
Last active October 18, 2021 10:56
Show Gist options
  • Save reegnz/e11a5c9b4bce0cb02e287e2327799466 to your computer and use it in GitHub Desktop.
Save reegnz/e11a5c9b4bce0cb02e287e2327799466 to your computer and use it in GitHub Desktop.
The simplest terraform version manager script you've ever seen

The simplest terraform version manager script you've ever seen

These two scripts provide the bare-minimum version-manager capability for terraform.

Installation:

  1. Put both files somewhere on your PATH
  2. chmod +x on both files to make them executable

Usage:

Anywhere the scripts find a '.terraform-version' file, they'll assume you want to have that version enabled in that directory and all subdirectories.

#!/usr/bin/env bash
set -euo pipefail
TF_VERSIONS_DIR=~/.cache/terraform-versions
find_up() {
name=$1
search_path=$PWD
while [[ "$search_path" != "/" ]]; do
if [[ -f "$search_path/$name" ]]; then
echo "$search_path/$name"
return 0
fi
search_path=$(dirname "$search_path")
done
}
version_file=$(find_up .terraform-version)
if [[ -z $version_file ]]; then
echo "No .terraform-version file found." >&2
exit 1
fi
version=$(cat "$version_file")
if [[ -z "$version" ]]; then
echo "No version defined in $version" >&2
exit 1
fi
if [[ ! -d $TF_VERSIONS_DIR ]]; then
echo "Terraform version $version not found in $TF_VERSIONS_DIR" >&2
echo "You can install terraform by running 'terraform-install'" >&2
exit 1
fi
if ! comm -1 -2 <(echo $version) <(ls -1 $TF_VERSIONS_DIR | sort) >/dev/null; then
echo "Terraform version $version not found in $TF_VERSIONS_DIR" >&2
echo "You can install terraform by running 'terraform-install'" >&2
exit 1
fi
if [[ ! -f $TF_VERSIONS_DIR/$version/terraform ]]; then
echo "Terraform executable not found for version $version in $TF_VERSIONS_DIR" >&2
echo "You can install terraform by running 'terraform-install'" >&2
exit 1
fi
exec "$TF_VERSIONS_DIR/$version/terraform" "$@"
#!/usr/bin/env bash
set -euo pipefail
TF_VERSIONS_DIR=~/.cache/terraform-versions
find_up() {
name=$1
search_path=$PWD
while [[ "$search_path" != "/" ]]; do
if [[ -f "$search_path/$name" ]]; then
echo "$search_path/$name"
return 0
fi
search_path=$(dirname "$search_path")
done
}
version_file=$(find_up .terraform-version)
if [[ -z $version_file ]]; then
echo "No .terraform-version file found." >&2
exit 1
fi
version=$(cat "$version_file")
if [[ -z "$version" ]]; then
echo "No Terraform version defined in $version" >&2
exit 1
fi
if [[ -f "$TF_VERSIONS_DIR/$version/terraform" ]]; then
echo "Terraform version $version already installed at $TF_VERSIONS_DIR/$version" >&2
exit 1
fi
mkdir -p $TF_VERSIONS_DIR/$version
tempfile=$(mktemp)
trap "rm -rf $tempfile" EXIT
# Determine arch
case $(uname -s) in
Darwin*) os=darwin ;;
MSYS_NT* | CYGWIN_NT* | MINGW64*) os=windows ;;
FreeBSD*) os=freebsd ;;
OpenBSD*) os=openbsd ;;
Linux*) os=linux ;;
*)
echo "Unsupported OS: $(uname -s)" >&2
exit 1
;;
esac
case $(uname -m) in
x86_64 | amd64) arch=amd64 ;;
arm64 | aarch64) arch=arm64 ;;
*)
echo "Unsupported Arch: $(uname -m)" >&2
exit 1
;;
esac
url="https://releases.hashicorp.com/terraform/${version}/terraform_${version}_${os}_${arch}.zip"
echo "Downloading $url" >&2
curl "$url" -# -o $tempfile
unzip -d $TF_VERSIONS_DIR/$version $tempfile >/dev/null
echo "Terraform $version installed at $TF_VERSIONS_DIR/$version/terraform" >&2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment