Class: PeerCommander::Command
- Inherits:
-
Object
- Object
- PeerCommander::Command
- Defined in:
- lib/peer_commander/command.rb
Overview
A single command to be executed, with access to results statuses, output, and timings
Instance Attribute Summary collapse
-
#command ⇒ Object
readonly
Returns the value of attribute command.
-
#env ⇒ Object
readonly
Returns the value of attribute env.
-
#exception ⇒ Object
readonly
Returns the value of attribute exception.
-
#opts ⇒ Object
readonly
Returns the value of attribute opts.
-
#stdin_data ⇒ Object
readonly
Returns the value of attribute stdin_data.
Instance Method Summary collapse
-
#duration ⇒ Object
How long the command took to run in seconds.
-
#execute ⇒ Object
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.
-
#executed? ⇒ Boolean
Returns a truthy object if the command has been executed, or nil if it has not.
- #exit_code ⇒ Object
-
#failed? ⇒ Boolean
Return true if the command failed.
-
#initialize(command, stdin_data: nil, env: {}, opts: {}) ⇒ Command
constructor
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.
-
#output ⇒ Object
Return the output (stdout and stderr interleaved) of running the command.
-
#success? ⇒ Boolean
Return true if the command was successful.
Constructor Details
#initialize(command, stdin_data: nil, env: {}, opts: {}) ⇒ Command
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
11 12 13 14 15 16 |
# 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 |
Instance Attribute Details
#command ⇒ Object (readonly)
Returns the value of attribute command.
6 7 8 |
# File 'lib/peer_commander/command.rb', line 6 def command @command end |
#env ⇒ Object (readonly)
Returns the value of attribute env.
6 7 8 |
# File 'lib/peer_commander/command.rb', line 6 def env @env end |
#exception ⇒ Object (readonly)
Returns the value of attribute exception.
6 7 8 |
# File 'lib/peer_commander/command.rb', line 6 def exception @exception end |
#opts ⇒ Object (readonly)
Returns the value of attribute opts.
6 7 8 |
# File 'lib/peer_commander/command.rb', line 6 def opts @opts end |
#stdin_data ⇒ Object (readonly)
Returns the value of attribute stdin_data.
6 7 8 |
# File 'lib/peer_commander/command.rb', line 6 def stdin_data @stdin_data end |
Instance Method Details
#duration ⇒ Object
How long the command took to run in seconds
59 60 61 62 63 |
# File 'lib/peer_commander/command.rb', line 59 def duration raise Errors::CommandNotExecutedError unless executed? timing end |
#execute ⇒ Object
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
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# 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, ) rescue StandardError => e @exception = e ensure @timing = Time.now - start_time end self end |
#executed? ⇒ Boolean
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
70 71 72 |
# File 'lib/peer_commander/command.rb', line 70 def executed? exception || status end |
#exit_code ⇒ Object
74 75 76 77 78 79 80 |
# File 'lib/peer_commander/command.rb', line 74 def exit_code raise Errors::CommandNotExecutedError unless executed? return nil if exception status.exitstatus end |
#failed? ⇒ Boolean
Return true if the command failed
47 48 49 |
# File 'lib/peer_commander/command.rb', line 47 def failed? !success? end |
#output ⇒ Object
Return the output (stdout and stderr interleaved) of running the command
52 53 54 55 56 |
# File 'lib/peer_commander/command.rb', line 52 def output raise Errors::CommandNotExecutedError unless executed? command_output end |
#success? ⇒ Boolean
Return true if the command was successful
40 41 42 43 44 |
# File 'lib/peer_commander/command.rb', line 40 def success? raise Errors::CommandNotExecutedError unless executed? exception.nil? && status.success? end |