Skip to content

Instantly share code, notes, and snippets.

@KilledKenny
Created February 20, 2021 16:22
Show Gist options
  • Save KilledKenny/67661d9a4a5fa5ddb6cb9db2d9bb4623 to your computer and use it in GitHub Desktop.
Save KilledKenny/67661d9a4a5fa5ddb6cb9db2d9bb4623 to your computer and use it in GitHub Desktop.
Bash script to change golang version
#!/bin/bash
if [ -z "$GOInstallDir" ] ; then
#echo ${BASH_SOURCE[0]}
GOInstallDir="$(dirname "$(readlink -f "$0")")"
fi
if [[ "$1" =~ (^$|-h) ]] ; then
echo "Usage: $(basename "$0") [-h] <version>"
echo "Example usage: $(basename "$0") 1.10"
echo "This tool downloads and installs a user defined version of go"
echo ""
echo "$(basename "$0") will use GOInstallDir as its root for all files"
echo "All go binary's will be placed in <GOInstallDir>/current/bin"
echo "GOROOT should be set to <GOInstallDir>/current"
echo ""
echo "<GOInstallDir>/current is a symlink to <GOInstallDir>/go<version>/go"
echo "$(basename "$0") will not remove any old versions"
echo "allowing for quick switching to another version."
echo "The default value of GOInstallDir is the same folder as this"
echo "script, note that symlinks are followed allowing this script"
echo "allowing for links from path folders."
echo ""
echo "Current value of GOInstallDir: \"$GOInstallDir\""
echo "GOInstallDir can be overridden using the env variable GOInstallDir"
echo "example:"
echo "cd /bin && ln -s /opt/golang/$(basename "$0")"
echo "GOInstallDir's default value would be /opt/golang/"
echo "GOInstallDir can be reconfigured the env var"
echo "GOInstallDir=/tmp $(basename "$0")"
echo ""
echo "Processor arch is determined using uname -m"
echo "arch can be set using the env var GOInstallArch"
echo ""
echo "OS is set to linux by default"
echo "OS can be overridden using the env variable GOInstallOS"
echo ""
echo "Its the users responsibility to preform cleanup:"
echo "It is safe to remove all <GOInstallDir>/[...].tar.gz"
echo "It is safe to remove <GOInstallDir>/go<version>/go folders except for"
echo "The currently used version"
exit 0
fi
echo "Switching to go version $1"
cd "$GOInstallDir"
#Arch
if [ -z "$goInstallArch" ] ; then
GOInstallArch="$(uname -m)"
fi
GOInstallArch=$(echo "$GOInstallArch" | sed 's| ||g;s|x86[-_]64|amd64|I;s|ARMv8|arm64|I;s|^x86$|386|I;s|ARMv6|armv6l|I;s|\(.*\)|\L\1|g')
#OS
if [ -z "$GOInstallOS" ] ; then
GOInstallOS="linux"
fi
GOInstallOS=$(echo "$GOInstallOS" | sed 's| ||g;s|\(.*\)|\L\1|g')
if ! [ -d "go$1" ] ; then
tarbollName="go$1.$GOInstallOS-$GOInstallArch.tar.gz"
if ! [ -f "$tarbollName" ] ; then
echo "go version $1 not found downloading using the follwing go env:"
echo "version: $1"
echo "arch: $GOInstallArch"
echo "os: $GOInstallOS"
wget "https://golang.org/dl/$tarbollName" || (echo "Unable to download version! quiting"; exit 1)
fi
echo "Unpacking $tarbollName"
mkdir "go$1"
tar xf "$tarbollName" -C "./go$1" || (echo "Unpack failed quitting"; rm -r "go$1" ; exit 1 )
rm "$tarbollName"
fi
rm "current"
ln -s "go$1/go" "current"
echo "Golang verison $1 set"
echo ""
echo "Checking install..."
PathErr=0
Err=0
for f in $(ls "current/bin/") ; do
whichF="$(which "$f")"
pathF="$(readlink -f "$whichF")"
curentF="$(readlink -f "current/bin/$f")"
#echo "$pathF"
#echo "$curentF"
if [ "$pathF" != "$curentF" ]; then
Err=1
PathErr=1
if ! [ -z "$pathF" ] ; then
echo -e "WARNING: path appears to be misconfigured for \e[31m$f\e[0m"
echo -e "- \e[31m$f\e[0m will call \e[31m$whichF\e[0m"
else
echo -e "WARNING: \e[31m$f\e[0m could not be found i PATH"
fi
echo ""
fi
done
if [ "$PathErr" -eq "0" ] ; then
echo "OK: Binarys in $(pwd)/current/bin/ are all found in path"
fi
if [[ ":$PATH:" != *"$(pwd)/current/bin"* ]] ; then
if [ "$PathErr" -eq "1" ]; then
echo -e "Path related warings may be caused by the fact that \e[31m$(pwd)\e[0m is not in \$PATH variable"
echo "If modifying path isnt an option consider adding symlinks to binarys in a path folerd:"
echo "cd <folder_in_path> && ln -s \"$(pwd)/current/bin/<missing_binary>\""
else
echo "INFO: $(pwd)/current/bin is not in the path, all binarys seams to be linked to from path"
fi
else
echo "OK: $(pwd)/current/bin is in the users path."
fi
goenv=$(go env)
goVersionEnv=$(echo "$goenv" | sed '/^GOVERSION/!d;s|[A-Z="]||gI')
if [ "$goVersionEnv" != "$1" ]; then
Err=1
echo "WARNING: Version reported by go env in GOVERISON is $goVersion not $1"
else
echo "OK: GOVERISON variable reported by go env is correct"
fi
goRootEnv=$(echo "$goenv" | sed '/^GOROOT/!d;s|GOROOT="||;s|"||')
goRootEnvLink=$(readlink -f "$goRootEnv")
if [ "$goRootEnvLink" != "$(readlink -f "current/")" ]; then
Err=1
echo "GOROOT reported by go env appears to be misconfigured"
echo "GOROOT shoudl be: $(pwd)/current"
echo "GOROOT is: $goRootEnv"
echo "Note this test may be buggy due to lack of testing...."
else
echo "OK: the GOROOT variable reported by go env is correct"
fi
goRootEnvLink=$(readlink -f "$goRootEnv")
if [ "$(readlink -f "$GOROOT")" != "$(readlink -f "current/")" ]; then
Err=1
echo "The envrioment varaible GOROOT appears to be misconfigured"
echo "GOROOT shoudl be: $(pwd)/current"
echo "GOROOT is: $GOROOT"
echo "Note this test may be buggy due to lack of testing...."
else
echo "OK: GOROOT enviroment variable is correct"
fi
if [ "$Err" -eq "0" ] ; then
echo "Install looks good"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment