Skip to content

Instantly share code, notes, and snippets.

@curtis1000
Created November 2, 2020 15:53
Show Gist options
  • Save curtis1000/13538f331de28316491b1c81c0fd22b3 to your computer and use it in GitHub Desktop.
Save curtis1000/13538f331de28316491b1c81c0fd22b3 to your computer and use it in GitHub Desktop.
Terraform Version Switcher for MacOS
#!/bin/bash
#######################################
# Terraform version switcher for MacOS
# Usage: use-terraform 0.12.29
#######################################
set -e
die () {
echo >&2 "$@"
exit 1
}
[ "$#" -eq 1 ] || die "1 argument required, $# provided"
version=$1
remote_file=terraform_${version}_darwin_amd64.zip
remote_path=https://releases.hashicorp.com/terraform/$version/${remote_file}
local_versioned_bin_path=/usr/local/bin/terraform-v${version}
local_bin_path=/usr/local/bin/terraform
local_tmp_path=/tmp/${remote_file}
if [ ! -f ${local_versioned_bin_path} ]; then
echo "Downloading ${remote_path} 🚚"
curl ${remote_path} > ${local_tmp_path}
unzip -p ${local_tmp_path} > ${local_versioned_bin_path}
echo "Unarchiving to ${local_versioned_bin_path} 🎁"
chmod +x ${local_versioned_bin_path}
fi
if [ -f ${local_bin_path} ]; then
rm ${local_bin_path}
fi
cp ${local_versioned_bin_path} ${local_bin_path}
echo "Copied ${local_versioned_bin_path} to ${local_bin_path}. Done ✅"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment