Created
February 26, 2014 13:01
-
-
Save iongion/9229105 to your computer and use it in GitHub Desktop.
Shell execution with chain support, break on error, break if invoked command does not exist, print error message in red :P
This file contains 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
def shell(*cmds) | |
output = "" | |
cmds.each do |cmd| | |
begin | |
stop = false | |
stdout_str, stderr_str, status = Open3.capture3(cmd) | |
output = "#{output}#{stdout_str}#{stderr_str}" | |
if status.success? | |
context = "#{cmd}\n#{stdout_str}" | |
else | |
context = "#{cmd}\n\033[31m#{stderr_str}\033[0m" | |
stop = true | |
end | |
rescue => detail | |
context = "#{cmd}\n\033[31m#{detail.message}\033[0m" | |
stop = true | |
end | |
puts context | |
break if stop | |
end | |
output.lines.map(&:chomp) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment