class PeerCommander::Command

A single command to be executed, with access to results statuses, output, and timings

Attributes

command[R]
command_output[R]
env[R]
exception[R]
opts[R]
status[R]
stdin_data[R]
timing[R]

Public Class Methods

new(command, stdin_data: nil, env: {}, opts: {}) click to toggle source

The env specified will be passed directly to Open3.capture2e as the env to run with The opts provided will also be passed directly to Open3.capture2e as opts stdin_data will also be passed directly to Open3.capture2e

# File lib/peer_commander/command.rb, line 11
def initialize(command, stdin_data: nil, env: {}, opts: {})
  @command = command
  @stdin_data = stdin_data
  @env = env
  @opts = opts
end

Public Instance Methods

duration() click to toggle source

How long the command took to run in seconds

# File lib/peer_commander/command.rb, line 59
def duration
  raise Errors::CommandNotExecutedError unless executed?

  timing
end
execute() click to toggle source

Execute the command, will capture any exceptions that inherit from StandardError raised by the execution of the given command and store it in the exception attribute, NOTE: This means it _will not_ propogate the exception upwards

Returns self

# File lib/peer_commander/command.rb, line 23
def execute
  raise Errors::CommandAlreadyExecutedError if executed?

  start_time = Time.now

  begin
    @command_output, @status = Open3.capture2e(env, command, execution_options)
  rescue StandardError => e
    @exception = e
  ensure
    @timing = Time.now - start_time
  end

  self
end
executed?() click to toggle source

Returns a truthy object if the command has been executed, or nil if it has not.

If the command has been executed it will either return the status code or the exception that was raised

# File lib/peer_commander/command.rb, line 70
def executed?
  exception || status
end
exit_code() click to toggle source
# File lib/peer_commander/command.rb, line 74
def exit_code
  raise Errors::CommandNotExecutedError unless executed?

  return nil if exception

  status.exitstatus
end
failed?() click to toggle source

Return true if the command failed

# File lib/peer_commander/command.rb, line 47
def failed?
  !success?
end
output() click to toggle source

Return the output (stdout and stderr interleaved) of running the command

# File lib/peer_commander/command.rb, line 52
def output
  raise Errors::CommandNotExecutedError unless executed?

  command_output
end
success?() click to toggle source

Return true if the command was successful

# File lib/peer_commander/command.rb, line 40
def success?
  raise Errors::CommandNotExecutedError unless executed?

  exception.nil? && status.success?
end

Private Instance Methods

execution_options() click to toggle source
# File lib/peer_commander/command.rb, line 86
def execution_options
  opts.tap do |options|
    options[:stdin_data] = stdin_data unless stdin_data.nil?
  end
end