Skip to content

Instantly share code, notes, and snippets.

@renevo
Created January 2, 2022 18:20
Show Gist options
  • Save renevo/f6ed9c609f3854c10a83ce1555a68f11 to your computer and use it in GitHub Desktop.
Save renevo/f6ed9c609f3854c10a83ce1555a68f11 to your computer and use it in GitHub Desktop.
Simple Go Version Management on Linux
#! /bin/bash
GVM_COMMAND=$1
GO_INSTALL_PATH=/usr/local/go
# if this is different, then change them...
OS=linux
ARCH=amd64
case $GVM_COMMAND in
install)
GO_VERSION=$2
if [[ -z $GO_VERSION ]]; then
echo 'ERROR: Go Version expected'
exit 1
fi
if [[ -d "${GO_INSTALL_PATH}-${GO_VERSION}" ]]; then
${GO_INSTALL_PATH}-${GO_VERSION}/bin/go version
exit 0
fi
echo "Installing $GO_VERSION"
wget -P ${HOME} https://storage.googleapis.com/golang/go${GO_VERSION}.${OS}-${ARCH}.tar.gz
mkdir -p ${HOME}/go-${GO_VERSION}
tar -xf ${HOME}/go${GO_VERSION}.${OS}-${ARCH}.tar.gz -C ${HOME}/go-${GO_VERSION} --strip-components=1
sudo mv ${HOME}/go-${GO_VERSION} /usr/local
rm -rfv ${HOME}/go-${GO_VERSION}
rm -rf ${HOME}/go${GO_VERSION}.${OS}-${ARCH}.tar.gz
${GO_INSTALL_PATH}-${GO_VERSION}/bin/go version
;;
list)
ls /usr/local | grep "go-" | sed 's/go-//'
;;
use)
GO_VERSION=$2
if [[ -z $GO_VERSION ]]; then
echo 'ERROR: Go Version expected'
exit 1
fi
if [[ -d "${GO_INSTALL_PATH}-${GO_VERSION}" ]]
then
if [[ -d "${GO_INSTALL_PATH}" ]]; then
sudo unlink ${GO_INSTALL_PATH}
fi
sudo ln -s "${GO_INSTALL_PATH}-${GO_VERSION}" "${GO_INSTALL_PATH}"
${GO_INSTALL_PATH}/bin/go version
else
echo "ERROR: Go version not found - try gvm install ${GO_VERSION} first"
fi
;;
rm)
GO_VERSION=$2
if [[ -z $GO_VERSION ]]; then
echo 'ERROR: Go Version expected'
exit 1
fi
if [[ -d "${GO_INSTALL_PATH}-${GO_VERSION}" ]]; then
echo "Removing go version ${GO_VERSION}"
sudo rm -rf "${GO_INSTALL_PATH}-${GO_VERSION}"
fi
# clean up the symlink
sudo find ${GO_INSTALL_PATH} -xtype l -delete
;;
*)
# write a help output....
echo "Unknown Command"
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment