From 5a15c73d1e005171c63f57ba76f35293ffed892c Mon Sep 17 00:00:00 2001 From: David Dollar Date: Mon, 2 Aug 2010 13:00:29 -0400 Subject: [PATCH] add an --auto flag for automated key creation and upload --- README.md | 13 ++++++++++++ lib/heroku/command/accounts.rb | 36 +++++++++++++++++++++++++++------- 2 files changed, 42 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 1c690c4..9f9aca2 100644 --- a/README.md +++ b/README.md @@ -22,6 +22,19 @@ To add accounts: IdentityFile /PATH/TO/PRIVATE/KEY IdentitiesOnly yes +Or you can choose a fully-automated approach: + + $ heroku accounts:add work --auto + Enter your Heroku credentials. + Email: work@example.org + Password: ****** + Generating new SSH key + Generating public/private rsa key pair. + Your identification has been saved in ~/.ssh/identity.heroku.work. + Your public key has been saved in ~/.ssh/identity.heroku.work.pub. + Adding entry to ~/.ssh/config + Adding public key to Heroku account: work@example.org + To switch an app to a different account: # in project root diff --git a/lib/heroku/command/accounts.rb b/lib/heroku/command/accounts.rb index 4771160..406a514 100644 --- a/lib/heroku/command/accounts.rb +++ b/lib/heroku/command/accounts.rb @@ -25,13 +25,31 @@ def add :password => password ) - display "" - display "Add the following to your ~/.ssh/config" - display "" - display "Host heroku.#{name}" - display " HostName heroku.com" - display " IdentityFile /PATH/TO/PRIVATE/KEY" - display " IdentitiesOnly yes" + if extract_option("--auto") then + display "Generating new SSH key" + system %{ ssh-keygen -t rsa -f #{account_ssh_key(name)} -N "" } + + display "Adding entry to ~/.ssh/config" + File.open(File.expand_path("~/.ssh/config"), "a") do |file| + file.puts + file.puts "Host heroku.#{name}" + file.puts " HostName heroku.com" + file.puts " IdentityFile #{account_ssh_key(name)}" + file.puts " IdentitiesOnly yes" + end + + display "Adding public key to Heroku account: #{username}" + client = Heroku::Client.new(username, password) + client.add_key(File.read(File.expand_path(account_ssh_key(name) + ".pub"))) + else + display "" + display "Add the following to your ~/.ssh/config" + display "" + display "Host heroku.#{name}" + display " HostName heroku.com" + display " IdentityFile /PATH/TO/PRIVATE/KEY" + display " IdentitiesOnly yes" + end end def remove @@ -92,6 +110,10 @@ def account_exists?(name) account_names.include?(name) end + def account_ssh_key(name) + "~/.ssh/identity.heroku.#{name}" + end + def read_account(name) YAML::load_file(account_file(name)) end