Class: Maze::InteractiveCLI
- Inherits:
-
Object
- Object
- Maze::InteractiveCLI
- Defined in:
- lib/maze/interactive_cli.rb
Overview
Encapsulates a shell session, retaining state and input streams for interactive tests
Instance Attribute Summary collapse
-
#current_buffer ⇒ Object
readonly
attr_reader :current_buffer.
-
#pid ⇒ Object
readonly
Returns the value of attribute pid.
-
#stderr_lines ⇒ Object
readonly
Returns the value of attribute stderr_lines.
-
#stdout_lines ⇒ Object
readonly
Returns the value of attribute stdout_lines.
Instance Method Summary collapse
-
#initialize(shell = '/bin/sh', stop_command = 'exit') ⇒ InteractiveCLI
constructor
Creates an InteractiveCLI instance.
- #on_exit(&block) ⇒ Object
-
#run_command(command) ⇒ Boolean
Runs the given command if the shell is running.
-
#running? ⇒ Boolean
Whether the shell is currently running.
- #start(threaded: true) ⇒ Object
-
#stop ⇒ Boolean
Attempts to stop the shell using the preset command and wait for it to exit.
Constructor Details
#initialize(shell = '/bin/sh', stop_command = 'exit') ⇒ InteractiveCLI
Creates an InteractiveCLI instance
29 30 31 32 33 34 35 36 37 38 |
# File 'lib/maze/interactive_cli.rb', line 29 def initialize(shell = '/bin/sh', stop_command = 'exit') @shell = shell @stop_command = stop_command @stdout_lines = [] @stderr_lines = [] @on_exit_blocks = [] @current_buffer = '' start_threaded_shell(shell) end |
Instance Attribute Details
#current_buffer ⇒ Object (readonly)
attr_reader :current_buffer
21 22 23 |
# File 'lib/maze/interactive_cli.rb', line 21 def current_buffer strip_nonprintable(@current_buffer) end |
#pid ⇒ Object (readonly)
Returns the value of attribute pid.
16 17 18 |
# File 'lib/maze/interactive_cli.rb', line 16 def pid @pid end |
#stderr_lines ⇒ Object (readonly)
Returns the value of attribute stderr_lines.
12 13 14 |
# File 'lib/maze/interactive_cli.rb', line 12 def stderr_lines @stderr_lines end |
#stdout_lines ⇒ Object (readonly)
Returns the value of attribute stdout_lines.
8 9 10 |
# File 'lib/maze/interactive_cli.rb', line 8 def stdout_lines @stdout_lines end |
Instance Method Details
#on_exit(&block) ⇒ Object
82 83 84 |
# File 'lib/maze/interactive_cli.rb', line 82 def on_exit(&block) @on_exit_blocks << block end |
#run_command(command) ⇒ Boolean
Runs the given command if the shell is running
71 72 73 74 75 76 77 78 79 80 |
# File 'lib/maze/interactive_cli.rb', line 71 def run_command(command) return false unless running? @in_stream.puts(command) true rescue ::Errno::EIO => err $logger.debug(pid) { "EIO error: #{err}" } false end |
#running? ⇒ Boolean
Returns Whether the shell is currently running.
62 63 64 |
# File 'lib/maze/interactive_cli.rb', line 62 def running? !@pid.nil? end |
#start(threaded: true) ⇒ Object
40 41 42 |
# File 'lib/maze/interactive_cli.rb', line 40 def start(threaded: true) threaded ? start_threaded_shell(@shell) : start_shell(@shell) end |
#stop ⇒ Boolean
Attempts to stop the shell using the preset command and wait for it to exit
47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/maze/interactive_cli.rb', line 47 def stop run_command(@stop_command) @in_stream.close maybe_thread = @thread.join(15) # The thread did not exit! return false if maybe_thread.nil? @pid = nil true end |