added hugo and golang install functions as module

trantor
David Kebler 2020-12-20 17:20:50 -08:00
parent 693983249a
commit 9e5768aa4d
2 changed files with 357 additions and 0 deletions

View File

@ -0,0 +1,218 @@
#!/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."
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"
}

View File

@ -0,0 +1,139 @@
#!/bin/bash
hugo_install () {
# inspried from this forum post https://discourse.gohugo.io/t/script-to-install-latest-hugo-release-on-macos-and-ubuntu/14774/10
# if you have run into github api anonymous access limits which happens during debugging/dev then add user and token here or sourced from a separate file
# . ~/githubapitoken
#GITHUB_USER=""
#GITHUB_TOKEN=""
if [ "$GITHUB_TOKEN" != "" ]; then
echo using access token with script
echo $GITHUB_USER $GITHUB_TOKEN
fi
EXTENDED=false
FORCE=false
EFILE=""
# options
# e - download and install the extended version
# c - use 'hugoe' as the install command for extended version otherwise 'hugo' will launch extended version
# f - force download/overwrite of same version
while getopts 'ecf' OPTION; do
case "$OPTION" in
e)
echo "installing extended hugo"
EXTENDED=true
;;
c)
if [ $EXTENDED = true ]; then
EFILE="e"
echo using hugoe for extended command
fi
;;
f)
echo "FORCING download/overwrite"
FORCE=true
;;
esac
done
shift $(( OPTIND - 1 ))
DEFAULT_BIN_DIR="/usr/local/bin"
# Single optional argument is directory in which to install hugo
BIN_DIR=${1:-"$DEFAULT_BIN_DIR"}
BIN_PATH="$(which hugo$EFILE)"
declare -A ARCHES
ARCHES=( ["arm64"]="ARM64" ["aarch64"]="ARM64" ["x86_64"]="64bit" ["arm32"]="ARM" ["armhf"]="ARM" )
ARCH=$(arch)
if [ -z "${ARCHES[$ARCH]}" ]; then
echo Your machine kernel architecture $ARCH is not supported by this script, aborting
exit 1
fi
INSTALLED="$(hugo$EFILE version 2>/dev/null | cut -d'v' -f2 | cut -c 1-6)"
CUR_VERSION=${INSTALLED:-"None"}
echo $(curl -u $GITHUB_USER:$GITHUB_TOKEN -s https://api.github.com/repos/gohugoio/hugo/releases/latest | grep tag_name)
NEW_VERSION="$(curl -u $GITHUB_USER:$GITHUB_TOKEN -s https://api.github.com/repos/gohugoio/hugo/releases/latest \
| grep tag_name \
| cut -d'v' -f2 | cut -c 1-6)"
echo "Hugo `[ $EXTENDED == true ] && echo "Extended"`: Current Version : $CUR_VERSION => New Version: $NEW_VERSION"
if [ -z "$NEW_VERSION" ]; then
echo Unable to retrieve new version number - Likely you have reached github anonymous limit
echo set environment variable `$GITHUB_USER` and `$GITHUB_TOKEN` and try again
exit 1
fi
if ! [ $NEW_VERSION = $CUR_VERSION ] || [ $FORCE = true ]; then
pushd /tmp/ > /dev/null
URL=$(curl -u $GITHUB_USER:$GITHUB_TOKEN -s https://api.github.com/repos/gohugoio/hugo/releases/latest \
| grep "browser_download_url.*hugo.*._Linux-${ARCHES[$ARCH]}\.tar\.gz" \
| \
if [ $EXTENDED = true ]; then
grep "_extended"
else
grep -v "_extended"
fi \
| cut -d ":" -f 2,3 \
| tr -d \" \
)
echo $URL
echo "Installing version $NEW_VERSION `[ $EXTENDED == true ] && echo "Extended"` "
echo "This machine's architecture is $ARCH"
echo "Downloading Tarball $URL"
wget --user=-u $GITHUB_USER --password=$GITHUB_TOKEN -q $URL
TARBALL=$(basename $URL)
# TARBALL="$(find . -name "*Linux-${ARCHES[$ARCH]}.tar.gz" 2>/dev/null)"
echo Expanding Tarball, $TARBALL
tar -xzf $TARBALL hugo
chmod +x hugo
if [ -w $BIN_DIR ]; then
echo "Installing hugo to $BIN_DIR"
mv hugo -f $BIN_DIR/hugo$EFILE
else
echo "installing hugo to $BIN_DIR (sudo)"
sudo mv -f hugo $BIN_DIR/hugo$EFILE
fi
rm $TARBALL
popd > /dev/null
echo Installing hugo `[ $EXTENDED == true ] && echo "extended"` as hugo$EFILE
BIN_PATH="$(which hugo$EFILE)"
if [ -z "$BIN_PATH" ]; then
printf "WARNING: Installed Hugo Binary in $BIN_DIR is not in your environment path\nPATH=$PATH\n"
else
if [ "$BIN_DIR/hugo$EFILE" != "$BIN_PATH" ]; then
echo "WARNING: Just installed Hugo binary hugo$EFILE to, $BIN_DIR , conflicts with existing Hugo in $BIN_PATH"
echo "add $BIN_DIR to path and delete $BIN_PATH"
else
echo "--- Installation Confirmation ---"
printf "New Hugo binary version at $BIN_PATH is\n $($BIN_PATH version)\n"
fi
fi
else
echo Latest version already installed at $BIN_PATH
fi
}