-
-
Save mil1i/f7f166227ec65a51b89a259546805d1d to your computer and use it in GitHub Desktop.
goup: bash script to update to newest or specified version of go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# Gather machine information | |
_mch_arch=$(uname -m) | |
_mch_os=$(uname -s) | |
_mch_goVer=$(go version 2> /dev/null | awk '{print $3}' | sed 's/go//g') | |
if [ ${_mch_arch} = "x86_64" ] | |
then | |
_mch_arch="amd64" | |
fi | |
# What version of Go are we installing? | |
if [ -z "${1}" ] | |
then | |
goVerNewest=$(curl -ls "https://go.dev/VERSION?m=text") | |
goVer=$(sed 's/go//g' <<< "${goVerNewest}") | |
printf "goup: no version provided, will use latest: go${goVer}\n" | |
else | |
goVer=$(sed 's/go//g' <<< "${1}") | |
fi | |
# Exit if go is currently installed and on the same version | |
if [ "${goVer}" = "${_mch_goVer}" ]; then printf "go version already installed: go${_mch_goVer}\n"; exit 0; fi | |
printf "goup: updating system to go${goVer}\n" | |
# Set install directory to either defined GOROOT or ~/.local/go | |
_install_dir="${GOROOT:-/usr/local/go}" | |
tmpdl=$(mktemp -d) | |
# Set download URL path | |
if ! GO_DL_OS=${_mch_os,,}; then GO_DL_OS=$(tr '[:upper:]' '[:lower:]' <<< "${_mch_os}"); fi | |
if ! GO_DL_ARCH=${_mch_arch,,}; then GO_DL_ARCH=$(tr '[:upper:]' '[:lower:]' <<< "${_mch_arch}"); fi | |
GO_DL_PATH="https://go.dev/dl/go${goVer}.${GO_DL_OS}-${GO_DL_ARCH}.tar.gz" | |
# Attempt to download and extract Go version | |
command pushd "${tmpdl}" > /dev/null | |
if ! curl -#OLf "${GO_DL_PATH}" | |
then | |
printf "goup: failed to download go${goVer}. check if version exists.\n\t${GO_DL_PATH}\n" | |
rm -rf "${tmpdl}" | |
exit 1 | |
fi | |
rm -rf "${_install_dir}/*" | |
tar -C "${_install_dir%/*}" -xzf *.tar.gz | |
command popd > /dev/null | |
# Cleanup | |
rm -rf "${tmpdl}" | |
if ! grep -q "${GOROOT}/bin" <<< "${PATH}" | |
then | |
printf "\nGOROOT not found in \$PATH. Recommending to add to \$PATH:\n\techo 'export PATH=\"\$PATH:\$GOROOT/bin\"' >> .profile\n" | |
fi | |
if ! grep -q "${GOPATH}/bin" <<< "${PATH}" | |
then | |
printf "\nGOPATH not found in \$PATH. Recommending to \$PATH:\n\techo 'export PATH=\"\$PATH:\$GOPATH/bin\"' >> .profile\n" | |
fi | |
go version |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment