postmodern/chruby — pull request #126 by ravigadad (2013-04-06 21:30:09+00:00)
Title: If no RUBY specified for chruby-exec, fall back to .ruby-version file

Great project; thank you so much for maintaining it. Our dev team drove ops insane (well, more insane) by sledgehammering the environment first with RVM, then rbenv. We'd finally caught a breath of uncluttered air with rbfu when it was discontinued in favor of chruby - but that was a welcome change, since we were about to fork rbfu to fix some issues which you've handled even more cleanly.

One thing we do miss about rbfu, however, is the ability for the execution wrapper (`chruby-exec`, in this case) to use a .ruby-version file when no RUBY is specified on the command line. Our thin init scripts did something like this:

```bash
cd ${APPDIR} && rbfu bundle exec thin -C ${CONF}
```

...and with chruby, that became a bit of a quandary. Sourcing the auto.sh script doesn't work for the exact command above even in a login shell, since the PROMPT_COMMAND obviously won't re-execute after the `&&` - that said, we'd like to avoid the requirement for auto-switching for our deployment user accounts anyway.

With chruby-exec, we'd have to do this:

```bash
cd ${APPDIR} && chruby-exec ${RUBY_VERSION} -- bundle exec thin -C ${CONF}
```

...which, while doable, requires us to source the .ruby-version file in the deployment script, or have duplication of version specification. Therefore, I forked so we could follow our current convention, and figured you might at least want to take a look. Now we can do the following, as long as there's a .ruby-version file in $APPDIR:

```bash
cd ${APPDIR} && chruby-exec -- bundle exec thin -C ${CONF}
```

I don't know if this goes against your design principles, or if there should at least be a configurable option to allow/disallow hunting for a .ruby-version file using `chruby-exec`, but I figured since it was something we were using extensively with rbfu (a similarly barebones approach to the problem), it might come in handy for other chruby users as well.

I'm not an experienced shell programmer, so I may have made some obvious mistakes, but hopefully it's at least close. I thought about DRYing up the hunt for a .ruby-version file so it's not duplicated in auto.sh and the new function I added to chruby-exec, but the refactoring would probably introduce more complexity than it's worth (since auto.sh's is so dependent on the RUBY_VERSION_FILE env variable). Maybe there's a relatively clean way of doing so, but again - shell scripting isn't a forte.

I did also make a small tweak to the chruby-exec test file to reduce false positives depending on developer setup, but possibly a resolution such as that proposed in https://github.com/postmodern/chruby/issues/95 would render that tweak unnecessary.

Again, thanks for this remarkably sane piece of software, from our dev team and especially our inexplicably-not-completely-bald-yet ops team.