shell-host/lang/go/go.inst

148 lines
3.3 KiB
Bash
Executable File

#!/bin/bash
go_install () {
local latest_url="https://go.dev/VERSION?m=text"
## START INSTALL
module_load host-utils
PLATFORM=$(get_platform)
if [ -z "$PLATFORM" ]; then
echo "Your operating system is not supported by this install script."
return 1
fi
local goroot
local force
declare OPTION
declare OPTARG
declare OPTIND
while getopts 'fr:' OPTION; do
# echo $OPTION $OPTARG
case "$OPTION" in
f)
force=true
;;
r)
goroot=$OPTARG
;;
*)
echo unknown option $OPTION
;;
esac
done
shift $(( OPTIND - 1 ))
if [[ $GOROOT ]]; then
goroot=$GOROOT
else
[[ ! $goroot ]] && goroot="$HOME/.go"
fi
[[ $GOPATH ]] && gopath=$GOROOT || gopath=$goroot/apps
goenv=(
"\n"
"# ======= GO LANGUAGE ENVIRONMENT ===========\n"
'export GOROOT='"$goroot"'\n'
'export PATH=$PATH:'"$goroot"'/bin\n'
'export GOPATH='"$gopath"'\n'
'export PATH=$PATH:'"$gopath"'/bin\n'
"# ======= GO LANGUAGE ENVIRONMENT ==========="
"\n")
module_load confirm
echo go install and environment will be
printf "${goenv[*]}\n"
confirm Do you want to continue || return 1
VERSION=$(curl -s $latest_url | sed 's/^go//')
if [[ $(which go) ]]; then
INSTALLED_VERSION=$(echo $(go version)| awk '{print $3}' | cut -c 3-)
# INSTALLED_VERSION=1.15.2 # this is for testing upgrade, comment out for production
echo installed: $INSTALLED_VERSION available: $VERSION
if [ "$INSTALLED_VERSION" == "$VERSION" ]; then
if [[ ! $force ]]; then
echo Installed Version $INSTALLED_VERSION is current nothing to do, exiting
echo use -r flag to force reinstall
return 2
fi
fi
confirm Do you want to upgrade || return 1
fi
echo installing version $VERSION ......
PACKAGE_NAME="go$VERSION.$PLATFORM.tar.gz"
TEMP_DIRECTORY=$(mktemp -d)
URL=https://go.dev/dl/go$VERSION.$PLATFORM.tar.gz
echo "Downloading $URL ..."
if which wget 2>/dev/null; then
wget $URL -O "$TEMP_DIRECTORY/go.tar.gz"
else
curl -o "$TEMP_DIRECTORY/go.tar.gz" $URL
fi
if [ $? -ne 0 ]; then
echo "Download failed! Exiting."
return 1
fi
echo "Extracting File..."
if ! mkdir -p "$goroot"; then
echo unable to make directory $goroot with current user $user; aborting
return 3
fi
tar -C "$goroot" --strip-components=1 -xzf "$TEMP_DIRECTORY/go.tar.gz"
echo done installing go binary and support files
rm -rf $TEMP_DIRECTORY
echo making go workspace directories
mkdir -p "${gopath}/"{src,pkg,bin}
# TODO set correct permission if all have access
echo "Setting up GO environment..."
if [[ -d $BASH_SHELL_HOST ]] && [[ ! $goroot == "$HOME/.go" ]]; then
echo saving environment to $BASH_SHELL_HOST/load/go.env
# confirm Do you want to continue || return 1
printf "${goenv[*]}\n" > $BASH_SHELL_HOST/load/go.env
else
if [[ -d $BASH_SHELL_USER_DIR ]] && [[ $goroot == "$HOME/.go" ]]; then
echo saving environment to $BASH_SHELL_USER_DIR/load/go.env
# confirm Do you want to continue || return 1
printf "${goenv[*]}\n" > $BASH_SHELL_USER_DIR/load/go.env
else
echo appending environment block to $HOME/.bashrc
# confirm Do you want to continue || return 1
printf "${goenv[*]}\n" >> $HOME/.bashrc
cat $HOME/.bashrc
fi
fi
echo -e "\nGo $VERSION was installed into $goroot.\n(re)start a new shell for environment to take effect"
return 0
}
# go_uninstall () {
# }
# go_upgrade () {
# }
(return 0 2>/dev/null) || go_install $@