-
-
Save tbuehlmann/5195811 to your computer and use it in GitHub Desktop.
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
require 'popen4' | |
class Exec | |
attr_reader :stdout | |
attr_reader :stderr | |
attr_reader :status | |
attr_reader :failure | |
def initialize *args | |
@stdout = nil | |
@stderr = nil | |
@status = nil | |
@cmd = nil | |
if args.length == 1 | |
self.do args.shift | |
end | |
end | |
def do cmd | |
@stdout = nil | |
@stderr = nil | |
@status = nil | |
@cmd = cmd | |
if @cmd.length == 0 | |
puts "Cowardly refusing to execute empty command" | |
return | |
end | |
h = Hash.new | |
status = POpen4::popen4(cmd) do |stdout,stderr,stdin,pid| | |
stdin.close | |
@stdout = stdout.read.strip | |
@stderr = stderr.read.strip | |
end | |
@status = status.exitstatus | |
if @status == 0 | |
@failure = false | |
else | |
@failure = true | |
end | |
end | |
def to_s | |
messages = [] | |
messages << "Command '#{@cmd}' FAILED with exitstatus: '#{@status}'" | |
@stdout.each_line { |line| messages << "stdout: #{line}" } | |
@stderr.each_line { |line| messages << "stderr: #{line}" } | |
messages.each { |message| puts message } | |
(messages.join + "\n") | |
end | |
def failure? | |
@failure | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment