Skip to content

Instantly share code, notes, and snippets.

@iongion
Created February 26, 2014 13:01
Show Gist options
  • Save iongion/9229105 to your computer and use it in GitHub Desktop.
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
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