Created
April 1, 2017 15:46
-
-
Save apparentlymart/b4342e320fa756690d36d52984bb17ea to your computer and use it in GitHub Desktop.
Helper bash script for creating and activating GOPATHs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Note: this is written for and tested only in bash | |
function _go-here { | |
DIR="$1" | |
export GOPATH="$DIR" | |
export PATH="$GOPATH/bin":$PATH | |
echo Activating GOPATH="$GOPATH" | |
} | |
function go-here { | |
COMMAND="$1" | |
THIS_DIR="$(realpath .)" | |
case "$COMMAND" in | |
init) | |
if [ "$(ls -A "$THIS_DIR")" ]; then | |
echo >&2 "Can't init a GOPATH in $THIS_DIR: not empty" | |
return 1 | |
fi | |
echo Initializing "$THIS_DIR" as GOPATH | |
mkdir -- "$THIS_DIR/bin" | |
mkdir -- "$THIS_DIR/src" | |
mkdir -- "$THIS_DIR/pkg" | |
_go-here "$THIS_DIR" | |
INITIAL_PACKAGE="$2" | |
if [ "$INITIAL_PACKAGE" != "" ]; then | |
echo Getting "$INITIAL_PACKAGE" | |
go get -u -t -- "$INITIAL_PACKAGE" | |
fi | |
return 0 | |
;; | |
activate) | |
while [ "$THIS_DIR" != "/" ]; do | |
if [ -d "$THIS_DIR"/src ]; then | |
_go-here "$THIS_DIR" | |
return 0 | |
fi | |
THIS_DIR="$(dirname -- "$THIS_DIR")" | |
done | |
>&2 echo Failed to find suitable GOPATH from here | |
return 1 | |
;; | |
*) | |
>&2 cat <<EOT | |
Usage: go-here <command> | |
Commands: | |
go-here init | |
Creates an empty GOPATH skeleton in the current directory (which must | |
be empty) and immediately activates it. | |
go-here init <initial-package> | |
Like the plain "go-here init" except also runs "go get" with the | |
given package to install an initial package (and its dependencies) | |
into the new GOPATH. | |
go-here activate | |
Looks for a suitable GOPATH by walking up from the current directory | |
until a "src" subdirectory is found and then activates the discovered | |
directory. | |
EOT | |
return 2 | |
;; | |
esac | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment