Module: SastBox::Runner

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

Instance Method Summary collapse

Instance Method Details

#command?(name) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#run_cmd(cmd) ⇒ Object



11
12
13
14
15
16
17
18
19
# File 'lib/sastbox-sdk/runner.rb', line 11

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



21
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
50
51
52
# File 'lib/sastbox-sdk/runner.rb', line 21

def run_cmd_with_timeout(cmd)
  out_reader = ''
  err_reader = ''

  Open3.popen3(*cmd) do |stdin, stdout, stderr, wait_thr|
    stdin.close_write
    output, pid = [], wait_thr.pid
    begin
      Timeout.timeout(@opts.timeout) do
        begin
          if stdout && !stdout.closed?
            out_reader = Thread.new { stdout.read }
          end

          if stderr && !stderr.closed?
            err_reader = Thread.new { stderr.read }
          end
        rescue IOError => e
          puts "Error reading IO: #{e.message}"
        end

        Process.wait(pid)
      end
    rescue Errno::ECHILD
    rescue Timeout::Error
      print_error('Timed out - Skipping...')
      Process.kill('TERM', pid)
      exit 1
    end
    [out_reader.value, err_reader.value, wait_thr.value]
  end
end