views:

87

answers:

2

Having a hard time getting any useful results from various searches on this concept -- probably because it's a. Wrong and/or b. obscure. Essentially, though, I'd like to write an application which works as either a normal web app or with a command-line interface. I've done this in the ancient past for sysadmin-y stuff using Perl, but that had none of the joy that using Ruby/Rails brings.

I am comfortable enough with Rails itself, and also use standalone Ruby for all manner of CLI stuff. What I'm looking for is best practices, if they exist, for extending a Rails application to have CLI functionality.

Perhaps the answer is as simple as using script/runner and doing my own "VC" while using my Rails models... This is what I was planning on doing, but I thought I'd step back and sanity-check that approach first. I'm having a hard time imagining how I'd utilize any of the Rails controller stuff, given that it's so tightly married to HTTP requests, but I'm often surprised by what clever(er) folks come up with.

Thanks for any useful responses.

+1  A: 

I think it all depends on whether you want to reuse your controller logic. If you do then you can go down the route of writing a gem/Rake task/standalone Ruby script that makes HTTP requests to the application and receives the responses as JSON/XML/plain text or whatever. Something like HTTParty is ideal for this.

The second alternative is as you describe: drive your Rails models directly from your own script and present the results.

John Topley
Yeah, I forgot to note that I have done what you've described (CLI util using ActiveResource to talk to my Rails app) too. That is in production now, but after a year or so of living with it I'm reasonably certain that it's the Wrong Way To Do It. Simple requests are OK, but there's a lot of complex stuff being done client-side which really shouldn't be. Thanks for the response.
mjmac
A: 

Another approach is that the web interface shells out to the CLI to do anything. Everything worth doing is in the CLI and the web just calls the CLI for all of its needs.

Shelling is a little bit expensive. If it turns out to hurt performance, use popen to load the CLI just once per web session. Then you can feed it commands (write to its stdin via the popen pipe) and get the results (read from its stdout via the popen pipe) without the CLI having to load for each command. If the CLI is of the "I take some arguments, do something, and exit" sort, then add a new mode to it "--stay-resident" or some-such, that switches it to the behavior that the web interface needs.

Wayne Conrad
This might work for very simple applications, maybe. The application I want to extend has a lot going on in the Rails layer, though. Driving the CLI from the web would probably result in a lot of duplicate functionality and also gets into some tricky security issues.
mjmac
You're modifying an existing rails app? That does change things. I had the same sort of job to do. To avoid duplication, I just moved all the "do something" code out of rails and into the CLI. As you say, that's not good for you.
Wayne Conrad