Class: Cotta::CommandRunner

Inherits:
Object
  • Object
show all
Defined in:
lib/cotta/impl/command_runner.rb

Overview

Command runner

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(command) ⇒ CommandRunner

Returns a new instance of CommandRunner.



20
21
22
# File 'lib/cotta/impl/command_runner.rb', line 20

def initialize(command)
  @command = command
end

Instance Attribute Details

#outputsObject (readonly)

Returns the value of attribute outputs.



18
19
20
# File 'lib/cotta/impl/command_runner.rb', line 18

def outputs
  @outputs
end

Instance Method Details

#executeObject

executs the command. If a closure is given, it will be called with the io to the process If not, it will print out the log pre-fixed with a random number for the process, collects the output and return the output when the process finishes. This method will also raise CommandError if the process fails.

Raises:



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/cotta/impl/command_runner.rb', line 29

def execute
  id = rand(10000)
  puts "[#{id}]$> #{@command}"
  output = nil
  IO.popen(@command) do |io|
    if (block_given?)
      output = yield io
    else
      output = load_output(id, io)
    end
  end
  last_process = $?
  raise CommandError.new(last_process, output), @command unless last_process.exitstatus == 0
  return output
end