Created
June 22, 2015 21:34
-
-
Save chorn/809d73ce4cf7ad04cd42 to your computer and use it in GitHub Desktop.
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
require 'open3' | |
module Derp | |
def self.system(*args) | |
raise "No arguments specified." unless args && args.class == Array && args.size > 0 | |
# This style of calling system allows for redirection and pipes, where the normal array style does not. | |
command = (args.flatten.compact.map{|a| a.match(/ /) ? "\"#{a}\"" : a }).join(' ') | |
puts command if $DEBUG | |
stdin_handle, stdout_handle, stderr_handle, wait_thread = Open3::popen3 command | |
pid = wait_thread.pid | |
stdin_handle.close | |
status = wait_thread.value | |
stdout = stdout_handle.read | |
stderr = stderr_handle.read | |
stdout_handle.close | |
stderr_handle.close | |
unless status.success? | |
puts "The following system call failed with return code #{ status.exitstatus }" | |
puts command | |
puts "pid : #{ pid }" | |
puts "stdout : #{ stdout }" | |
puts "stderr : #{ stderr }" | |
end | |
Status.new(pid, stdout, stderr, status.exitstatus, status.success?) | |
end | |
class Status | |
attr_reader :pid, :stdout, :stderr, :exitstatus, :success | |
def initialize(pid, stdout, stderr, exitstatus, success) | |
@pid = pid | |
@stdout = stdout | |
@stderr = stderr | |
@exitstatus = exitstatus | |
@success = success | |
end | |
def success? | |
@success | |
end | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment