shell-host/install/common/golang-install.func

228 lines
5.5 KiB
Bash
Executable File

#!/bin/bash
# shellcheck disable=SC2016
# set -e
# golang_install remove
# golang_ install -e m
function get_platform () {
local OS
local ARCH
local PLATFORM
OS="$(uname -s)"
ARCH="$(uname -m)"
case $OS in
"Linux")
case $ARCH in
"x86_64")
ARCH=amd64
;;
"aarch64")
ARCH=arm64
;;
"armv6")
ARCH=armv6l
;;
"armv8")
ARCH=arm64
;;
.*386.*)
ARCH=386
;;
esac
PLATFORM="linux-$ARCH"
;;
"Darwin")
PLATFORM="darwin-amd64"
;;
esac
echo $PLATFORM
}
golang_install () {
## START INSTALL
PLATFORM=$(get_platform)
if [ -z "$PLATFORM" ]; then
echo "Your operating system is not supported by this install script."
return 1
fi
module_load block
[ ! "$(module_loaded block)" ] && echo unable to load block module, exiting && return 1
module_load confirm
declare ENV_TYPE="u" # Which shell repo for the block
declare ENV_FILE # override shell repo location
# u=user h=host n=network b=base
# by default it is installed in userspace
# local versions of these
declare goroot
declare gopath
local force
declare OPTION
declare OPTARG
declare OPTIND
while getopts 're:f:r:p:' OPTION; do
# echo $OPTION $OPTARG
case "$OPTION" in
r)
force=true
;;
f)
ENV_FILE=$OPTARG
echo explicing setting block to $ENV_FILE
;;
e)
ENV_TYPE=$OPTARG
echo
;;
r)
goroot=$OPTARG
echo setting root $goroot
;;
p)
gopath=$OPTARG
echo setting path $gopath
;;
*)
echo unknown option $OPTION
;;
esac
done
shift $(( OPTIND - 1 ))
if [[ ! $ENV_FILE ]]; then
FILE=lang/go
case $ENV_TYPE in
# h=host m=machine n=network b=base
"h")
[[ -d $BASH_SHELL_HOST/$(hostname) ]] && ENV_FILE=$BASH_SHELL_HOST/$(hostname)/$FILE
;&
"m")
[[ -d $BASH_SHELL_HOST/all ]] && ENV_FILE=$BASH_SHELL_HOST/all/$FILE
;&
"n")
([[ -d $BASH_SHELL_NETWORK/$NETWORKNAME ]] && [[ ! $ENV_FILE ]]) && ENV_FILE=$BASH_SHELL_NETWORK/$NETWORKNAME/$FILE
;&
"b")
([[ -d $BASH_SHELL_BASE ]] && [[ ! $ENV_FILE ]]) && ENV_FILE=$BASH_SHELL_BASE/$FILE
;;
esac
[[ ! $ENV_FILE ]] && ENV_FILE=$HOME/.bashrc # default is userspace
ENV_TYPE="u"
fi
if [[ ! $goroot ]]; then
goroot=$GOROOT
if [[ ! $goroot ]]; then
[[ $ENV_TYPE = "u" ]] && goroot="$HOME/go" || goroot="/opt/go"
fi
fi
if [[ ! $gopath ]]; then
gopath=$GOPATH
if [[ ! $gopath ]]; then
[[ $ENV_TYPE = "u" ]] && gopath="$HOME/go/apps" || gopath="/opt/go/apps"
fi
fi
echo --- go environment settings are -----
echo GOROOT: $goroot
echo GOPATH: $gopath
echo ---------------------------------------
goenv=('export GOROOT='"$goroot"'\n'
'export PATH=$PATH:'"$goroot"'/bin\n'
'export GOPATH='"$gopath"'\n'
'export PATH=$PATH:'"$gopath"'/bin')
echo ==== envionrment block is ====
printf "${goenv[*]}\n"
echo =========================================
echo environment block file is $ENV_FILE
confirm Do you want to continue || return 1
set_block -f $ENV_FILE -n "Go Language Environment"
if [[ $1 = remove ]]; then
confirm Do you really want to remove the go installation? || return 1
remove_block
confirm Delete directory $gopath??? && rm -rf $gopath
confirm Delete directory $goroot??? && rm -rf $goroot
return 1
fi
# the github api doesn't show any release records to had to grab this way
VERSION=$(echo "$(wget -qO- https://github.com/golang/go/tags)" | sed -e 's/<[^>]*>//g'| sed '/^\s*$/d'| grep release-branch| awk '{ print $2 }'|grep -v runtime|sort -V|tail -1|cut -c 3-)
if [[ $(which go) ]]; then
INSTALLED_VERSION=$(echo $(go version)| awk '{print $3}' | cut -c 3-)
# INSTALLED_VERSION=1.15.2 # this is for testing, 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
fi
if [ -d "$goroot" ]; then
echo "The Go install directory ($goroot) already exists. "
confirm Do you want to upgrade to $VERSION? || return 0
fi
echo installing version $VERSION ......
PACKAGE_NAME="go$VERSION.$PLATFORM.tar.gz"
TEMP_DIRECTORY=$(mktemp -d)
echo "Downloading $PACKAGE_NAME ..."
if hash wget 2>/dev/null; then
wget https://storage.googleapis.com/golang/$PACKAGE_NAME -O "$TEMP_DIRECTORY/go.tar.gz"
else
curl -o "$TEMP_DIRECTORY/go.tar.gz" https://storage.googleapis.com/golang/$PACKAGE_NAME
fi
if [ $? -ne 0 ]; then
echo "Download failed! Exiting."
return 1
fi
echo "Extracting File..."
mkdir -p "$goroot"
# TODO set correct permissions here if necessary
sudo 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 "Configuring GO environment in: $ENV_FILE"
mkdir -p "$(dirname $ENV_FILE)"
touch "$ENV_FILE"
add_block
block_add_line "${goenv[*]}"
# echo ---------contents of $ENV_FILE------------------
# cat $ENV_FILE
# echo ------------------------------------------------
echo -e "\nGo $VERSION was installed into $goroot.\n(re)start a new shell environment to take effect"
echo NOTE: all files have root:root user:group, run a chown/chmod/setfacl commands if need be
return 0
}