Skip to content

Instantly share code, notes, and snippets.

@sharpner
Created August 14, 2015 09:32
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sharpner/7d255d7a3a81052a42b3 to your computer and use it in GitHub Desktop.
Save sharpner/7d255d7a3a81052a42b3 to your computer and use it in GitHub Desktop.
clone and symlink go open source project
#/!bin/bash
repository=github.com
if [ -z "$GOPATH" ] ; then
echo '$GOPATH not set.'
exit 1
fi
cd $GOPATH
if [ $# -lt 1 ] ; then
echo "Usage: goclone group/project [repository]"
exit 1
fi
if [ ! -z "$2" ] ; then
repository=$2
fi
group=`echo $1 | cut -f1 -d"/"`
project=`echo $1 | cut -f2 -d"/"`
if [ -z "$project" ] || [ -z "$group" ] ; then
echo "Usage: goclone group/project [repository]"
exit 1
fi
repo=$1
target=$GOPATH/src/$repository/$group
if [ ! -d $target ] ; then
mkdir -p $target
echo "Created folder "$target
fi
cd $target
git clone git@$repository:$repo.git
if [ $? -ne 0 ] ; then
echo "Could not clone repository"
exit 1
fi
symlink=$project
cd $GOPATH
ln -s $target/$project _$symlink
@sharpner
Copy link
Author

this small snippet checks out any go project to the correct destination in your gopath and creates a symlink to fast access it.

It should be placed within your $PATH environment variable.

goclone go-mgo/mgo will then create a symlink $GOPATH/_mgo which points to $GOPATH/src/github.com/go-mgo/mgo

This is particulary useful on own projects.

@flyinprogrammer
Copy link

you might find adding something like:

# First path
GOPATH=$(echo $GOPATH | cut -d":" -f1)

or

# Last path
GOPATH=$(echo $GOPATH | rev | cut -d":" -f1 | rev)

helpful. I ended up using last path so that by default go get would dump into my pkgenv but goclone would dump into my go workspace.

@mattn
Copy link

mattn commented Aug 16, 2015

you can use motemen/ghq.

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