Top Level Namespace

Defined Under Namespace

Classes: Pair

Constant Summary collapse

J =
MultiJson
SERVER_HOST =
"pairkit.com"
SERVER_WEB =
"http://#{SERVER_HOST}:80"
SERVER_SSH_USERNAME =
'pairkit'

Instance Method Summary collapse

Instance Method Details

#ask_userObject



21
22
23
# File 'lib/ssh_keys.rb', line 21

def ask_user
  $stdin.gets.to_s.strip
end

#error(message) ⇒ Object



25
26
27
28
# File 'lib/ssh_keys.rb', line 25

def error(message)
  $stderr.puts(message)
  exit(1)
end

#generate_ssh_key(keyfile) ⇒ Object



8
9
10
11
12
13
14
15
# File 'lib/ssh_keys.rb', line 8

def generate_ssh_key(keyfile)
  ssh_dir = File.join(home_directory, ".ssh")
  unless File.exists?(ssh_dir)
    FileUtils.mkdir_p ssh_dir
    File.chmod(0700, ssh_dir)
  end
  `ssh-keygen -t rsa -N "" -f \"#{home_directory}}/.ssh/#{keyfile}\" 2>&1`
end

#get_key(key) ⇒ Object



17
18
19
# File 'lib/ssh_keys.rb', line 17

def get_key(key)
  File.read(key).chomp #strip trailing newline if present
end

#get_or_generate_ssh_keyObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/ssh_keys.rb', line 38

def get_or_generate_ssh_key
  public_keys = Dir.glob("#{home_directory}/.ssh/*.pub").sort

  case public_keys.length
  when 0 then
    puts "Could not find an existing public key."
    puts "Let's generate one..."
    puts "Generating new SSH public key."
    generate_ssh_key("id_rsa")
    get_key("#{home_directory}/.ssh/id_rsa.pub")
  when 1 then
    puts "Using existing public key: #{public_keys.first}"
    get_key(public_keys.first)
  else
    puts "Found the following SSH public keys:"
    public_keys.each_with_index do |key, index|
      puts "#{index+1}) #{File.basename(key)}"
    end
    puts "Which would you like to use? (Enter number) "
    choice = ask_user.to_i - 1
    chosen = public_keys[choice]
    if choice == -1 || chosen.nil?
      error("Invalid choice.")
    end
    get_key(chosen)
  end
end

#home_directoryObject



30
31
32
# File 'lib/ssh_keys.rb', line 30

def home_directory
  running_on_windows? ? ENV['USERPROFILE'].gsub("\\","/") : ENV['HOME']
end

#running_on_windows?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/ssh_keys.rb', line 34

def running_on_windows?
  RUBY_PLATFORM =~ /mswin32|mingw32/
end