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
Returns the value of attribute 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
28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/maze/interactive_cli.rb', line 28 def initialize(shell = '/bin/sh', stop_command = 'exit') @shell = shell @stop_command = stop_command @stdout_lines = [] @stderr_lines = [] @on_exit_blocks = [] @current_buffer = '' # TODO: Removed pending PLAT-6322 # @boring = Boring.new start_threaded_shell(shell) end |
Instance Attribute Details
#current_buffer ⇒ Object (readonly)
Returns the value of attribute current_buffer.
22 23 24 |
# File 'lib/maze/interactive_cli.rb', line 22 def current_buffer @current_buffer end |
#pid ⇒ Object (readonly)
Returns the value of attribute pid.
18 19 20 |
# File 'lib/maze/interactive_cli.rb', line 18 def pid @pid end |
#stderr_lines ⇒ Object (readonly)
Returns the value of attribute stderr_lines.
14 15 16 |
# File 'lib/maze/interactive_cli.rb', line 14 def stderr_lines @stderr_lines end |
#stdout_lines ⇒ Object (readonly)
Returns the value of attribute stdout_lines.
10 11 12 |
# File 'lib/maze/interactive_cli.rb', line 10 def stdout_lines @stdout_lines end |
Instance Method Details
#on_exit(&block) ⇒ Object
83 84 85 |
# File 'lib/maze/interactive_cli.rb', line 83 def on_exit(&block) @on_exit_blocks << block end |
#run_command(command) ⇒ Boolean
Runs the given command if the shell is running
72 73 74 75 76 77 78 79 80 81 |
# File 'lib/maze/interactive_cli.rb', line 72 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.
63 64 65 |
# File 'lib/maze/interactive_cli.rb', line 63 def running? !@pid.nil? end |
#start(threaded: true) ⇒ Object
41 42 43 |
# File 'lib/maze/interactive_cli.rb', line 41 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
48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/maze/interactive_cli.rb', line 48 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 |