class SimCtl::Executor

Public Class Methods

execute(command) { |parse| ... } click to toggle source
# 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

Private Class Methods

looks_like_json?(output) click to toggle source
# File lib/simctl/executor.rb, line 27
def looks_like_json?(output)
  output.start_with?('[', '{')
end