Class: ProcessExecuter::Command::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/process_executer/command/runner.rb

Overview

The Runner class executes subprocess commands and captures their status and output.

It does the following:

  • Run commands (call) with options for capturing output, handling timeouts, and merging stdout/stderr.
  • Process command results, including logging and error handling.
  • Raise detailed exceptions for common command failures, such as timeouts or subprocess errors.

This class is used internally by ProcessExecuter.run.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(logger) ⇒ Runner

Create a new RunCommand instance

Examples:

runner = Runner.new()
status = runner.call('echo', 'hello')

Parameters:

  • logger (Logger)

    The logger to use. Defaults to a no-op logger if nil.



28
29
30
# File 'lib/process_executer/command/runner.rb', line 28

def initialize(logger)
  @logger = logger || Logger.new(nil)
end

Instance Attribute Details

#loggerLogger (readonly)

The logger to use

Examples:

runner.logger #=> #<Logger:0x00007f9b1b8b3d20>

Returns:

  • (Logger)


36
37
38
# File 'lib/process_executer/command/runner.rb', line 36

def logger
  @logger
end

Instance Method Details

#call(*command, out: nil, err: nil, merge: false, raise_errors: true, **options_hash) ⇒ ProcessExecuter::Command::Result

Run a command and return the status including stdout and stderr output

Examples:

command = %w[git status]
status = run(command)
status.success? # => true
status.exitstatus # => 0
status.out # => "On branch master\nnothing to commit, working tree clean\n"
status.err # => ""

Parameters:

  • command (Array<String>)

    The command to run

  • out (#write) (defaults to: nil)

    The object to which stdout is written

  • err (#write) (defaults to: nil)

    The object to which stderr is written

  • merge (Boolean) (defaults to: false)

    Write both stdout and stderr into the buffer for stdout

  • raise_errors (Boolean) (defaults to: true)

    Raise an exception if the command fails

  • options_hash (Hash)

    Additional options to pass to Process.spawn

    See ProcessExecuter.run for a full list of options.

Returns:



61
62
63
64
65
66
67
68
# File 'lib/process_executer/command/runner.rb', line 61

def call(*command, out: nil, err: nil, merge: false, raise_errors: true, **options_hash)
  out ||= StringIO.new
  err ||= (merge ? out : StringIO.new)

  status = spawn(command, out:, err:, **options_hash)

  process_result(command, status, out, err, options_hash[:timeout], raise_errors)
end