Class: Appear::Runner

Inherits:
Service show all
Defined in:
lib/appear/runner.rb

Overview

Service for executing commands. Better than a mixin everywhere.

Direct Known Subclasses

RunnerRecorder

Instance Method Summary collapse

Methods inherited from BaseService

delegate, #initialize, require_service, required_services

Constructor Details

This class inherits a constructor from Appear::BaseService

Instance Method Details

#run(command, opts = {}) ⇒ String

Run a command. Throws an exception if the command fails. Command can either be a string, or an array of command name and parameters. Returns the combinded STDERR and STDOUT of the command.

Parameters:

  • command (String, Array)

    command to run, as an argv array, or as a sh command string.

  • opts (Hash) (defaults to: {})

    options

Options Hash (opts):

  • :allow_failure (Boolean) — default: false

    permit running the command to fail. Do not raise an ExecutionFailure error.

Returns:

  • (String)

Raises:



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/appear/runner.rb', line 33

def run(command, opts = {})
  allow_failure = opts[:allow_failure] || false
  start = Time.new
  if command.is_a? Array
    output, status = Open3.capture2e(*command)
  else
    output, status = Open3.capture2e(command)
  end
  finish = Time.new
  log("Runner: ran #{command.inspect} in #{finish - start}s")
  if !status.success? && !allow_failure
    raise ExecutionFailure.new(command, output)
  end
  output
end