Class: AemLookout::Terminal

Inherits:
Object
  • Object
show all
Defined in:
lib/aem_lookout/terminal.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(log = Logger.new(STDOUT)) ⇒ Terminal

Returns a new instance of Terminal.



9
10
11
# File 'lib/aem_lookout/terminal.rb', line 9

def initialize(log = Logger.new(STDOUT))
  @log = log
end

Instance Attribute Details

#logObject (readonly)

Returns the value of attribute log.



7
8
9
# File 'lib/aem_lookout/terminal.rb', line 7

def log
  @log
end

Instance Method Details

#execute_command(command) ⇒ Object



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



20
21
22
23
24
# 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 = {}) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# 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