Skip to content

Instantly share code, notes, and snippets.

@inancgumus
Created October 29, 2017 22:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save inancgumus/480b8aa8e87e0a01dbe9bb361ce9c02b to your computer and use it in GitHub Desktop.
Save inancgumus/480b8aa8e87e0a01dbe9bb361ce9c02b to your computer and use it in GitHub Desktop.
Makes your environment ready for Go contributions
#!/bin/bash
# -----------------------------------------------------------------------------
# HOW TO RUN?
# -----------------------------------------------------------------------------
# . ./godev.bash
# WARNING:
#
# if you run it directly like: ./godev.bash
# it won't save to $PATH environment variable.
# WARNING:
#
# Because, this file will be sourced into the current shell. We'll be
# clearing out all the variables we've defined.
# -----------------------------------------------------------------------------
# WHY THIS?
# -----------------------------------------------------------------------------
# switches the environment for go development,
# under $GODEV directory.
# if you don't run this, you can't do development
# on your machine because it will use homebrew's
# go instead.
# -----------------------------------------------------------------------------
# SETTINGS
# -----------------------------------------------------------------------------
# SET THIS TO WHERE THE GO DEVELOPMENT WILL BE DONE:
GODEV="$HOME/dev/golang"
# -----------------------------------------------------------------------------
# OK, GO!
# -----------------------------------------------------------------------------
# wrap with : to ease the regex in rempath
XPATH=":$PATH:"
rempath() {
XPATH=$(echo $XPATH | sed 's|:[^:]*'"$1":'|:|g')
}
# removes the wrapped colons
remcolons() {
XPATH=${XPATH:1:${#XPATH}-2}
}
# unset go env vars
unset OLD_GOROOT
if [[ ! -z "${GOROOT// }" ]]; then
OLD_GOROOT=$GOROOT
unset GOROOT
fi
unset OLD_GOPATH
if [[ ! -z "${GOPATH// }" ]]; then
OLD_GOPATH=$GOPATH
unset GOPATH
fi
# remove the existing go paths
rempath "/go/bin"
rempath "/go/libexec/bin"
remcolons
# add go binaries path to the beginning of path if it's not there.
# this will let us only to use the go binaries inside $GODEV.
if [[ $XPATH != "$GODEV/bin"* ]]; then
XPATH="$GODEV/bin:$XPATH"
fi
# -----------------------------------------------------------------------------
# DISPLAY LOGS
# -----------------------------------------------------------------------------
if [[ -z $OLD_GOROOT ]]; then
echo "GOROOT was empty"
else
echo -e "OLD GOROOT: $OLD_GOROOT"
echo -e "GOROOT is cleared"
fi
if [[ -z $OLD_GOPATH ]]; then
echo "GOPATH was empty"
else
echo -e "OLD GOPATH: $OLD_GOPATH"
echo -e "GOPATH is cleared"
fi
if [[ $PATH == $XPATH ]]; then
echo 'Nothing is changed. $PATH is fine.'
else
echo -e "\n"
echo -e "OLD PATH:\n\n\t$PATH\n\n"
echo -e "NEW PATH:\n\n\t$XPATH\n"
fi
# -----------------------------------------------------------------------------
# SET AND CLEAR VARIABLES - FINISHING TOUCH
# -----------------------------------------------------------------------------
PATH=$XPATH
unset XPATH
unset GODEV
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment