class PeerCommander::Command
A single command to be executed, with access to results statuses, output, and timings
Attributes
Public Class Methods
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
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 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
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
# File lib/peer_commander/command.rb, line 74 def exit_code raise Errors::CommandNotExecutedError unless executed? return nil if exception status.exitstatus end
Return true if the command failed
# File lib/peer_commander/command.rb, line 47 def failed? !success? end
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
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
# 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