Skip to content

Instantly share code, notes, and snippets.

@shanna
Last active May 3, 2016 15:11
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 shanna/7b98b8f8b6788e2280481d8e97bc5a94 to your computer and use it in GitHub Desktop.
Save shanna/7b98b8f8b6788e2280481d8e97bc5a94 to your computer and use it in GitHub Desktop.
Symlink Go packages into local project directory.
#!/usr/bin/env sh
# Setup Go environment.
#
# source ~/local/bin/go-env
# http://superuser.com/questions/39751/add-directory-to-path-if-its-not-already-there
__util_env_path_prepend() {
if [ -d "$1" ] && [[ ":$PATH:" != *":$1:"* ]]; then
PATH="$1:$PATH"
fi
}
# Don't put GOPATH in my project directory.
#
# See go-ln and symlink in your go projects with more suitable (shorter) names
# and less directory traversal.
#
# I like a flat project repository structure.
#
# ~/projects/
# shanna-foo/
# shanna-bar/
#
# $ git clone github.com/shanna/bar shanna-bar
# $ go-ln github.com/shanna/bar shanna-bar
#
if [ -x "$HOME/local/go/bin/go" ]; then
mkdir -p "$HOME/go/src $HOME/go/pkg $HOME/go/bin"
export GOROOT="$HOME/local/go"
export GOPATH="$HOME/go"
__util_env_path_prepend "$HOME/local/go/bin"
__util_env_path_prepend "$HOME/go/bin"
GO_VERSION=$(go version | cut -f3 -d' ')
echo "❤ go ${GO_VERSION}"
elif [ `which go` ]; then
GO_VERSION=$(go version | cut -f3 -d' ')
echo "✗ go, fallback to system go ${GO_VERSION}"
fi
# Cleanup if sourced.
unset -f __util_env_path_prepend
#!/usr/bin/env bash
set -e
# https://golang.org/dl
version=go1.6.1
release="${version}.linux-amd64.tar.gz"
if [ "$(uname)" == "Darwin" ]; then
release="${version}.darwin-amd64.tar.gz"
fi
mkdir -p ~/local
if [ -x "$GOROOT/bin/go" ]; then
GO_VERSION=$(go version | cut -f3 -d' ')
echo "✗ go ${GO_VERSION} already in path $GOROOT"
else
wget -q --no-check-certificate https://storage.googleapis.com/golang/$release -O $release
tar -C ~/local -xzf $release
rm $release
GOROOT=~/local/go
GO_VERSION=$($GOROOT/bin/go version | cut -f3 -d' ')
echo "❤ go ${GO_VERSION} installed in path ~/local/go"
fi
#!/usr/bin/env sh
set -e
main() {
if [ $# -ne 2 ]; then
cat <<-USAGE
usage: goln [remote] [local]
Symlink GOPATH package to local directory. Handy if you have an existing
development folder structure and you don't want Go packages messing it
up.
example:
echo \$GOPATH
#=> /home/shanna/go
pwd -P
#=> /home/shanna/projects
goln github.com/shanna/some-package some-package
#=> /home/shanna/go/src/github.com/shanna/some-package -> /home/shanna/projects/some-package
USAGE
exit 0;
fi
if [ "$GOPATH" == "" ]; then
echo "No \$GOPATH set."
exit 1
fi
local=$2
remote=$GOPATH/src/$1
mkdir -p $local
local=$(cd $local && pwd -P)
mkdir -p $(dirname $remote)
ln -sfhv $local $remote
}
main "$@"
@deepfryed
Copy link

Don't use set -e in interactive shells (go-env). It will be inherited by all sub processes.

@shanna
Copy link
Author

shanna commented May 3, 2016

👍 Updated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment