qrush / sub

a delicious way to organize programs
http://37signals.com/svn/posts/3264-automating-with-convention-introducing-sub
MIT License
1.74k stars 148 forks source link

cd from within sub #29

Closed cmalven closed 6 years ago

cmalven commented 11 years ago

This isn't so much an issue as a plea for assistance.

I'm working on a sub script that I can use for managing my various projects. One aspect of that involves navigating to the directory for a project.

I would use it like so:

cmal proj my-project

This command opens the project folder in Sublime Text, opens the dev url for the project in my browser, and ideally would cd into the directory for this project.

That last part is the tricky one, because the cd command is being run in a subshell, so once the cmal proj script finishes running I'm still in the same directory I started in.

Any tips on getting around this within the workings of sub?

In case it's not obvious, I'm a designer, not a command line guru. I know that normally I could get around this by using source my-script or . my-script but that's about all I know. Thanks.

mislav commented 11 years ago

If you want to change the state of you current shell, like set an environment variable or change directory, you can't call a command to do that (as you've already discovered). It has to be handled by a shell function or eval.

Fortunately, sub supports this via "sh-*" commands. Write a command as you normally would that starts with "sh-", and that command should output shell commands that will get evaled in your shell by sub.

Take a look at "cd" command in mislav/coral

cmalven commented 11 years ago

Fantastic! Thanks @mislav

Is there any way to prevent parts of my script from being evaled? For instance, my script drops into Ruby to do some side work and put things to the screen, and this seems to be screwing up my sh-proj command.

Alternatively, does sub let you call one of it's commands from within a sub command? For instance, could I call sh-cd from within proj?

mislav commented 11 years ago

The script can do many things. Only its output to stdout will be eval'ed. Make sure it only outputs the cd shell command after it does all the work!

No the sh- command needs to be directly called. If you call it from another command, the effect will be lost.

djtal commented 11 years ago

Hi

Thanks for the explain but I'dont understand how to cd to a directory in a sub command

Edit now it can cd. But I've lost autocompletion.

Here what I've tried so far

My command is name tech-sh-open.

# Provide tech completions                                                                                                                                                                                                                                                  
if [ "$1" = "--complete" ]; then                                                                                                                                                                                                                                            
  find ~/dev/{plugins,projets,r-et-d} -mindepth 0 -maxdepth 1 ! -name '.*'  -exec basename {} \;                                                                                                            
fi   dir=$(find ~/dev -mindepth 1 -maxdepth 2 -name "$1" | head -n 1)

if [ -e "$dir" ]; then
  { [ -d "$dir/trunk" ] && app="$dir/trunk"; } || { [ -d "$dir/webapp" ] && app="$dir/webapp"; }
  echo cd ${app}"
else
  echo "Aucun projet '$1' trouvé..." >&2
  exit 1
fi

Any hints on the possible error ?

djtal commented 11 years ago

searching through github lead me to

cybera/sub@bd1c3eb073a59ca7a1a421e895b62db95887362 which seem to allow autcompleting sh-* commands. It work on my command

jeffreyroberts commented 11 years ago

I was able to do this by

cd $dir bash

of course you have to type exit to end the execution of the script