Class: SshShort::KeySet

Inherits:
Object
  • Object
show all
Defined in:
lib/ssh_short/keyset.rb

Instance Method Summary collapse

Constructor Details

#initialize(keys_dir) ⇒ KeySet

Returns a new instance of KeySet.



7
8
9
# File 'lib/ssh_short/keyset.rb', line 7

def initialize(keys_dir)
  @keys_dir = keys_dir
end

Instance Method Details

#find_keysObject



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/ssh_short/keyset.rb', line 37

def find_keys
  abort "Error: Cannot find keys directory at #{@keys_dir}" unless File.exist? @keys_dir
  
  # Recursively search directory, including following symlinks
  search_string = "#{File.expand_path(@keys_dir)}/**{,/*/**}/*"
  
  keys = Dir.glob(search_string).select { |e| File.file? e }
  abort "Error: No keys found in #{@keys_dir}" unless keys.count > 0
  
  keys
end

#get_key(key_name) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/ssh_short/keyset.rb', line 24

def get_key(key_name)
  if key_name.eql?('id_rsa')
    key = File.expand_path('~/.ssh/id_rsa')
  else
    keys = find_keys.select { |path| File.basename(path) == key_name }
    abort "Error: More than one key found called #{key_name}" if keys.count > 1
    key = keys.first
  end
  # key = File.expand_path(key)
  abort "Error: Cannot find #{key}" unless File.exist? key
  key
end

#prompt_for_keyObject



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/ssh_short/keyset.rb', line 11

def prompt_for_key
  key_names = find_keys.collect { |key| File.basename key }
  key_names.unshift 'id_rsa'

  puts 'Select a key:'
  key_names.each_with_index { |key_name, i| puts "#{i}) #{key_name}" }

  key_selection = STDIN.gets.to_i
  abort "#{key_selection} is not a valid key" if (key_selection >= key_names.count)

  key_names[key_selection]
end