Last active
January 22, 2016 19:08
-
-
Save jjb/906332 to your computer and use it in GitHub Desktop.
runix: The ruby shell command executor you've always wanted.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Ruby + UNIX = runix | |
# | |
# If return status is 0, returns standard out. | |
# If return status is non-0, raises an exception with standard error in the message. | |
# (None of the other methods you know about have this behavior) | |
# | |
# Great overview of all ruby shell methods: | |
# http://tech.natemurray.com/2007/03/ruby-shell-commands.html | |
# | |
# dependency: open4 gem | |
require 'open4' | |
def runix(command) | |
standard_error_message=nil | |
out="" | |
status = Open4::popen4(command) do |p, sin, sout, serr| | |
standard_error_message=serr.read | |
out = sout.read | |
end | |
# Open4.popen4 will raise an exception in some non-0 return states, but not all | |
unless 0 == status | |
raise "The shell command:\n\n#{command}\n\nfailed with status #{status} and this message:\n\n#{standard_error_message}\n\n" | |
end | |
out | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
/cc @ahoward :-)