class Getch::Command

Public Class Methods

new(cmd) click to toggle source
# File lib/getch/command.rb, line 5
def initialize(cmd)
  @cmd = cmd
  @block_size = 1024
  @log = Getch::Log.new
end

Public Instance Methods

run!() click to toggle source
# File lib/getch/command.rb, line 11
def run!
  @log.info "Running command: " + @cmd.gsub(/\"/, '')

  Open3.popen3(@cmd) do |stdin, stdout, stderr, wait_thr|
    stdin.close_write
    code = wait_thr.value

    # only stderr
    begin
      @log.debug stderr.readline until stderr.eof.nil?
    rescue EOFError
    end

    begin
      files = [stdout, stderr]

      until all_eof(files) do
        ready = IO.select(files)

        if ready
          readable = ready[0]
          # writable = ready[1]
          # exceptions = ready[2]

          display_lines(readable)
        end
      end
    rescue IOError => e
      puts "IOError: #{e}"
    end

    unless code.success?
      @log.fatal "Running #{@cmd}"
      exit 1
    end

    @log.debug "Done - #{@cmd} - #{code}"
  end
end

Private Instance Methods

all_eof(files) click to toggle source

Returns true if all files are EOF

# File lib/getch/command.rb, line 54
def all_eof(files)
  files.find { |f| !f.eof }.nil?
end
display_lines(block) click to toggle source
# File lib/getch/command.rb, line 58
def display_lines(block)
  block.each do |f|
    begin
      data = f.read_nonblock(@block_size)
      puts data if OPTIONS[:verbose]
    rescue EOFError
      puts ""
    rescue => e
      puts "Fatal - #{e}"
    end
  end
end