install golang script first iteration

Sets up environment using block module with new shell env organization
master
David Kebler 2020-11-07 22:19:15 -08:00
parent 628ce9524d
commit 8e441e23b9
3 changed files with 220 additions and 0 deletions

211
golang/goinstall.sh Executable file
View File

@ -0,0 +1,211 @@
#!/bin/bash
# shellcheck disable=SC2016
# set -e
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
}
## START INSTALL
PLATFORM=$(get_platform)
if [ -z "$PLATFORM" ]; then
echo "Your operating system is not supported by this install script."
exit 1
fi
module_load block
[ ! "$(module_loaded block)" ] && echo unable to load block module, exiting && exit 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
declare OPTION
declare OPTARG
declare OPTIND
while getopts 'e:f:r:p:' OPTION; do
# echo $OPTION $OPTARG
case "$OPTION" in
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 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
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 || exit 1
set_block -f $ENV_FILE -n "Go Language Environment"
if [[ $1 = remove ]]; then
confirm Do you really want to remove the go installation? || exit
remove_block
confirm Delete directory $gopath??? && rm -rf $gopath
confirm Delete directory $goroot??? && rm -rf $goroot
exit
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
echo Installed Version $INSTALLED_VERSION is current nothing to do, exiting
exit 0
fi
fi
if [ -d "$goroot" ]; then
echo "The Go install directory ($goroot) already exists. "
confirm Do you want to upgrade to $VERSION? || exit
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."
exit 1
fi
echo "Extracting File..."
mkdir -p "$goroot"
# TODO set correct permissions here if necessary
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"

5
golang/install Executable file
View File

@ -0,0 +1,5 @@
# unset GOROOT
# unset GOPATH
# by sourcing instead of running it will preserve changes to env
source ./goinstall.sh -e m
# source ./install/goinstall.sh -p gopathtest

4
golang/user-remove Executable file
View File

@ -0,0 +1,4 @@
# unset GOROOT
# unset GOPATH
# by sourcing instead of running it will preserve changes to env
source ./goinstall.sh remove