Class: Sprites::Resources::Exec::Session
- Inherits:
-
Object
- Object
- Sprites::Resources::Exec::Session
- Defined in:
- lib/sprites/resources/exec.rb
Overview
WebSocket session for interactive command execution.
Instance Attribute Summary collapse
-
#exit_code ⇒ Integer?
readonly
Exit code once process exits.
Instance Method Summary collapse
-
#close ⇒ Object
Close the WebSocket connection.
-
#initialize(connection, tty: false) ⇒ Session
constructor
A new instance of Session.
-
#on_exit {|Integer| ... } ⇒ Object
Register a callback for process exit.
-
#on_stderr {|String| ... } ⇒ Object
Register a callback for stderr data.
-
#on_stdout {|String| ... } ⇒ Object
Register a callback for stdout data.
- #read_loop ⇒ Object
-
#send_eof ⇒ Object
Signal end of stdin (non-TTY mode only).
-
#write(data) ⇒ Object
Write data to stdin.
Constructor Details
#initialize(connection, tty: false) ⇒ Session
Returns a new instance of Session.
194 195 196 197 198 199 |
# File 'lib/sprites/resources/exec.rb', line 194 def initialize(connection, tty: false) @connection = connection @tty = tty @callbacks = { stdout: [], stderr: [], exit: [] } @exit_code = nil end |
Instance Attribute Details
#exit_code ⇒ Integer? (readonly)
Returns exit code once process exits.
202 203 204 |
# File 'lib/sprites/resources/exec.rb', line 202 def exit_code @exit_code end |
Instance Method Details
#close ⇒ Object
Close the WebSocket connection.
238 239 240 |
# File 'lib/sprites/resources/exec.rb', line 238 def close @connection.close end |
#on_exit {|Integer| ... } ⇒ Object
Register a callback for process exit.
214 |
# File 'lib/sprites/resources/exec.rb', line 214 def on_exit(&block) = @callbacks[:exit] << block |
#on_stderr {|String| ... } ⇒ Object
Register a callback for stderr data.
210 |
# File 'lib/sprites/resources/exec.rb', line 210 def on_stderr(&block) = @callbacks[:stderr] << block |
#on_stdout {|String| ... } ⇒ Object
Register a callback for stdout data.
206 |
# File 'lib/sprites/resources/exec.rb', line 206 def on_stdout(&block) = @callbacks[:stdout] << block |
#read_loop ⇒ Object
242 243 244 245 246 247 248 249 250 |
# File 'lib/sprites/resources/exec.rb', line 242 def read_loop loop do = @connection.read break unless () break if @exit_code end end |
#send_eof ⇒ Object
Signal end of stdin (non-TTY mode only).
229 230 231 232 233 234 235 |
# File 'lib/sprites/resources/exec.rb', line 229 def send_eof return if @tty = [STREAM_STDIN_EOF].pack("C") @connection.write(Protocol::WebSocket::BinaryMessage.new()) @connection.flush end |
#write(data) ⇒ Object
Write data to stdin.
218 219 220 221 222 223 224 225 226 |
# File 'lib/sprites/resources/exec.rb', line 218 def write(data) if @tty @connection.write(Protocol::WebSocket::BinaryMessage.new(data)) else = [STREAM_STDIN].pack("C") + data @connection.write(Protocol::WebSocket::BinaryMessage.new()) end @connection.flush end |