Sha256: 503ab9bfc2869d5dc215c25024a8b9d3172159714c39f752ad36230710cafeb2

Contents?: true

Size: 1.19 KB

Versions: 1

Compression:

Stored size: 1.19 KB

Contents

=begin
if RUBY_PLATFORM =~ /mswin|mingw/
  begin
    require 'win32/open3'
  rescue LoadError
    warn "You must 'gem install win32-open3' to use Cotta on Windows"
    exit 1
  end
else
  require 'open3'
end
=end

module Cotta
  
# Command runner
class CommandRunner
  attr_reader :outputs

  def initialize(command)
    @command = command
  end
  
  # 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.
  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
  
  private
  def load_output(id, io)
    output = ''
    while (line = io.gets) do
      puts "[#{id}] #{line}"
      output << line
    end
    return output
  end
end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
cotta-1.0.0 lib/cotta/impl/command_runner.rb