Class: ProductTaxonomy::CommandExecutor

Inherits:
Object
  • Object
show all
Defined in:
lib/product_taxonomy/command_executor.rb

Constant Summary collapse

DEFAULT_RUNNER =
Open3.method(:capture3)

Instance Method Summary collapse

Constructor Details

#initialize(command_runner: DEFAULT_RUNNER) ⇒ CommandExecutor

Returns a new instance of CommandExecutor.



9
10
11
# File 'lib/product_taxonomy/command_executor.rb', line 9

def initialize(command_runner: DEFAULT_RUNNER)
  @command_runner = command_runner
end

Instance Method Details

#capture(*command, chdir:, failure_message:) ⇒ Object



23
24
25
26
27
# File 'lib/product_taxonomy/command_executor.rb', line 23

def capture(*command, chdir:, failure_message:)
  @command_runner.call(*command, chdir:)
rescue SystemCallError => error
  raise "#{failure_message} #{error.message}"
end

#run!(*command, chdir:, failure_message:) ⇒ Object



13
14
15
16
17
18
19
20
21
# File 'lib/product_taxonomy/command_executor.rb', line 13

def run!(*command, chdir:, failure_message:)
  stdout, stderr, status = capture(*command, chdir:, failure_message:)
  return stdout if status.success?

  details = stderr.strip
  details = stdout.strip if details.empty?
  message = details.empty? ? failure_message : "#{failure_message} #{details}"
  raise message
end