Skip to content

Instantly share code, notes, and snippets.

@allenap
Last active December 30, 2016 07:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save allenap/6221004 to your computer and use it in GitHub Desktop.
Save allenap/6221004 to your computer and use it in GitHub Desktop.
# Bash function to cd to a package on GOPATH (inspiration for that
# from... elsewhere; I can't remember now unfortunately), and bash
# completion for the aforementioned (my own work). Have at it.
# -- Gavin Panella, 2013
gcd() {
local package="$1"
local pdir="$(go list -e -f '{{.Dir}}' "${package}")"
if [ -z "${pdir}" ]
then
echo "${package} not found" >&2
return 1
else
cd "${pdir}"
fi;
}
_gcd() {
local cur path paths
# The current word due for completion.
cur=${COMP_WORDS[COMP_CWORD]}
# Non-local array storing the possible completions.
COMPREPLY=()
# Split GOPATH by colon, being careful about whitespace.
IFS=: read -a paths -r <<< "$GOPATH"
# Iterate GOPATH, again being careful about whitespace.
for path in "${paths[@]}"
do
COMPREPLY=(
"${COMPREPLY[@]}"
# Ignore missing directories on GOPATH.
$(cd "${path}/src" 2> /dev/null &&
compgen -o dirnames "${cur}")
)
done
return 0
}
complete -F _gcd -o nospace -S / gcd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment