Make Zeus work from any subdirectory of Rails.root

May 17, 2013

I’ve started to use Zeus in my projects to speed up rails console and tests.
I’m also a big user of the command line and tend to end up in sub directories such as app/models, etc. I’d forget and type zeus console and would get the error “Can’t connect to master. Run zeus start first.” This is because zeus tries to connect to .zeus.sock in the current directory.

My solution is a small Bash function:

function z {
  cwd=$PWD
  while [[ ! -f "$cwd/Rakefile" && \
           ! -f "$cwd/Gemfile" && \
           ! -d "$cwd/app" && \
           ! -d "$cwd/config" && \
           ! -d "$cwd/db" && \
           "$cwd" != "/Users/philip/Work" ]]
  do
    cwd=${cwd%\/*}
  done
  (cd $cwd && zeus $*)
}

Note: The last condition matches the directory that I put all of my applications. You’ll want to change that or remove it entirely. It’s not strictly necessary, but it does stop the method from potentially looping forever.

Now I can just type z console from any sub directory in the project and it does what I want. It also has the advantage that I can’t reliably spell ‘zeus’ to save my life, but ‘z’ is pretty easy.

Update: I’m told the latest version of zeus solves this problem by allowing you to specify the location of the socket file via an ENV variable.