class Net::SSH::Script::Command

Net::SSH::Connection::Session wrapper to capture status/stdout/stderr produced by a command.

Attributes

error[R]
output[R]

Public Class Methods

[](command) click to toggle source
# File lib/net/ssh/script/command.rb, line 11
def self.[](command)
  new command
end
new(command_string) click to toggle source
# File lib/net/ssh/script/command.rb, line 15
def initialize(command_string)
  @_cmd = command_string
  @_done = @_success = false
  @output = @error = ''
end

Public Instance Methods

done?() click to toggle source
# File lib/net/ssh/script/command.rb, line 33
def done?
  @_done
end
inspect() click to toggle source
# File lib/net/ssh/script/command.rb, line 37
def inspect
  case status
  when :ok
    "#{status}: #{@_cmd}\nOutput:\n#{@output}\n"
  when :error
    "#{status}: #{@_cmd}\nError:\n#{@error}\n"
  else
    "#{status}: #{@_cmd}"
  end
end
run(ssh) click to toggle source
# File lib/net/ssh/script/command.rb, line 21
def run(ssh)
  channel = ssh.open_channel do |ch|
    # In case when we want rerun a command
    @_done = @_success = false
    exec_command @_cmd, ch
  end

  channel.wait

  ok?
end
status() click to toggle source
# File lib/net/ssh/script/command.rb, line 48
def status
  return :pending unless done?
  return :failed unless @_success
  return :error unless @error.empty?
  :ok
end
to_s() click to toggle source
# File lib/net/ssh/script/command.rb, line 55
def to_s
  @_cmd
end

Private Instance Methods

exec_command(command, chan) click to toggle source
# File lib/net/ssh/script/command.rb, line 75
def exec_command(command, chan)
  chan.exec command do |ch, success|
    @_success = success

    unless success
      @_done = true
      return false
    end

    listen_channel ch
  end
end
listen_channel(chan) click to toggle source
# File lib/net/ssh/script/command.rb, line 67
def listen_channel(chan)
  chan.on_close { @_done = true }
  chan.on_data { |_, data| @output << data }
  chan.on_extended_data do |_, type, data|
    @error << "(#{type}):#{data}\n"
  end
end