Skip to content

Instantly share code, notes, and snippets.

@mdippery
Created January 25, 2012 02:21
Show Gist options
  • Save mdippery/1674216 to your computer and use it in GitHub Desktop.
Save mdippery/1674216 to your computer and use it in GitHub Desktop.
Switch between multiple Heroku accounts with ease
#!/usr/bin/env ruby
require 'fileutils'
def die(code=1)
$stderr.puts 'Usage: herokusu [--list | --switch user]'
exit code
end
HEROKU_PATH = File.expand_path '~/.heroku'
CREDENTIALS_PATH = File.join HEROKU_PATH, 'credentials'
die if ARGV.length > 2
option = ARGV.shift
if ARGV.length == 0 or option == '--verbose'
real_file = File.readlink CREDENTIALS_PATH
user = real_file.split('.')[-1]
print user
if option == '--verbose'
File.open(CREDENTIALS_PATH) do |f|
email = f.gets.chomp
print " (#{email})"
end
end
puts
exit 0
end
if option == '--list'
Dir.chdir(HEROKU_PATH) do
Dir['credentials.*'].each do |f|
user = f.split('.')[-1]
puts user
end
end
exit 0
elsif option == '--switch'
die if ARGV.length != 1
user = ARGV.shift
credentials = File.join HEROKU_PATH, "credentials.#{user}"
Dir.chdir(HEROKU_PATH) do
FileUtils.ln_sf credentials, CREDENTIALS_PATH
end
puts "Switched to #{credentials}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment