class R10K::Util::Subprocess

The subprocess namespace implements an interface similar to childprocess. The interface has been simplified to make it easier to use and does not depend on native code.

@api private

Attributes

argv[R]

@!attribute [r] argv

@return [Array<String>] The command to be executed
cwd[RW]

@!attribute [rw] cwd

@return [String] The directory to be used as the cwd when executing
  the command.
logger[W]

@!attribute [w] logger

Allow calling processes to take ownership of execution logs by passing
their own logger to the command being executed.
raise_on_fail[RW]

@!attribute [rw] #raise_on_fail

Determine whether #execute raises an error when the command exits
with a non-zero exit status.
@return [true, false]

Public Class Methods

new(argv) click to toggle source

Prepare the subprocess invocation.

@param argv [Array<String>] The argument vector to execute

# File lib/r10k/util/subprocess.rb, line 53
def initialize(argv)
  @argv = argv

  @raise_on_fail = false
end
runner() click to toggle source

@return [Class < R10K::Util::Subprocess::Runner]

# File lib/r10k/util/subprocess.rb, line 18
def self.runner
  if R10K::Util::Platform.windows?
    R10K::Util::Subprocess::Runner::Windows
  elsif R10K::Util::Platform.jruby?
    R10K::Util::Subprocess::Runner::JRuby
  else
    R10K::Util::Subprocess::Runner::POSIX
  end
end

Public Instance Methods

execute() click to toggle source

Execute the given command and return the result of evaluation.

@api public @raise [R10K::Util::Subprocess::SubprocessError] if #raise_on_fail is

true and the command exited with a non-zero status.

@return [R10K::Util::Subprocess::Result]

# File lib/r10k/util/subprocess.rb, line 65
def execute
  subprocess = self.class.runner.new(@argv)
  subprocess.cwd = @cwd if @cwd

  logmsg = "Starting process: #{@argv.inspect}"
  logmsg << "(cwd: #{@cwd})" if @cwd
  logger.debug2(logmsg)

  result = subprocess.run
  logger.debug2("Finished process:\n#{result.format}")

  if @raise_on_fail && result.failed?
    raise SubprocessError.new("Command exited with non-zero exit code", :result => result)
  end

  result
end