DEV Community

Cover image for Shell command execution via Ruby; Fury the first line of CLI defence
Ahsan Nabi Dar
Ahsan Nabi Dar

Posted on

Shell command execution via Ruby; Fury the first line of CLI defence

Originally Posted https://darnahsan.medium.com/shell-command-execution-via-ruby-fury-the-first-line-of-cli-defence-20f5e248e255 Published on May 22, 2020

We all have dabbed command line once or twice. Using terminal to run commands makes you feels like super human when there is no GUI.

When working with multiple tools you might not find support for each tool in the shape of a library, SDK or API for the language you want to work in. Each language has built its niche such as Python is known for Data Science, Ruby for scripting and web with Rails or Java for enterprise. They all are equally capable to do any job i.e take input and provide output. You will find alot of the tools are written and their interface on the Unix/Linux environment is shared as Command Line, one such popular example is cURL. Did you know cURL is based on libcurl which is written in C ? Now how do you use a library written in C with Ruby ? Create A Ruby C extension , map all the features and maintain it ? Or just use the command line interface of the tool and execute commands from your code. This just ain’t needed only for executing some 3rd party tools, you might want to monitor your system , manipulate files, check for running porcesses and handle them. The Unix/Linux environment has tools that are rock solid when it comes to doing their job. They are battle tested and there are no better alternatives to tools such as grep tail sed awk less ps | and many more.

Over time you come in to situations where using CLI tools to accessing services are better suited as there might not be an officially supported library or a feature complete library available. As an example at a certain time AWS and Google Cloud didn’t have great ruby support for its cloud libraries and many others follow the same route as Ruby is now more favoured as a web language due to the popularity of Rails.

I got the idea to script the usage of executing shell commands via Ruby when I had to work with Git and Xcode build tools. In Ruby there are a few ways to execute shell comands such as exec, system and %x() or *Backticksc *but the simplest is to use backticks. The purpose was to run multiple shell commands and get its output. It would allow you to setup chain of commands need to be executed and also give a standard interface to run shell commands without worrying about different ruby syntax to execute command and capture the output and use them repeatedly or extend them with more commands. so how to use this Ruby Gem a.k.a Fury.

GitHub logo ahsandar / fury

(Nick) Fury humanity's first line of defense against command line usage

Github repo is a mirror of a Gitlab repo

(Nick) Fury humanity's first line of defense against command line usage

Its a gem version of Command Service to run multiple shell commands and get its output. It allows you to setup chain of commands need to be executed and also give a standard interface to run shell commands without worrying about different ruby syntax to execute command and capture the output.

Usage

require 'fury'

fury = Fury.new('echo Hell knows no Fury')
fury.execute # executes command and can be used again
fury.reset_cmd! # Used to clear command queue
fury = Fury.new('echo gonna have to ask you to exit the donut')
fury.execute! # Resets the command queue after execution to use for new command
# Execute command without creating a command queue
Fury.run_now('echo Director Fury is no longer in command. Override order 7-Alpha-1-1.')
#commands can be added to the queue for execution

Usage

fury = Fury.new('echo Hell knows no Fury')
fury.execute # executes command and can be used again

fury.reset_cmd! # Used to clear command queue

fury = Fury.new('echo gonna have to ask you to exit the donut')
fury.execute! # Resets the command queue after execution to use for new command

#commands can be added to the queue for execution ,
#commands are combined using ';' for shell execution

fury = Fury.new('echo Hell knows no Fury')
fury.execute
fury << 'echo gonna have to ask you to exit the donut'
fury << 'echo Director Fury is no longer in command. Override order 7-Alpha-1-1.'
fury.execute!

#commands can be added to the queue for execution ,
#commands can be combined using desired separator for
#shell execution

fury = Fury.new('echo Hell knows no Fury')
fury.execute
fury.join('&')
fury.queue 'echo gonna have to ask you to exit the donut'
fury.join('&')
fury.queue 'echo Director Fury is no longer in command. Override order 7-Alpha-1-1.'
fury.execute!
Enter fullscreen mode Exit fullscreen mode

So here is some basic usage for the Gem, it can be used to run any tool that is available on you command line. I know there are dime a dozen gems available that do command line magic and are more advanced and probably have superb features but the purpose of this gem isn’t to replace any of them but to have a simple interface to running shell commands and capture output to do further prcessing without any overhead of dependencies. That is why I named it Fury in the world of Avengers someone with no super powers delivers what’s needed.

Top comments (0)