Last active
August 29, 2015 13:58
-
-
Save corny/9935984 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
# 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