Skip to content

Instantly share code, notes, and snippets.

@dansimau
Last active November 30, 2017 17:02
Show Gist options
  • Star 16 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dansimau/f5d8b0b2aefb9c9c46621f4869ccc151 to your computer and use it in GitHub Desktop.
Save dansimau/f5d8b0b2aefb9c9c46621f4869ccc151 to your computer and use it in GitHub Desktop.
Bash functions for navigating Go workspaces ("Go-go!")
#
# Change to the directory of the specified Go package name.
#
gg() {
paths=($(g "$@"))
path_index=0
if [ ${#paths[@]} -gt 1 ]; then
c=1
for path in "${paths[@]}"; do
echo [$c]: cd ${GOPATH}/${path}
c=$((c+1))
done
echo -n "Go to which path: "
read path_index
path_index=$(($path_index-1))
fi
path=${paths[$path_index]}
cd $GOPATH/src/$path
}
#
# Print the directories of the specified Go package name.
#
g() {
local pkg_candidates="$((cd $GOPATH/src && find . -mindepth 1 -maxdepth 5 -type d \( -path "*/$1" -or -path "*/$1.git" \) -print) | sed 's/^\.\///g')"
echo "$pkg_candidates"
}
#
# Bash autocomplete for g and gg functions.
#
_g_complete()
{
COMPREPLY=()
local cur
local prev
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
COMPREPLY=( $(compgen -W "$(for f in $(find "$GOPATH/src" -mindepth 1 -maxdepth 5 -type d -name "${cur}*" ! -name '.*' ! -path '*/.git/*' ! -path '*/test/*' ! -path '*/Godeps/*' ! -path '*/examples/*'); do echo "${f##*/}"; done)" -- "$cur") )
return 0
}
complete -F _g_complete g
complete -F _g_complete gg
@dansimau
Copy link
Author

Change directory easily:

dan-mbp-work:~ dan$ gg testify
dan-mbp-work:testify dan$ pwd
/Users/dan/.go/src/github.com/stretchr/testify
dan-mbp-work:testify dan$ 

When there's more than one possible path:

dan-mbp-work:~ dan$ gg thrift
[1]: cd /Users/dan/.go/src/git.apache.org/thrift.git/lib/go/thrift
[2]: cd /Users/dan/.go/src/github.com/apache/thrift
[3]: cd /Users/dan/.go/src/github.com/samuel/go-thrift/thrift
[4]: cd /Users/dan/.go/src/github.com/uber/ringpop-go/test/thrift
[5]: cd /Users/dan/.go/src/github.com/uber/tchannel/golang/thrift
[6]: cd /Users/dan/.go/src/github.com/uber/tchannel/thrift
[7]: cd /Users/dan/.go/src/github.com/uber/tchannel-go/examples/thrift
[8]: cd /Users/dan/.go/src/github.com/uber/tchannel-go/thrift
[9]: cd /Users/dan/.go/src/github.com/uber/tchannel-go/trace/thrift
Go to which path: 1
dan-mbp-work:thrift dan$ pwd
/Users/dan/.go/src/git.apache.org/thrift.git/lib/go/thrift
dan-mbp-work:thrift dan$ 

Forgot the full path to that package? Use the g helper:

dan-mbp-work:ringpop-go dan$ godep update `g tchannel-go`
dan-mbp-work:ringpop-go dan$

@dansimau
Copy link
Author

Autocomplete:

dan-mbp-work:~ dan$ gg ringp[TAB]
ringpop       ringpop-go    ringpop-node  ringpop-wait  ringpopd-go   
dan-mbp-work:~ dan$ gg ringpop

Use a more specific path to avoid interactive prompt:

dan-mbp-work:~ dan$ gg apache/thrift
dan-mbp-work:thrift dan$ pwd
/Users/dan/.go/src/github.com/apache/thrift
dan-mbp-work:thrift dan$ 

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