Skip to content

Instantly share code, notes, and snippets.

@laidbackware
Last active November 20, 2023 13:33
Show Gist options
  • Save laidbackware/71c642e22edb949086ffccf2eca9ddfd to your computer and use it in GitHub Desktop.
Save laidbackware/71c642e22edb949086ffccf2eca9ddfd to your computer and use it in GitHub Desktop.
ASDF install/update function

Instructions

This function is used to install and switch versions of CLI plugins.

You must source the script containing the funciton. E.g.

source asdfu.sh

The function will:

  • Add plugin if necessary.
  • Install the latest version if no version is specified.
  • Install the version if necessary
  • Set the version globally

Install latest kubectl:

asdfu kubectl

Install Kubectl version 1.28.2

asdfu kubectl 1.28.2
function asdfu() {
product_name=${1:-}
if [ -z $product_name ]; then
>&2 echo "You must pass in a product name. E.g. asdfu kubectl"
return 1
fi
product_version=${2:-}
if [ -z $product_name ]; then
product_version=latest
fi
asdf plugin add $product_name
ret_code=$?
if [ $ret_code -eq 2 ]; then
asdf plugin update $product_name || return 1
elif [ $ret_code -ne 2 ] && [ $ret_code -ne 0 ]; then
>&2 echo "ERROR: Aborting"
return 1
fi
asdf install $product_name $product_version
if [ $? -ne 0 ]; then
>&2 echo "ERROR: Aborting"
return 1
fi
if [ "$product_version" == "latest" ]; then
product_version="$(asdf list $product_name | sed 's/\*//g' | sort -r | head -n 1 | xargs)" || return 1
fi
echo "Setting ${product_version} as global default for ${product_name}"
asdf global $product_name $product_version
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment