Class: ForemanMaintain::Utils::CommandRunner

Inherits:
Object
  • Object
show all
Defined in:
lib/foreman_maintain/utils/command_runner.rb

Overview

Wrapper around running a command

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(logger, command, options) ⇒ CommandRunner

Returns a new instance of CommandRunner.

Raises:

  • (ArgumentError)


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, options)
  options.validate_options!(:stdin, :hidden_patterns, :interactive, :valid_exit_statuses)
  options[:valid_exit_statuses] ||= [0]
  @logger = logger
  @command = command
  @stdin = options[:stdin]
  @hidden_patterns = Array(options[:hidden_patterns])
  @interactive = options[:interactive]
  @options = options
  @valid_exit_statuses = options[:valid_exit_statuses]
  raise ArgumentError, 'Can not pass stdin for interactive command' if @interactive && @stdin
end

Instance Attribute Details

#commandObject (readonly)

Returns the value of attribute command.



8
9
10
# File 'lib/foreman_maintain/utils/command_runner.rb', line 8

def command
  @command
end

#loggerObject (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_errorObject



51
52
53
54
55
56
# File 'lib/foreman_maintain/utils/command_runner.rb', line 51

def execution_error
  raise Error::ExecutionError.new(hide_strings(@command),
                                  exit_status,
                                  hide_strings(@stdin),
                                  @interactive ? nil : hide_strings(@output))
end

#exit_statusObject



42
43
44
45
# File 'lib/foreman_maintain/utils/command_runner.rb', line 42

def exit_status
  raise 'Command not yet executed' unless defined? @exit_status
  @exit_status
end

#interactive?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/foreman_maintain/utils/command_runner.rb', line 33

def interactive?
  @interactive
end

#outputObject



37
38
39
40
# File 'lib/foreman_maintain/utils/command_runner.rb', line 37

def output
  raise 'Command not yet executed' unless defined? @output
  @output
end

#runObject



23
24
25
26
27
28
29
30
31
# File 'lib/foreman_maintain/utils/command_runner.rb', line 23

def run
  logger.debug(hide_strings("Running command #{@command} with stdin #{@stdin.inspect}"))
  if @interactive
    run_interactively
  else
    run_non_interactively
  end
  logger.debug("output of the command:\n #{hide_strings(output)}")
end

#success?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/foreman_maintain/utils/command_runner.rb', line 47

def success?
  @valid_exit_statuses.include? exit_status
end