Class: SSHMan::CLI

Inherits:
Thor
  • Object
show all
Defined in:
lib/sshman/cli.rb

Instance Method Summary collapse

Instance Method Details

#addObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/sshman/cli.rb', line 19

def add
  prompt = TTY::Prompt.new
  name = prompt.ask("Enter connection name:")
  host = prompt.ask("Enter host:")
  user = prompt.ask("Enter username:")
  port = prompt.ask("Enter port:", default: "22")
  auth_type = prompt.select("Choose authentication type:", %w(Password SSH\ Key))
  
  if auth_type == "SSH Key"
    ssh_key = prompt.ask("Enter path to SSH key (relative to ~/.ssh/):")
  else
    ssh_key = nil
  end

  connection = Connection.new(name, host, user, port, auth_type, ssh_key)
  Manager.add_connection(connection)
  puts "Connection added successfully!"
end

#connect(name) ⇒ Object



69
70
71
72
73
74
75
76
77
78
# File 'lib/sshman/cli.rb', line 69

def connect(name)
  connection = Manager.find_connection(name)
  return puts "Connection not found." unless connection

  cmd = "ssh #{connection.user}@#{connection.host} -p #{connection.port}"
  cmd += " -i ~/.ssh/#{connection.ssh_key}" if connection.auth_type == "SSH Key"
  
  puts "Connecting to #{connection.name}..."
  exec(cmd)
end

#delete(name) ⇒ Object



60
61
62
63
64
65
66
# File 'lib/sshman/cli.rb', line 60

def delete(name)
  if Manager.delete_connection(name)
    puts "Connection deleted successfully!"
  else
    puts "Connection not found."
  end
end

#edit(name) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/sshman/cli.rb', line 39

def edit(name)
  connection = Manager.find_connection(name)
  return puts "Connection not found." unless connection

  prompt = TTY::Prompt.new
  connection.host = prompt.ask("Enter new host:", default: connection.host)
  connection.user = prompt.ask("Enter new username:", default: connection.user)
  connection.port = prompt.ask("Enter new port:", default: connection.port)
  connection.auth_type = prompt.select("Choose new authentication type:", %w(Password SSH\ Key), default: connection.auth_type)
  
  if connection.auth_type == "SSH Key"
    connection.ssh_key = prompt.ask("Enter new path to SSH key (relative to ~/.ssh/):", default: connection.ssh_key)
  else
    connection.ssh_key = nil
  end

  Manager.update_connection(connection)
  puts "Connection updated successfully!"
end

#listObject



9
10
11
12
13
14
15
16
# File 'lib/sshman/cli.rb', line 9

def list
  connections = Manager.list_connections
  table = TTY::Table.new(
    header: ['Name', 'Host', 'User', 'Port', 'Auth Type'],
    rows: connections.map { |c| [c.name, c.host, c.user, c.port, c.auth_type] }
  )
  puts table.render(:unicode, padding: [0, 1])
end