my dotz
1#!/bin/sh
2#
3# list installed tf versions +
4# switch terraform versions +
5# + download tf if not found
6
7tf_version="$1"
8tf_install_dir="${HOME}/.local/share/chtf"
9tf_link_name="${HOME}/bin/terraform"
10
11# make some os assumptions
12if [ "$(uname)" = "Linux" ]; then
13 os="linux"
14else
15 os="darwin"
16fi
17
18mkdir -p "$tf_install_dir"
19
20if [ -z "$tf_version" ]; then
21 # if no arguments, list versions of tf plus list symlink pointer.
22 printf "available:\n"
23 ls "$tf_install_dir"
24 printf "active:\n"
25 if [ -e "$tf_link_name" ]; then
26 readlink "$tf_link_name" | xargs basename
27 fi
28else
29 if [ ! -f "${tf_install_dir}/${tf_version}" ]; then
30 # download tf version if not found
31 printf "terraform version not found. downloading...\n"
32 dl="https://releases.hashicorp.com/terraform/${tf_version}/terraform_${tf_version}_${os}_amd64.zip"
33 printf "downloading %s\n" "$dl"
34 curl -L --output /tmp/tf.zip "$dl"
35 unzip /tmp/tf.zip -d /tmp
36 mv /tmp/terraform "${tf_install_dir}/${tf_version}"
37 rm /tmp/tf.zip
38 fi
39 ln -sf "${tf_install_dir}/${tf_version}" "$tf_link_name"
40fi