def check_for_associated_ssh_key
return unless client.keys.length.zero?
public_keys = available_ssh_public_keys
case public_keys.length
when 0 then
display "Could not find an existing public key."
display "Would you like to generate one? [Yn] ", false
unless ask.strip.downcase == "n"
display "Generating new SSH public key."
generate_ssh_key("id_rsa")
associate_key("#{home_directory}/.ssh/id_rsa.pub")
end
when 1 then
display "Found existing public key: #{public_keys.first}"
display "Would you like to associate it with your Heroku account? [Yn] ", false
associate_key(public_keys.first) unless ask.strip.downcase == "n"
else
display "Found the following SSH public keys:"
public_keys.sort.each_with_index do |key, index|
display "#{index+1}) #{File.basename(key)}"
end
display "Which would you like to use with your Heroku account? ", false
chosen = public_keys[ask.to_i-1] rescue error("Invalid choice")
associate_key(chosen)
end
end