small pixel drawing of a pufferfish chtf

chtf

#!/bin/sh
#
# list installed tf versions +
# switch terraform versions +
# + download tf if not found
#
# deps
#  - curl
#  - a posix shell
#  - unzip

set -e

tf_version="$1"
tf_install_dir="$HOME/.local/share/chtf"
tf_link_name="$HOME/bin/terraform"

# make some os assumptions
if [ "$(uname)" = "Linux" ]; then
    os="linux"
else
    os="darwin"
fi

mkdir -p "$tf_install_dir"

if [ -z "$tf_version" ]; then
    [ -e "$tf_link_name" ] && active=$(readlink "$tf_link_name" | xargs basename)
    for i in $(ls "$tf_install_dir"); do
        if [ "$i" = "$active" ]; then
            printf "%s*\n" "$i"
        else
            printf "%s\n" "$i"
        fi
    done
else
    if [ ! -f "${tf_install_dir}/${tf_version}" ]; then
        dl="https://releases.hashicorp.com/terraform/${tf_version}/terraform_${tf_version}_${os}_amd64.zip"
        printf "terraform version not found\ndownloading %s\n" "$dl"
        curl -sS --location --fail --output /tmp/tf.zip "$dl" 2>&1 >/dev/null
        unzip /tmp/tf.zip -d /tmp >/dev/null
        mv /tmp/terraform "${tf_install_dir}/${tf_version}"
        rm /tmp/tf.zip
    fi
    mkdir -p "$HOME/bin"
    ln -sf "${tf_install_dir}/${tf_version}" "$tf_link_name"
fi