class SshShort::KeySet

Public Class Methods

new(keys_dir) click to toggle source
# File lib/ssh_short/keyset.rb, line 7
def initialize(keys_dir)
  @keys_dir = keys_dir
end

Public Instance Methods

find_keys() click to toggle source
# 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) click to toggle source
# 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_key() click to toggle source
# 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