Class: Sprites::Resources::Exec::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/sprites/resources/exec.rb

Overview

WebSocket session for interactive command execution.

Instance Attribute Summary collapse

Instance Method Summary collapse

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_codeInteger? (readonly)

Returns exit code once process exits.

Returns:

  • (Integer, nil)

    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

#closeObject

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.

Yields:

  • (Integer)

    exit code



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.

Yields:

  • (String)

    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.

Yields:

  • (String)

    stdout data



206
# File 'lib/sprites/resources/exec.rb', line 206

def on_stdout(&block) = @callbacks[:stdout] << block

#read_loopObject



242
243
244
245
246
247
248
249
250
# File 'lib/sprites/resources/exec.rb', line 242

def read_loop
  loop do
    message = @connection.read
    break unless message

    handle_message(message)
    break if @exit_code
  end
end

#send_eofObject

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

  message = [STREAM_STDIN_EOF].pack("C")
  @connection.write(Protocol::WebSocket::BinaryMessage.new(message))
  @connection.flush
end

#write(data) ⇒ Object

Write data to stdin.

Parameters:

  • data (String)

    data to write



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
    message = [STREAM_STDIN].pack("C") + data
    @connection.write(Protocol::WebSocket::BinaryMessage.new(message))
  end
  @connection.flush
end