Class: GitGo::Connection::Remote

Inherits:
Base
  • Object
show all
Defined in:
lib/git_go/connection/remote.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#core, #current_path

Instance Method Summary collapse

Methods inherited from Base

#cd, #close_and_exit, #directory?, #file?, #mkdir, #mv, #rm

Constructor Details

#initialize(core) ⇒ GitGo::Connection::Remote

Creates a new instance of GitGo::Connection::Remote. Establishes a session with the host and stores that session in #ssh.

Parameters:

  • core (GitGo::Core)

    takes and assigns a reference to parent class in #core.



13
14
15
16
# File 'lib/git_go/connection/remote.rb', line 13

def initialize(core)
  super(core)
  @ssh = Net::SSH.start(core.host, core.user)
end

Instance Attribute Details

#sshNet::SSH::Connection::Session (readonly)

Returns instance.

Returns:

  • (Net::SSH::Connection::Session)

    instance.



6
7
8
# File 'lib/git_go/connection/remote.rb', line 6

def ssh
  @ssh
end

Instance Method Details

#bash(command) ⇒ Hash

Executes the provided COMMAND over the #ssh connection. This is a blocking (non-asynchronous) operation.

Parameters:

  • command (String)

    the command to perform on the host.

Returns:

  • (Hash)

    containing response data (:stdout, :stderr, :exit_code).



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/git_go/connection/remote.rb', line 31

def bash(command)
  response = {
    :stdout => "",
    :stderr => ""
  }

  ssh.open_channel do |channel|
    channel.exec(%Q(#{source} cd "#{current_path}"; #{command})) do |ch, success|
      abort "Failed: Couldn't execute command (ssh.channel.exec)." unless success

      channel.on_data                   { |_, data|     response[:stdout]    += data           }
      channel.on_extended_data          { |_, __, data| response[:stderr]    += data           }
      channel.on_request("exit-status") { |_, data|     response[:exit_code]  = data.read_long }
    end
  end

  ssh.loop
  response
end

#closeObject

Closes the #ssh connection and clears the ‘@connection` instance variable of this objects parent `Core` instance so new connections can be established.



21
22
23
24
# File 'lib/git_go/connection/remote.rb', line 21

def close
  ssh.close
  core.clear_connection
end