Class: AemLookout::Terminal
- Inherits:
-
Object
- Object
- AemLookout::Terminal
- Defined in:
- lib/aem_lookout/terminal.rb
Instance Attribute Summary collapse
-
#log ⇒ Object
readonly
Returns the value of attribute log.
Instance Method Summary collapse
- #execute_command(command) ⇒ Object
- #flush(options = {}) ⇒ Object
-
#initialize(log = Logger.new(STDOUT)) ⇒ Terminal
constructor
A new instance of Terminal.
- #stream_to_log(io, options = {}) ⇒ Object
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
#log ⇒ Object (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( = {}) stdout_thread = stream_to_log(.fetch(:stdout)) stderr_thread = stream_to_log(.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, = {}) = .merge({error: false}) thread = Thread.new do while = io.gets if .fetch(:error) log.error .chomp else log.info .chomp end sleep 0.01 # to ensure line isn't still being written to end end end |