Created
June 3, 2019 15:16
-
-
Save liskl/4c3d59c65885c532972d49553fa68b60 to your computer and use it in GitHub Desktop.
install or swap Terraform versions on Mac OSX.
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
#!/usr/bin/env bash | |
scratch=$(mktemp -d -t tmp.XXXXXXXXXX) | |
function finish { | |
rm -rf "$scratch" | |
} | |
trap finish EXIT | |
STAGE=1 | |
function step_message (){ | |
echo -ne "\033[0;36mSTEP ${STAGE})\033[0m: \033[0;35m${@}\033[0m\n" | |
let "STAGE++" | |
} | |
if command -v dialog; then | |
true; | |
else | |
echo "install dialog"; | |
echo "MACOSX: 'brew install dialog; brew upgrade dialog;'"; | |
exit 1; | |
fi | |
if command -v curl; then | |
true; | |
else | |
echo "install curl"; | |
echo "MACOSX: 'brew install curl; brew upgrade curl;'"; | |
exit 1; | |
fi | |
# TODO Dynamic Options Array..., would also need improves case statement logic... | |
# $ curl -skL https://releases.hashicorp.com/terraform/ | grep 'a href' | grep '/terraform' | grep '\.[0-9]*</a>' | sed -e 's@^.*\w*<a href="/[a-z]*/\([0-9\.]*\)/">.*$@\1@' | sort -t '.' -k2 -n --uniq | |
HEIGHT=15 | |
WIDTH=40 | |
CHOICE_HEIGHT=4 | |
BACKTITLE="Backtitle here" | |
TITLE="Choose Terraform Version" | |
MENU="Choose one of the following options:" | |
OPTIONS=( 1 "v0.11.14" | |
2 "v0.12.0" | |
3 "v0.10.8" | |
4 "v0.9.11" | |
5 "v0.8.8" | |
6 "v0.7.13" | |
7 "v0.6.16" | |
8 "v0.5.3" | |
9 "v0.4.2" | |
10 "v0.3.7" | |
11 "v0.2.2" | |
12 "v0.1.1") | |
CHOICE=$(dialog --clear \ | |
--backtitle "$BACKTITLE" \ | |
--title "Choose Terraform Version" \ | |
--menu "Choose one of the following options:" \ | |
$HEIGHT $WIDTH $CHOICE_HEIGHT \ | |
"${OPTIONS[@]}" \ | |
2>&1 >/dev/tty) | |
STRING="You chose Terraform version:" | |
clear | |
case $CHOICE in | |
1) | |
VERSION="0.11.14" ; | |
echo "${OPTIONS[1]}" | |
;; | |
2) | |
VERSION="0.12.0" ; | |
echo "${OPTIONS[2+1]}" | |
;; | |
3) | |
VERSION="0.10.8" ; | |
echo "${OPTIONS[4+2:A1]}" | |
;; | |
4) | |
VERSION="0.9.11" ; | |
;; | |
5) | |
VERSION="0.8.8" ; | |
;; | |
6) | |
VERSION="0.7.13" ; | |
;; | |
7) | |
VERSION="0.6.16" ; | |
;; | |
8) | |
VERSION="0.5.3" ; | |
;; | |
9) | |
VERSION="0.4.2" ; | |
;; | |
10) | |
VERSION="0.3.7" ; | |
;; | |
11) | |
VERSION="0.2.2" ; | |
;; | |
12) | |
VERSION="0.1.1" ; | |
;; | |
*) | |
echo "exiting caused by user abort." | |
exit 0; | |
;; | |
esac | |
echo "${STRING} ${VERSION}"; | |
URL="https://releases.hashicorp.com/terraform/${VERSION}/terraform_${VERSION}_darwin_amd64.zip"; | |
step_message "Downloading Terraform:\t\t\t'${URL}'" ; | |
curl -skL -o "${scratch}/terraform_${VERSION}_darwin_amd64.zip" "${URL}" ; | |
step_message "Extracting Binary from Archive:\t'${scratch}/terraform_${VERSION}_darwin_amd64.zip'" ; | |
unzip -q "${scratch}/terraform_${VERSION}_darwin_amd64.zip" -d ${scratch} ; | |
step_message "ensuring directory ${HOME}/.bin exists." ; | |
mkdir -p "${HOME}/.bin" ; | |
step_message "installing terraform binary at '${HOME}/.bin/tf_${VERSION}'" ; | |
chmod +x "${scratch}/terraform" && \ | |
chown "$(id -un):$(id -gn)" "${scratch}/terraform" && \ | |
mv "${scratch}/terraform" "${HOME}/.bin/tf_${VERSION}" && \ | |
ln -sf "${HOME}/.bin/tf_${VERSION}" "${HOME}/.bin/terraform" ; | |
if ! grep -q 'export PATH="${PATH}:${HOME}/.bin";' ~/.bash_profile; then | |
step_message "patching \$PATH in ${HOME}/.bash_profile to include ${HOME}/.bin" ; | |
sed -i'' -e '/export PATH="${PATH}:${HOME}\/.bin";/d' ${HOME}/.bash_profile ; | |
echo 'export PATH="${PATH}:${HOME}/.bin";' >> ${HOME}/.bash_profile ; | |
echo "run the following command to activate change." | |
echo "source '${HOME}/.bash_profile';" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment