Class: Sshify::Commands::Connect

Inherits:
Sshify::Command show all
Defined in:
lib/sshify/commands/connect.rb

Instance Method Summary collapse

Methods inherited from Sshify::Command

#command, #cursor, #editor, #exec_exist?, #generator, #pager, #platform, #prompt, #screen, #which

Constructor Details

#initialize(options) ⇒ Connect



10
11
12
13
14
15
16
17
# File 'lib/sshify/commands/connect.rb', line 10

def initialize(options)
  @options = options

  @config = TTY::Config.new
  @config.filename = "sshify-config"
  @config.extname = ".yml"
  @config.append_path Dir.home
end

Instance Method Details

#execute(input: $stdin, output: $stdout) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/sshify/commands/connect.rb', line 19

def execute(input: $stdin, output: $stdout)
  @config ||= self.class.new(@options).config
  @config.read if @config.persisted?

  selection = prompt.select("Select one to connect") do |menu|
    @config.read.keys.each do |server_name|
      menu.choice server_name
    end
  end

  server = @config.fetch(selection)
  user = server['user'].first
  server_ip = server['server_ip'].first

  exec("ssh #{user}@#{server_ip}")
end