class Bio::Commandeer

See run

Public Class Methods

run(command, options={}) click to toggle source

Run a command line program, and be opinionated about how to handle failure

command is a string of the command to be run

  • options is a hash, with keys:

:stdin: a string that is the stdin :log: if true, turn on logging. If given an object use it as the logger :timeout: number of seconds to allow the process to run for. If nil (the default),

no timeout.
# File lib/bio-commandeer/commandeer.rb, line 15
def self.run(command, options={})
  obj = run_to_finish(command, options)
  obj.raise_if_failed

  return obj.stdout
end
run_to_finish(command, options={}) click to toggle source

Options are as per run, but return a CommandResult object

# File lib/bio-commandeer/commandeer.rb, line 23
def self.run_to_finish(command, options={})
  if options[:log]
    if options[:log] == true
      log_name = 'bio-commandeer'
      @log = Bio::Log::LoggerPlus[log_name]
      if @log.nil? or @log.outputters.empty?
        @log = Bio::Log::LoggerPlus.new(log_name)
        Bio::Log::CLI.configure(log_name)
      end
    else
      @log = options[:log]
    end

    @log.info "Running command: #{command}"
  end
  res = CommandResult.new
  res.command = command
  begin
    Timeout::timeout(options[:timeout]) do
      res.status, res.stdout, res.stderr = systemu command, :stdin => options[:stdin]
    end
  rescue Timeout::Error => e
    res.timed_out = true
  end

  if @log
    @log.info "Command finished with exitstatus #{res.status.exitstatus}"
  end
  return res
end