Class: Reviewer::Shell::Result
- Inherits:
-
Object
- Object
- Reviewer::Shell::Result
- Defined in:
- lib/reviewer/shell/result.rb
Overview
Provides a structure interface for the results of running a command
Constant Summary collapse
- EXIT_STATUS_CODES =
{ success: 0, cannot_execute: 126, executable_not_found: 127, terminated: 130 }.freeze
- STD_ERROR_STRINGS =
Not all command line tools use the 127 exit status when an executable cannot be found, so this provides a home for recognizeable strings in those tools’ error messages that we can translate to the appropriate exit status for internal consistency
{ executable_not_found: "can't find executable" }.freeze
Instance Attribute Summary collapse
-
#exit_status ⇒ Object
Returns the value of attribute exit_status.
-
#status ⇒ Object
Returns the value of attribute status.
-
#stderr ⇒ Object
Returns the value of attribute stderr.
-
#stdout ⇒ Object
Returns the value of attribute stdout.
Instance Method Summary collapse
-
#cannot_execute? ⇒ Boolean
Determines whether a command simply cannot be executed.
-
#executable_not_found? ⇒ Boolean
Determines whether the command failed because the executable cannot be found.
- #exists? ⇒ Boolean
-
#initialize(stdout = nil, stderr = nil, status = nil) ⇒ self
constructor
An instance of a result from running a local command.
-
#rerunnable? ⇒ Boolean
Determines if re-running a command is entirely futile.
-
#to_s ⇒ String
Returns a string representation of the result.
Constructor Details
#initialize(stdout = nil, stderr = nil, status = nil) ⇒ self
An instance of a result from running a local command. Captures the values for ‘$stdout`,
`$stderr`, and the exit status of the command to provide a reliable way of interpreting
the results for commands that otherwise use these values inconsistently.
35 36 37 38 39 40 |
# File 'lib/reviewer/shell/result.rb', line 35 def initialize(stdout = nil, stderr = nil, status = nil) @stdout = stdout @stderr = stderr @status = status @exit_status = status&.exitstatus end |
Instance Attribute Details
#exit_status ⇒ Object
Returns the value of attribute exit_status.
21 22 23 |
# File 'lib/reviewer/shell/result.rb', line 21 def exit_status @exit_status end |
#status ⇒ Object
Returns the value of attribute status.
21 22 23 |
# File 'lib/reviewer/shell/result.rb', line 21 def status @status end |
#stderr ⇒ Object
Returns the value of attribute stderr.
21 22 23 |
# File 'lib/reviewer/shell/result.rb', line 21 def stderr @stderr end |
#stdout ⇒ Object
Returns the value of attribute stdout.
21 22 23 |
# File 'lib/reviewer/shell/result.rb', line 21 def stdout @stdout end |
Instance Method Details
#cannot_execute? ⇒ Boolean
Determines whether a command simply cannot be executed.
57 58 59 |
# File 'lib/reviewer/shell/result.rb', line 57 def cannot_execute? exit_status == EXIT_STATUS_CODES[:cannot_execute] end |
#executable_not_found? ⇒ Boolean
Determines whether the command failed because the executable cannot be found. Since this is an error that can be corrected fairly predictably and easily, it provides the ability to tailor the error guidance to help folks recover
67 68 69 70 |
# File 'lib/reviewer/shell/result.rb', line 67 def executable_not_found? exit_status == EXIT_STATUS_CODES[:executable_not_found] || stderr&.include?(STD_ERROR_STRINGS[:executable_not_found]) end |
#exists? ⇒ Boolean
42 43 44 |
# File 'lib/reviewer/shell/result.rb', line 42 def exists? [stdout, stderr, exit_status].compact.any? end |
#rerunnable? ⇒ Boolean
Determines if re-running a command is entirely futile. Primarily to help when a command fails within a batch and needs to be re-run to show the output
50 51 52 |
# File 'lib/reviewer/shell/result.rb', line 50 def rerunnable? exit_status < EXIT_STATUS_CODES[:cannot_execute] end |
#to_s ⇒ String
Returns a string representation of the result
75 76 77 78 79 80 81 |
# File 'lib/reviewer/shell/result.rb', line 75 def to_s result_string = '' result_string += stderr result_string += stdout result_string.strip end |