Last active
November 2, 2017 09:28
-
-
Save farnasirim/fa4f7ab61a6944e214df6b9fd6525cd9 to your computer and use it in GitHub Desktop.
navigate to a go project directory easily using `goto` command
This file contains 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
#!/bin/bash | |
if [ -z "$@" ]; then | |
echo "Usage: goto project-name" | |
return | |
fi | |
search_path=$GOPATH/src/ | |
for i in {1..3}; do | |
len=${#search_path} | |
project_dir=$(find $GOPATH/src -maxdepth $i -mindepth $i | cut -c$len- | grep -m 1 $1) | |
if [ ! -z "$project_dir" ] ; then | |
cd $search_path/$project_dir | |
break | |
fi | |
done |
exit 0
instead of return
in usage clause. 😃
@remohammadi I'll let you try that to find out why it would not work.
Things would be cleaner if you didn't source the script every time. Instead of sourcing a script that then executes you could write:
# goto.sh here
function goto
{
cd $(find $GOPATH/src -type d | egrep $* )
}
Then usage would be :
- Add "
. ~/path/to/go.sh
" to the end of ~/.bash_profile, or your zsh init file.
(Obviously I simplified your script, but not by too much I hope.)
Very helpful 👍
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage:
goto.sh
):~/.zshrc
or~/.bashrc
if you're not hanging out with the cool kids yet: IMPORTANT: The.
in the beginning is MANDATORY. It causes the script to be run in the current environment, giving it the ability to change the current working directory usingcd