Skip to content

Instantly share code, notes, and snippets.

@farnasirim
Last active November 2, 2017 09:28
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save farnasirim/fa4f7ab61a6944e214df6b9fd6525cd9 to your computer and use it in GitHub Desktop.
Save farnasirim/fa4f7ab61a6944e214df6b9fd6525cd9 to your computer and use it in GitHub Desktop.
navigate to a go project directory easily using `goto` command
#!/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
@farnasirim
Copy link
Author

farnasirim commented Jul 15, 2017

Usage:

  • Download the above script (goto.sh):
    wget https://goo.gl/BMfbny
    
  • Add the following line to your ~/.zshrc or ~/.bashrc if you're not hanging out with the cool kids yet:
    alias goto=". /path_to_go_to/goto.sh"
    
    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 using cd
  • Now you can do
    $ goto github      # cd $GOPATH/src/github.com
    $ goto colonelmo   # cd $GOPATH/src/github.com/colonelmo
    $ goto cobra       # cd $GOPATH/src/github.com/spf13/cobra 
    

@remohammadi
Copy link

remohammadi commented Jul 15, 2017

exit 0 instead of return in usage clause. 😃

@farnasirim
Copy link
Author

@remohammadi I'll let you try that to find out why it would not work.

@skx
Copy link

skx commented Jul 15, 2017

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.)

@arastu
Copy link

arastu commented Jul 15, 2017

Very helpful 👍

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