Skip to content

Instantly share code, notes, and snippets.

@corny
Last active August 29, 2015 13:58
Show Gist options
  • Save corny/9935984 to your computer and use it in GitHub Desktop.
Save corny/9935984 to your computer and use it in GitHub Desktop.
# A wrapper for IO.popen that returns the combined stdout and stderr.
# An exception is thrown if the subprocess exists with non-zero.
# by Julian Kornberger
module Subprocess
class Error < ::StandardError
attr_reader :status, :output
def initialize(args, status, output)
super "Subprocess #{args.inspect} exited with status #{status}:\n#{output}"
@status = status
@output = output
end
end
# Runs a command
# The optional hash contains environment variables
def self.run(*args)
env = args.extract_options!.stringify_keys
args = args.map(&:to_s)
output = IO.popen env, args, err: [:child, :out], &:read
status = $?.exitstatus
if status != 0
raise Error.new args, status, output
end
output
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment