Class: ForemanMaintain::Utils::CommandRunner
- Inherits:
-
Object
- Object
- ForemanMaintain::Utils::CommandRunner
- Defined in:
- lib/foreman_maintain/utils/command_runner.rb
Overview
Wrapper around running a command
Instance Attribute Summary collapse
-
#command ⇒ Object
readonly
Returns the value of attribute command.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
Instance Method Summary collapse
- #execution_error ⇒ Object
- #exit_status ⇒ Object
-
#initialize(logger, command, options) ⇒ CommandRunner
constructor
A new instance of CommandRunner.
- #interactive? ⇒ Boolean
- #output ⇒ Object
- #run ⇒ Object
- #success? ⇒ Boolean
Constructor Details
#initialize(logger, command, options) ⇒ CommandRunner
Returns a new instance of CommandRunner.
10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/foreman_maintain/utils/command_runner.rb', line 10 def initialize(logger, command, ) .(:stdin, :hidden_patterns, :interactive, :valid_exit_statuses) [:valid_exit_statuses] ||= [0] @logger = logger @command = command @stdin = [:stdin] @hidden_patterns = Array([:hidden_patterns]).compact @interactive = [:interactive] @options = @valid_exit_statuses = [:valid_exit_statuses] raise ArgumentError, 'Can not pass stdin for interactive command' if @interactive && @stdin end |
Instance Attribute Details
#command ⇒ Object (readonly)
Returns the value of attribute command.
8 9 10 |
# File 'lib/foreman_maintain/utils/command_runner.rb', line 8 def command @command end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
8 9 10 |
# File 'lib/foreman_maintain/utils/command_runner.rb', line 8 def logger @logger end |
Instance Method Details
#execution_error ⇒ Object
53 54 55 56 57 58 |
# File 'lib/foreman_maintain/utils/command_runner.rb', line 53 def execution_error raise Error::ExecutionError.new(hide_strings(@command), exit_status, hide_strings(@stdin), @interactive ? nil : hide_strings(@output)) end |
#exit_status ⇒ Object
44 45 46 47 |
# File 'lib/foreman_maintain/utils/command_runner.rb', line 44 def exit_status raise 'Command not yet executed' unless defined? @exit_status @exit_status end |
#interactive? ⇒ Boolean
35 36 37 |
# File 'lib/foreman_maintain/utils/command_runner.rb', line 35 def interactive? @interactive end |
#output ⇒ Object
39 40 41 42 |
# File 'lib/foreman_maintain/utils/command_runner.rb', line 39 def output raise 'Command not yet executed' unless defined? @output @output end |
#run ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/foreman_maintain/utils/command_runner.rb', line 23 def run if logger logger.debug(hide_strings("Running command #{@command} with stdin #{@stdin.inspect}")) end if @interactive run_interactively else run_non_interactively end logger.debug("output of the command:\n #{hide_strings(output)}") if logger end |
#success? ⇒ Boolean
49 50 51 |
# File 'lib/foreman_maintain/utils/command_runner.rb', line 49 def success? @valid_exit_statuses.include? exit_status end |