Skip to content

Instantly share code, notes, and snippets.

@gbolo
Last active November 6, 2019 16:01
Show Gist options
  • Save gbolo/e5bb8bd2e10619d06b3362b89e817583 to your computer and use it in GitHub Desktop.
Save gbolo/e5bb8bd2e10619d06b3362b89e817583 to your computer and use it in GitHub Desktop.
go_version_switcher.sh
#!/usr/bin/env bash
#
# Go Version Switcher/Downloader
# THIS SCRIPT SHOULD BE SOURCED UNLESS YOU WANT TO KEEP A STATIC GOROOT OF: /opt/goroot/go
# example usage: source ./go_version_switcher.sh 1.11.5
#
# Some defaults
GO_ROOT_PATH=/opt/goroot
# required binaries
BINS_REQ=(uname curl tar basename)
# validate args
if [ "$#" -ne 1 ]; then
echo "Usage: $(basename "$0") <list,latest,go_version>"
echo " --> list installed versions: $(basename "$0") list"
echo " --> install and switch to latest version: $(basename "$0") latest"
echo " --> install and switch to custom version: $(basename "$0") 1.13.1"
exit 1
fi
# set vars
GO_VERSION="${1}"
# Functions --------------------------------------------------------------------
# check for Required program(s)
CHECK_REQUIREMENTS() {
for p in ${BINS_REQ[@]}; do
hash "$p" 2>&- || \
{ echo >&2 " Required program \"$p\" not installed."; exit 1; }
done
}
# return GO OS if supported
VALIDATE_OS() {
case "${OSTYPE}" in
linux-gnu*) echo "linux"
;;
darwin*) echo "darwin"
;;
*) echo " !! error unsupported OS: $OSTYPE"
exit 1
;;
esac
}
# return GO ARCH if supported
VALIDATE_ARCH() {
case "$(uname -m)" in
x86_64) echo "amd64"
;;
i386) echo "386"
;;
i686) echo "386"
;;
s390x) echo "s390x"
;;
*) echo " !! error unsupported arch: $(uname -m)"
exit 1
;;
esac
}
# go install logic
INSTALL_GO() {
if [ ! -d "${GO_ROOT_PATH}" ]; then
echo ">> CREATING GO_ROOT_PATH DIR: ${GO_ROOT_PATH}"
mkdir -p "${GO_ROOT_PATH}"
fi
if [ ! -d "${GO_ROOT_PATH}/go${GO_VERSION}/go" ]; then
echo ">> ${GO_VERSION} not found. Attempting download and install..."
CHECK_GO_URL
echo ">> downloading ${GO_URL}"
curl -L -o ${GO_ROOT_PATH}/go.tgz ${GO_URL}
if [ $? -ne 0 ] ; then
echo " !! error downloading go version: ${GO_VERSION}"
exit 1
fi
mkdir ${GO_ROOT_PATH}/go${GO_VERSION} && \
tar -xf ${GO_ROOT_PATH}/go.tgz -C ${GO_ROOT_PATH}/go${GO_VERSION} && rm -rf ${GO_ROOT_PATH}/go.tgz
if [ $? -ne 0 ] ; then
echo " !! error extracting tar: ${GO_ROOT_PATH}/go.tgz"
exit 1
fi
else
echo ">> ${GO_VERSION} already installed"
fi
echo "setting GOROOT=${GO_ROOT_PATH}/go${GO_VERSION}/go"
export GOROOT=${GO_ROOT_PATH}/go${GO_VERSION}/go
# TODO perhaps symlink go bin to an existing path instead of adding it...
export PATH=${PATH}:${GOROOT}/bin
# rewrite default link, in case you dont want to source this script
echo "re-linking default GOROOT path"
ln -sTvf ${GO_ROOT_PATH}/go${GO_VERSION}/go ${GO_ROOT_PATH}/go
}
# check if this version of go is valid/available
CHECK_GO_URL() {
RES_CODE=$(curl -w "%{http_code}" -s -I --output /dev/null ${GO_URL})
if [ ${RES_CODE} -ne 200 ] ; then
echo " !! error downloading go version (http status: ${RES_CODE})"
exit 1
fi
}
# list previously installed versions
LIST_INSTALLED_VERSIONS() {
echo ">> INSTALLED GO VERSIONS:"
for v in $(ls -d ${GO_ROOT_PATH}/go1*); do
basename "${v}"
done
}
# displays the current latest version of go from official url
GET_LATEST_VERSION() {
echo -n ">> LATEST RELEASED VERSION:"
curl -s https://golang.org/VERSION?m=text
}
# MAIN -------------------------------------------------------------------------
CHECK_REQUIREMENTS
case "${GO_VERSION}" in
list*) LIST_INSTALLED_VERSIONS
exit 0
;;
latest*) GET_LATEST_VERSION
GO_VERSION=$(curl -s https://golang.org/VERSION?m=text | sed 's/[^0-9\.]*//g')
;;
esac
GO_OS=$(VALIDATE_OS)
GO_ARCH=$(VALIDATE_ARCH)
GO_URL="https://storage.googleapis.com/golang/go${GO_VERSION}.${GO_OS}-${GO_ARCH}.tar.gz"
INSTALL_GO
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment