Skip to content

Instantly share code, notes, and snippets.

@tbuehlmann
Forked from TvL2386/gist:5195772
Last active December 15, 2015 03:39
Show Gist options
  • Save tbuehlmann/5195811 to your computer and use it in GitHub Desktop.
Save tbuehlmann/5195811 to your computer and use it in GitHub Desktop.
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