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 ⇒ Integer?
The exit status code from the command.
-
#status ⇒ Object
Returns the value of attribute status.
-
#stderr ⇒ String?
Standard error from the command.
-
#stdout ⇒ String?
Standard output from the command.
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.
44 45 46 47 48 49 |
# File 'lib/reviewer/shell/result.rb', line 44 def initialize(stdout = nil, stderr = nil, status = nil) @stdout = stdout @stderr = stderr @status = status @exit_status = status&.exitstatus end |
Instance Attribute Details
#exit_status ⇒ Integer?
Returns the exit status code from the command.
29 |
# File 'lib/reviewer/shell/result.rb', line 29 attr_reader :stdout, :stderr, :status |
#status ⇒ Object
Returns the value of attribute status.
29 |
# File 'lib/reviewer/shell/result.rb', line 29 attr_reader :stdout, :stderr, :status |
#stderr ⇒ String?
Returns standard error from the command.
29 |
# File 'lib/reviewer/shell/result.rb', line 29 attr_reader :stdout, :stderr, :status |
#stdout ⇒ String?
Returns standard output from the command.
29 30 31 |
# File 'lib/reviewer/shell/result.rb', line 29 def stdout @stdout end |
Instance Method Details
#cannot_execute? ⇒ Boolean
Determines whether a command simply cannot be executed.
62 |
# File 'lib/reviewer/shell/result.rb', line 62 def cannot_execute? = exit_status == EXIT_STATUS_CODES[:cannot_execute] |
#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
70 71 72 73 |
# File 'lib/reviewer/shell/result.rb', line 70 def executable_not_found? exit_status == EXIT_STATUS_CODES[:executable_not_found] || stderr&.include?(STD_ERROR_STRINGS[:executable_not_found]) end |
#exists? ⇒ Boolean
51 |
# File 'lib/reviewer/shell/result.rb', line 51 def exists? = [stdout, stderr, exit_status].compact.any? |
#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
57 |
# File 'lib/reviewer/shell/result.rb', line 57 def rerunnable? = exit_status < EXIT_STATUS_CODES[:cannot_execute] |
#to_s ⇒ String
Returns a string representation of the result
78 79 80 |
# File 'lib/reviewer/shell/result.rb', line 78 def to_s [stderr, stdout].compact.join("\n").strip end |