Skip to content

Commit

Permalink
Implemented add, remove, switch, and setup.
Browse files Browse the repository at this point in the history
  • Loading branch information
tylerhunt committed Jun 9, 2010
1 parent 4f2668f commit 6a1f057
Showing 1 changed file with 117 additions and 6 deletions.
123 changes: 117 additions & 6 deletions lib/heroku_accounts_command.rb
@@ -1,34 +1,144 @@
require 'ftools'
require 'fileutils' require 'fileutils'
require 'heroku/commands/auth'


module Heroku::Command module Heroku::Command
class Accounts < BaseWithApp class Accounts < BaseWithApp
CURRENT_DIR = 'current_account'
HEROKU_DIR = '.heroku'

def index def index
setup setup


Dir[File.join(accounts_directory, '*')].each do |path| Dir[File.join(accounts_path, '*')].each do |path|
display File.basename(path) display File.basename(path)
end end
end end


def add def add
if account = args.first
unless File.directory?(account_path(account))
display "Creating account #{account}"

account_path = account_path(account)
FileUtils.mkdir_p(account_path)

ssh_key = File.join(account_path, 'id_rsa')
`ssh-keygen -f #{ssh_key} -N '' > /dev/null`

Class.new(Heroku::Command::Auth).tap do |auth|
auth.class_eval <<-end_eval
def credentials_file
"#{File.join(account_path, 'credentials')}"
end
end_eval

auth.new(["#{ssh_key}.pub"]).get_credentials
end

Heroku::Command.run_internal('accounts:switch', [account])
end
else
display "Usage: heroku accounts:add account_name"
end
end end


def switch def switch
if account = args.first
if account?(account)
Dir.chdir(heroku_path)
FileUtils.rm(CURRENT_DIR)
File.symlink(File.join('accounts', account), CURRENT_DIR)
display "Switched to #{account}"
end
else
display "Usage: heroku switch account_name"
end
end end


def remove def remove
if account?(args.first)
FileUtils.rm_rf(account_path(args.first), :secure => true)
else
display "Usage: heroku accounts:remove account_name"
end
end end


def setup def setup
unless File.directory?(accounts_directory) unless File.directory?(accounts_path)
FileUtils.mkdir_p(accounts_directory) if account = args.first
display "Creating initial account #{account}"

account_path = account_path(account)
FileUtils.mkdir_p(account_path)

Dir.chdir(heroku_path)
File.symlink(File.join('accounts', account), CURRENT_DIR)

Dir.glob(File.join(ssh_path, 'id_*')).each do |file|
File.cp(file, File.join(account_path, File.basename(file)))
end

file = File.join(heroku_path, 'credentials')
File.mv(file, File.join(account_path, File.basename(file)))

Dir.glob(File.join(account_path, '*')).each do |file|
File.symlink(
File.join(CURRENT_DIR, File.basename(file)),
File.basename(file)
)
end

update_ssh_config
else
display "Usage: heroku accounts:setup account_name"
end
end
end

def update_ssh_config
config_path = File.join(ssh_path, 'config')
config_exists = File.exists?(config_path)
config_done = File.read(config_path) =~ /Host\s+heroku.com/

unless config_exists && config_done
File.open(config_path, 'a') do |file|
file << "\nHost heroku.com\n IdentityFile ~/#{HEROKU_DIR}/id_rsa\n"
end

display "Added heroku.com to ~/.ssh/config"
end
end
private :update_ssh_config

def heroku_path
@heroku_path ||= File.join(home_directory, HEROKU_DIR)
end
private :heroku_path

def accounts_path
@accounts_path ||= File.join(heroku_path, 'accounts')
end
private :accounts_path

def account_path(account)
File.join(accounts_path, account)
end
private :account_path

def account?(account)
if account
File.exists?(account_path(account)).tap do |exists|
display "Account not found: #{account}" unless exists
end
end end
end end
private :setup private :account?


def accounts_directory def ssh_path
@accounts_directory ||= File.join(home_directory, '.heroku', 'accounts') @ssh_path ||= File.join(home_directory, '.ssh')
end end
private :ssh_path
end end
end end


Expand All @@ -37,4 +147,5 @@ def accounts_directory
group.command('accounts:add', 'adds a new account') group.command('accounts:add', 'adds a new account')
group.command('accounts:switch', 'switches accounts') group.command('accounts:switch', 'switches accounts')
group.command('accounts:remove', 'removes a new account') group.command('accounts:remove', 'removes a new account')
group.command('accounts:setup', 'sets up multiple accounts')
end end

0 comments on commit 6a1f057

Please sign in to comment.