Class: Bio::Commandeer

Inherits:
Object
  • Object
show all
Defined in:
lib/bio-commandeer/commandeer.rb

Overview

See #run

Class Method Summary collapse

Class Method Details

.run(command, options = {}) ⇒ Object

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.


15
16
17
18
19
20
# 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 = {}) ⇒ Object

Options are as per #run, but return a CommandResult object



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/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