Skip to content

Instantly share code, notes, and snippets.

@JasonGiedymin
Last active August 29, 2015 14:05
Show Gist options
  • Save JasonGiedymin/fc3ae3ac99a8536e3dc2 to your computer and use it in GitHub Desktop.
Save JasonGiedymin/fc3ae3ac99a8536e3dc2 to your computer and use it in GitHub Desktop.
Golang version switcher. Activate by `goswitch 1.4` or `goswitch list`. See help at `goswitch help`.
#!/bin/bash
#
# Author: Jason Giedymin <jasong@apache.org>
# Copyright 2014 Jason Giedymin
# License: Apache2
#
# Basic goswitch script to swtich between two gopath roots.
# This script can be useful if you have a basic structure
# and your build scripts don't have knowledge of where
# your golibs are installed.
#
# This script is especially useful if you have a custom
# golang compiler and you need a clean userland install
# without command path trickery.
#
#
# Requirements:
#
# - mkdir ~/local/lib/golang
# - mkdir ~/local/usr/golang
# - mkdir ~/.go
# - put all the golang versions you want in ~/local/lib/golang
# - modify your shell profile and set:
# - GOROOT=~/.go/bin
# - GOPATH=~/.go/workspace
#
# App Vars
# Note: standard installs use /usr/local/go/, however userland is better.
GOROOT=${GOROOT-~/.go/bin}
GOPATH=${GOPATH-~/.go/workspace}
# Directory where all the golang versions reside
GO_LIB_ROOT=~/local/lib/golang
GO_WORKSPACE_ROOT=~/local/usr/golang
# tput colors
T_OK=2
T_WARN=5
T_ERROR=1
T_RESET=0
rmGo(){
# These should exist but for safety they're checked.
if [ -e $GOPATH ]; then
rm -R $GOPATH
fi
if [ -e $GOROOT ]; then
rm -R $GOROOT
fi
}
log() {
echo "$(tput setaf $1)$2 $(tput setaf $T_RESET)"
}
askCreate() {
log $T_WARN "Would you like me to create [$1] for you? [yn]:"
read create
if [ "$create" == "y" ]; then
mkdir -p $1
else
log $T_ERROR "User aborted. Please create workspace manually."
exit 1;
fi
}
goswitch() {
local version=$1
local link_version=go.$version
local link=$GO_LIB_ROOT/$link_version
local workspace_link=$GO_WORKSPACE_ROOT/$link_version
if [ ! -e $link ]; then
log $T_ERROR "Specified go version: [$version] could not be found at [$link]."
exit 1;
fi; # else proceed
if [ ! -e $workspace_link ]; then
log $T_ERROR "Specified go workspace for version: [$version] could not be found at [$workspace_link]."
askCreate $workspace_link
fi; # else proceed
rmGo
ln -sf $link $GOROOT
ln -sf $workspace_link $GOPATH
log $T_OK "Switched to go $version which resides in $link"
}
list() {
ls -x $GO_LIB_ROOT | sed 's/go.//g'
}
help() {
log $T_OK "------------------- Commands -------------------"
log $T_OK " help : this help screen"
log $T_OK " list : lists all versions of go available"
echo
log $T_OK "otherwise pass the version to goswitch:"
log $T_OK " goswitch 1.4"
echo
log $T_OK "other examples:"
log $T_OK " goswitch list"
}
main() {
case "$1" in
list)
$1
;;
help)
$1
;;
*)
if [ $# -gt 0 ]; then
goswitch $1
else
log $T_ERROR "\nInvalid Usage!\n"
help
fi
;;
esac
}
main $@
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment