Created
August 10, 2019 17:26
-
-
Save cymerrad/4e198751a5191745ab759506989b9c93 to your computer and use it in GitHub Desktop.
Gentle Golang installation/update script - it doesn't modify existing GOPATH or binary location.
This file contains 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 | |
# USER DEFINED | |
go_version=1.12.7 | |
go_platform=linux-amd64 | |
go_workspace=$HOME/go # keep the workspace relative to $HOME folder | |
# /USER DEFINED | |
versioned_tarball=go${go_version}.${go_platform}.tar.gz | |
tmp_dir=/tmp/go_install | |
go_tarball=${tmp_dir}/${versioned_tarball} | |
go_unpacked=${tmp_dir}/go | |
go_binary=/usr/bin/go | |
# this will get pushed into .profile | |
go_workspace_literal='$HOME'${go_workspace//${HOME}/} | |
# mandatory | |
go_library_root=/usr/local | |
go_library=${go_library_root}/go | |
go_library_bin=${go_library}/bin | |
function merge_variables_with_existing_installation { | |
# what if someone just added the go binary to the $PATH from the library itself? | |
# e.g. $(which go) == /usr/local/go/bin/go | |
# soft link that I'll perform later will fail | |
if [ -x "$(command -v go)" ]; then | |
go_binary=$(which go) | |
fi | |
if ! [ -z "$GOPATH" ]; then | |
_workspace_after_home=${GOPATH//${HOME}/} | |
go_workspace_literal='$HOME'${_workspace_after_home} | |
eval go_workspace=${go_workspace_literal} | |
else | |
# if $GOPATH is not set, there is no installation | |
echo 'Adding $GOPATH to ~/.profile' | |
echo 'export GOPATH='${go_workspace_literal} >> ~/.profile | |
echo 'export PATH=$PATH:'${go_library_bin}':$GOPATH/bin' >> ~/.profile | |
source ~/.profile | |
fi | |
} | |
function clear_downloads { | |
# downloaded tarball exists | |
if [ -f ${go_tarball} ] | |
then | |
rm ${go_tarball} | |
fi | |
# unpacked tarball exists | |
if [ -d ${go_unpacked} ] | |
then | |
sudo rm -rf ${go_unpacked} | |
fi | |
rm -rf "${tmp_dir}" | |
} | |
function clear_existing_installation { | |
if [ -d "${go_library}" ] | |
then | |
sudo rm -rf "${go_library}" | |
fi | |
if [ -f "${go_binary}" ] | |
then | |
sudo rm "${go_binary}" | |
fi | |
} | |
function make_tmp_and_workspace_dirs { | |
mkdir -p ${tmp_dir} | |
mkdir -p ${go_workspace}/{bin,src,pkg} | |
} | |
function download_and_unpack_tarball { | |
wget -O ${go_tarball} https://storage.googleapis.com/golang/${versioned_tarball} | |
tar -C ${tmp_dir} -xvf ${go_tarball} | |
test -d ${go_unpacked} || { echo "Go didn't unpack. Aborting."; exit 1; } | |
} | |
function change_permissions { | |
# Relict, but was very often necessary for me | |
sudo chown -R root:root ${go_unpacked} | |
sudo chmod -R o+rw ${go_unpacked} | |
} | |
function move_libs { | |
sudo mv ${go_unpacked} ${go_library_root} | |
} | |
function link_binary { | |
sudo ln -s ${go_library_bin}/go ${go_binary} | |
} | |
function err_clean_up { | |
clear_existing_installation | |
clear_downloads | |
} | |
function perform_test { | |
test_dir=${tmp_dir}/install_test | |
mkdir -p ${test_dir} | |
main=${test_dir}/main.go | |
echo "package main">>$main | |
echo "import \"fmt\"">>$main | |
echo "func main() {">>$main | |
echo " fmt.Printf(\"Hello, World!\n\")">>$main | |
echo "}">>$main | |
go run $main | |
test $? -eq 0 || { echo "Test failed. Reverting?"; err_clean_up; exit 1; } | |
go version | |
} | |
############### | |
merge_variables_with_existing_installation | |
make_tmp_and_workspace_dirs | |
download_and_unpack_tarball | |
change_permissions | |
clear_existing_installation | |
move_libs | |
link_binary | |
perform_test | |
clear_downloads |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment