module SastBox::Runner

Public Instance Methods

command?(name) click to toggle source

TODO: find a better way to do this

# File lib/sastbox-sdk/runner.rb, line 7
def command?(name)
  `which #{name}`
  $?.success?
end
run_cmd(cmd) click to toggle source
# File lib/sastbox-sdk/runner.rb, line 12
def run_cmd(cmd)
  print_debug(cmd) if @opts.verbose
  if command?(cmd[0])
    run_cmd_with_timeout(cmd)
  else
    print_error("Command not found: #{cmd[0]}")
    exit 1
  end
end
run_cmd_with_timeout(cmd) click to toggle source
# File lib/sastbox-sdk/runner.rb, line 22
def run_cmd_with_timeout(cmd)
  out_reader = ''
  err_reader = ''
  Open3.popen3(*cmd) do |stdin, stdout, stderr, wait_thr|
    # https://stackoverflow.com/questions/8952043/how-to-fix-hanging-popen3-in-ruby
    stdin.close_write
    output, pid = [], wait_thr.pid
    begin
      Timeout.timeout(@opts.timeout) do
        begin
          err_reader = Thread.new { stderr.read }
        rescue IOError
        end

        out_reader = stdout.read

        #output = [stdout.read, stderr.read]
        Process.wait(pid)
      end
    rescue Errno::ECHILD
    rescue Timeout::Error
      print_error('Timed out - Skipping...')
      Process.kill('HUP', pid)
      exit 1
    end
    [out_reader, wait_thr.value]
  end
end