Class: SimCtl::Executor

Inherits:
Object
  • Object
show all
Defined in:
lib/simctl/executor.rb

Class Method Summary collapse

Class Method Details

.execute(command) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/simctl/executor.rb', line 7

def execute(command)
  command = command.flatten.join(' ')
  $stderr.puts command if ENV['SIMCTL_DEBUG']
  Open3.popen3(command) do |_stdin, stdout, stderr, result|
    output = stdout.read
    if result.value.to_i > 0
      output = stderr.read if output.empty?
      raise output
    end
    return unless block_given?
    if looks_like_json?(output)
      yield JSON.parse(output)
    else
      yield output.chomp
    end
  end
end