class AemLookout::Terminal

Attributes

log[R]

Public Class Methods

new(log = Logger.new(STDOUT)) click to toggle source
# File lib/aem_lookout/terminal.rb, line 9
def initialize(log = Logger.new(STDOUT))
  @log = log
end

Public Instance Methods

execute_command(command) click to toggle source
# File lib/aem_lookout/terminal.rb, line 13
def execute_command(command)
  Open3.popen3(command) do |stdin, stdout, stderr, thread|
    flush(stdout: stdout, stderr: stderr) until !thread.alive?
    flush(stdout: stdout, stderr: stderr)
  end
end
flush(options = {}) click to toggle source
# File lib/aem_lookout/terminal.rb, line 20
def flush(options = {})
  stdout_thread = stream_to_log(options.fetch(:stdout))
  stderr_thread = stream_to_log(options.fetch(:stderr), error: true)
  sleep 0.1 until !stdout_thread.alive? and !stdout_thread.alive?
end
stream_to_log(io, options = {}) click to toggle source
# File lib/aem_lookout/terminal.rb, line 26
def stream_to_log(io, options = {})
  options = options.merge({error: false})
  thread = Thread.new do
    while message = io.gets
      if options.fetch(:error)
        log.error message.chomp
      else
        log.info message.chomp
      end

      sleep 0.01 # to ensure line isn't still being written to
    end
  end
end