Class: JetBlack::TerminalSession
- Inherits:
-
Object
- Object
- JetBlack::TerminalSession
- Defined in:
- lib/jet_black/terminal_session.rb
Constant Summary collapse
- DEFAULT_TIMEOUT =
10
Instance Attribute Summary collapse
-
#exit_status ⇒ Object
readonly
Returns the value of attribute exit_status.
Instance Method Summary collapse
- #end_session(signal: "INT") ⇒ Object
- #expect(expected_value, reply: nil, timeout: DEFAULT_TIMEOUT, signal_on_timeout: "KILL") ⇒ Object
- #finished? ⇒ Boolean
-
#initialize(raw_command, env:, directory:) ⇒ TerminalSession
constructor
A new instance of TerminalSession.
- #stderr ⇒ Object
- #stdout ⇒ Object
- #wait_for_finish ⇒ Object
Constructor Details
#initialize(raw_command, env:, directory:) ⇒ TerminalSession
Returns a new instance of TerminalSession.
11 12 13 14 15 |
# File 'lib/jet_black/terminal_session.rb', line 11 def initialize(raw_command, env:, directory:) @stderr_reader, @stderr_writer = IO.pipe @output, @input, @pid = PTY.spawn(env, raw_command, chdir: directory, err: stderr_writer.fileno) self.raw_stdout = [] end |
Instance Attribute Details
#exit_status ⇒ Object
Returns the value of attribute exit_status.
9 10 11 |
# File 'lib/jet_black/terminal_session.rb', line 9 def exit_status @exit_status end |
Instance Method Details
#end_session(signal: "INT") ⇒ Object
48 49 50 51 52 53 |
# File 'lib/jet_black/terminal_session.rb', line 48 def end_session(signal: "INT") Process.kill(signal, pid) finalize_io self.exit_status = wait_for_exit_status end |
#expect(expected_value, reply: nil, timeout: DEFAULT_TIMEOUT, signal_on_timeout: "KILL") ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/jet_black/terminal_session.rb', line 17 def expect(expected_value, reply: nil, timeout: DEFAULT_TIMEOUT, signal_on_timeout: "KILL") output_matches = output.expect(expected_value, timeout) if output_matches.nil? end_session(signal: signal_on_timeout) raise TerminalSessionTimeoutError.new(self, expected_value, timeout) end raw_stdout.concat(output_matches) if reply != nil input.puts(reply) end end |
#finished? ⇒ Boolean
55 56 57 |
# File 'lib/jet_black/terminal_session.rb', line 55 def finished? !exit_status.nil? end |
#stderr ⇒ Object
36 37 38 |
# File 'lib/jet_black/terminal_session.rb', line 36 def stderr raw_std_err end |
#stdout ⇒ Object
32 33 34 |
# File 'lib/jet_black/terminal_session.rb', line 32 def stdout raw_stdout.join.gsub("\r", "") end |
#wait_for_finish ⇒ Object
40 41 42 43 44 45 46 |
# File 'lib/jet_black/terminal_session.rb', line 40 def wait_for_finish return if finished? finalize_io self.exit_status = wait_for_exit_status end |