Commit 472252fc authored by Dom Sekotill's avatar Dom Sekotill
Browse files

Add gosetup for easily installing Go

parent 53765874
Loading
Loading
Loading
Loading

.bin/gosetup

0 → 100755
+70 −0
Original line number Diff line number Diff line
#!/usr/bin/env bash
#
# Copyright 2019  Dominik Sekotill <dom.sekotill@kodo.org.uk>
# 
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# 
#     http://www.apache.org/licenses/LICENSE-2.0
# 
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

set -eu -o pipefail
shopt -s extglob

case $0 in
	?(*/)go1.*)
		versions=( ~/.local/$(basename "$0")* )
		export GOROOT=${versions[-1]}
		exec ${GOROOT}/bin/go "$@"
		;;
esac

# Install Go-$VERSION

case `uname -m` in
	x86_64|amd64) ARCH=amd64 ;;
	i686*|i386) ARCH=386 ;;
	armv6l|armv7l) ARCH=armv6l ;;
	arm64|aarch64) ARCH=arm64 ;;
esac
KERNEL=$(uname -s | tr '[[:upper:]]' '[[:lower:]]')
PATTERN="https:\\/\\/dl.google.com\\/go\\/go${VERSION-}[0-9.]*\\.${KERNEL}-${ARCH}\\.tar\\.gz" 
URL=$(wget -qO- https://golang.org/dl/ | grep -o "${PATTERN}" | head -n1) || true

if [[ ${#URL} -eq 0 ]]; then
	echo >&2 "Could not find a link for version: ${VERSION-latest}"
	echo >&2 "(tried ${PATTERN})"
	exit 1
fi

if [[ -z "${VERSION-}" ]]; then
	SET_DEFAULT=y
fi

VERSION=$(basename $URL)
VERSION=${VERSION%.${KERNEL}-${ARCH}.tar.gz}
VERSION=${VERSION#go}
PRIME_VERSION=${VERSION%.${VERSION#*.*.}}

if [[ -x ~/.local/go${VERSION}/bin/go ]]; then
	echo >&2 "Go ${VERSION} appears to already be installed"
elif ! mkdir ~/.local/go${VERSION}; then
	echo >&2 "Please remove this directory/file to continue installation"
	exit 1
else
	wget -O- $URL | tar -xz -C ~/.local/go${VERSION} --strip-components 1
fi

ln -srf $0 ~/.local/bin/go${VERSION}
ln -srf $0 ~/.local/bin/go${PRIME_VERSION}

if [[ -n "${SET_DEFAULT-}" ]]; then
	rm -f ~/.local/go
	ln -sr ~/.local/go${VERSION} ~/.local/go
fi