aboutsummaryrefslogtreecommitdiff
path: root/lib/leap_cli/commands/user.rb
diff options
context:
space:
mode:
authorelijah <elijah@riseup.net>2012-10-25 00:49:10 -0700
committerelijah <elijah@riseup.net>2012-10-25 00:49:10 -0700
commita269255b2f51783ba19cbc363759ba9a61038ea0 (patch)
tree0818a266e13be244baee3a0d4faf022def7c59b8 /lib/leap_cli/commands/user.rb
parent80747fe28c40403bd07473c2bc7fad19cb562fcd (diff)
downloadleap_cli-a269255b2f51783ba19cbc363759ba9a61038ea0.tar.gz
leap_cli-a269255b2f51783ba19cbc363759ba9a61038ea0.tar.bz2
cleaned up the code a bit by adding SshKey class.
Diffstat (limited to 'lib/leap_cli/commands/user.rb')
-rw-r--r--lib/leap_cli/commands/user.rb28
1 files changed, 19 insertions, 9 deletions
diff --git a/lib/leap_cli/commands/user.rb b/lib/leap_cli/commands/user.rb
index a7bf848..5f7702a 100644
--- a/lib/leap_cli/commands/user.rb
+++ b/lib/leap_cli/commands/user.rb
@@ -40,12 +40,11 @@ module LeapCli
if options[:self]
username ||= `whoami`.strip
- ssh_pub_key ||= pick_ssh_key
+ ssh_pub_key ||= pick_ssh_key.to_s
pgp_pub_key ||= pick_pgp_key
end
assert!(ssh_pub_key, 'Sorry, could not find SSH public key.')
- #assert!(pgp_pub_key, 'Sorry, could not find OpenPGP public key.')
if ssh_pub_key
write_file!([:user_ssh, username], ssh_pub_key)
@@ -61,19 +60,30 @@ module LeapCli
# let the the user choose among the ssh public keys that we encounter, or just pick the key if there is only one.
#
def pick_ssh_key
- assert_bin! 'ssh-add'
- ssh_fingerprints = `ssh-add -l`.split("\n").compact
- assert! ssh_fingerprints.any?, 'Sorry, could not find any SSH public key for you. Have you run ssh-keygen?'
+ ssh_keys = []
+ Dir.glob("#{ENV['HOME']}/.ssh/*.pub").each do |keyfile|
+ ssh_keys << SshKey.load(keyfile)
+ end
+
+ if `which ssh-add && ssh-add -L`.strip.any?
+ `ssh-add -L`.split("\n").compact.each do |line|
+ key = SshKey.load(line)
+ key.comment = 'ssh-agent'
+ ssh_keys << key unless ssh_keys.include?(key)
+ end
+ end
+ ssh_keys.compact!
+
+ assert! ssh_keys.any?, 'Sorry, could not find any SSH public key for you. Have you run ssh-keygen?'
- if ssh_fingerprints.length > 1
- key_index = numbered_choice_menu('Choose your SSH public key', ssh_fingerprints) do |key, i|
- say("#{i+1}. #{key}")
+ if ssh_keys.length > 1
+ key_index = numbered_choice_menu('Choose your SSH public key', ssh_keys.collect(&:summary)) do |line, i|
+ say("#{i+1}. #{line}")
end
else
key_index = 0
end
- ssh_keys = `ssh-add -L`.split("\n").compact
return ssh_keys[key_index]
end