Skip to content

Instantly share code, notes, and snippets.

@reschenburgIDBS
Last active October 2, 2020 12:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save reschenburgIDBS/cb781f2fca5ac76d9a6bc9f98fd0106f to your computer and use it in GitHub Desktop.
Save reschenburgIDBS/cb781f2fca5ac76d9a6bc9f98fd0106f to your computer and use it in GitHub Desktop.
version switcher for kubectl, kops, helm and terraform - requires https://github.com/junegunn/fzf
function versionswitcher {
local BINARY="$1"
local URL="$2" #must contain 'VERSION' in it somewhere to enable download of different versions
local CACHE="${HOME}/.${BINARY}.versions"
local DSTFILE="/usr/local/bin/${BINARY}"
local TEMPDIR="${CACHE}/tmp"
mkdir -p "$CACHE"
if [[ ! "$3" ]]; then
local VERSION=$(ls "$CACHE" | fzf)
if [[ "$VERSION" == "" ]]; then return 1; fi
local SRCFILE="${CACHE}/${VERSION}"
else
local VERSION="$3"
local SRCFILE="${CACHE}/${BINARY}_${VERSION}"
fi
if ! [ -f "$SRCFILE" ]; then
echo "not found - downloading ${VERSION} of ${BINARY}..."
curl -L --fail --silent "${URL//VERSION/$VERSION}" -o "$SRCFILE"
if [ $? -ne "0" ] ; then
echo "error downloading ${VERSION} of ${BINARY} - aborting"
return 1
fi
if [[ "$URL" == *.tar.gz || "$URL" == *.zip ]]; then
mkdir -p "$TEMPDIR"
tar -xf "$SRCFILE" -C "$TEMPDIR" || unzip "$SRCFILE" -d "$TEMPDIR"
mv -f "$(find "$TEMPDIR" -type f -name "$BINARY")" "$SRCFILE"
rm -rf "$TEMPDIR"
fi
chmod +x "$SRCFILE"
fi
if [ -L "$DSTFILE" ]; then
unlink "$DSTFILE"
fi
echo "setting ${BINARY} to ${VERSION}"
ln -s "$SRCFILE" "$DSTFILE"
}
function gethelm {
versionswitcher "helm" "https://get.helm.sh/helm-VERSION-darwin-amd64.tar.gz" "$1"
}
function getkops {
versionswitcher "kops" "https://github.com/kubernetes/kops/releases/download/VERSION/kops-darwin-amd64" "$1"
}
function getkubectl {
versionswitcher "kubectl" "https://storage.googleapis.com/kubernetes-release/release/VERSION/bin/darwin/amd64/kubectl" "$1"
}
function gettf {
versionswitcher "terraform" "https://releases.hashicorp.com/terraform/VERSION/terraform_VERSION_darwin_amd64.zip" "$1"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment