Module: SastBox::Runner

Included in:
Scanner
Defined in:
lib/sastbox-sdk/runner.rb

Instance Method Summary collapse

Instance Method Details

#command?(name) ⇒ Boolean

TODO: find a better way to do this



7
8
9
10
# File 'lib/sastbox-sdk/runner.rb', line 7

def command?(name)
  `which #{name}`
  $?.success?
end

#run_cmd(cmd) ⇒ Object



12
13
14
15
16
17
18
19
20
# 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) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# 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