Files
analyzer-pipeline/lib/pipeline/util/external_command.rb

61 lines
1.2 KiB
Ruby
Raw Normal View History

2019-09-10 10:58:45 +01:00
require 'open3'
2019-09-03 14:45:25 +01:00
module Pipeline::Util
class ExternalCommand
attr_accessor :cmd_string, :status, :stdout, :stderr, :suppress_output
def initialize(cmd_string)
@cmd_string = cmd_string
end
def call!
call
raise "Failed #{cmd_string}" unless status.success?
end
def call
c = cmd
puts "> #{c}" unless suppress_output
@stdout, @stderr, @status = Open3.capture3(c)
2019-11-27 20:38:52 +00:00
# Open3.popen3("ls") do |stdout, stderr, status, thread|
# @stdout = stdout.read
# @stderr = stderr.read
# @status = status
# end
2019-09-03 14:45:25 +01:00
puts "status: #{status}" unless suppress_output
puts "stdout: #{stdout}" unless suppress_output
puts "stderr: #{stderr}" unless suppress_output
end
def cmd
if @timeout
2019-11-27 20:22:46 +00:00
"/usr/bin/timeout -s 9 -k #{@timeout + 1} #{@timeout} #{cmd_string}"
2019-09-03 14:45:25 +01:00
else
cmd_string
end
end
def success?
status.success?
end
def exit_status
status.exitstatus
end
def timeout=(timeout)
@timeout = timeout
end
def report
{
cmd: cmd_string,
success: success?,
stdout: stdout,
stderr: stderr
}
end
2019-09-03 14:45:25 +01:00
end
end