Skip to content

Instantly share code, notes, and snippets.

@rjeczalik
Created August 3, 2016 15:12
Show Gist options
  • Save rjeczalik/3c82b0f1e3d78ee0e372171018d0a060 to your computer and use it in GitHub Desktop.
Save rjeczalik/3c82b0f1e3d78ee0e372171018d0a060 to your computer and use it in GitHub Desktop.
#!/bin/bash
toggle-go() {
local version="${1-}"
if [[ ! -z "$version" ]]; then
toggle-go-install "$version"
else
version=$(toggle-go-next "$(toggle-go-current)")
fi
rm -f "${HOME}/go"
ln -s "${HOME}/${version}" "${HOME}/go"
go version
}
toggle-go-install() {
local version="${1-}"
case "$version" in
gotip)
toggle-go-install go1.4
local goPath="${HOME}/gotip"
if [[ ! -e "$goPath" ]]; then
git clone [email protected]:golang/go "$goPath"
fi
pushd "${goPath}/src" &>/dev/null
git pull --rebase origin master
GOBIN="${goPath}/bin" ./make.bash
popd
;;
"")
;;
go*)
local urls=(
"https://storage.googleapis.com/golang/${version}.darwin-amd64.tar.gz"
"https://storage.googleapis.com/golang/${version}.darwin-amd64-osx10.8.tar.gz"
)
local goPath="${HOME}/${version}"
[[ -e "$goPath" ]] && return
pushd $(mktemp -d /tmp/XXXXX) &>/dev/null
echo "# downloading ${version}..." >&2
for url in ${urls[@]}; do
if curl -sSL "$url" | tar -zxf - 2>/dev/null; then
break
fi
done
mv go "$goPath"
popd
;;
esac
}
toggle-go-current() {
local currentVersion="$( (go version || true) | cut -d' ' -f3 )"
if [[ "$currentVersion" = "devel" ]]; then
echo "gotip"
else
echo "$currentVersion"
fi
}
toggle-go-next() {
local allVersions=$(find -E ~ -type d -depth 1 -regex '.*go(tip|[0-9\.]+)$' -exec basename {} \; )
( echo $allVersions; echo $allVersions ) | tr ' ' '\n' | grep "$1" -A1 | sed -n 2p
}
export GOPATH=$HOME
export PATH=$HOME/go/bin:$HOME/bin:$PATH
export GOROOT=$HOME/go
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment