CLI-based "V" in Rails MVC?

It is not easy to get any useful results from various searches on this concept - perhaps because it is a. Incorrect and / or b. unclear. Essentially, I would like to write an application that runs both a regular web application and a command line interface. I did this in the ancient past for sysadmin-y using Perl, but it did not have the joy of using Ruby / Rails.

I'm comfortable with Rails, and also use standalone Ruby for all CLI types. What I'm looking for are the best methods, if any, to extend a Rails application to provide CLI functionality.

Perhaps the answer is as simple as using a script / runner and making your own VC when using my Rails models ... This is what I planned to do, but I thought I would step back and sanity -View this approach. It’s hard for me to imagine how I use any element of the Rails controller, given that he married HTTP requests so much, but I often wonder what kind of smart people come with them.

Thanks for any helpful answers.

+4
source share
2 answers

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

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

+1
source

Another approach is that the web interface is laid out in the CLI for something. All that is worth doing is the CLI, and the web server simply calls the CLI for all its needs.

Schelling is a little expensive. If this affects performance, use popen to load the CLI only once per web session. Then you can program the commands (write to your stdin via the popen pipe) and get the results (read them from stdout via the popen pipe) if the CLI is not loading for each command. If the CLI is “I accept some arguments, do something and exit,” and then add a new “--stay-resident” mode to it or some other mode that switches it to the behavior that the web interface requires .

0
source

Source: https://habr.com/ru/post/1302232/


All Articles