public
Last active

  • Download Gist
ghkey.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
#!/usr/bin/env ruby
 
require 'rubygems'
require 'choice'
 
Choice.options do
header ''
header 'options:'
option :phrase do
short '-p'
long '--passphrase=PHRASE'
desc 'Passphrase to be used to create you SSH key'
end
option :user do
short '-u'
long '--user=USER'
desc 'Your GitHub user name'
end
option :token do
short '-t'
long '--token=TOKEN'
desc 'Your GitHub token'
end
option :email do
short '-e'
long '--email=EMAIL'
desc 'The email to be used with git'
end
option :name do
short '-n'
long '--name=NAME'
desc 'Name to be used with git'
end
end
 
key = "#{ENV['HOME']}/.ssh/id_rsa"
pubkey = "#{key}.pub"
if !File.exist?(pubkey)
`ssh-keygen -t rsa -N "#{Choice.choices[:phrase]}" -f #{key}`
end
 
gitconfig = "#{ENV['HOME']}/.gitconfig"
if !File.exist?(gitconfig)
if not Choice.choices[:user] or not Choice.choices[:token]
raise "Missing #{gitconfig}. Use ghkey --help to see options on how to create one."
end
`git config --global github.user #{Choice.choices[:user]}`
`git config --global github.token #{Choice.choices[:token]}`
 
`git config --global user.name #{Choice.choices[:name]}` if Choice.choices[:name]
`git config --global user.email #{Choice.choices[:email]}` if Choice.choices[:email]
end
 
sshkey = File.open(pubkey, 'r') { |f| f.read }
hostname = `hostname`
`curl -is --basic -u "#{Choice.choices[:user]}/token:#{Choice.choices[:token]}" http://github.com/api/v2/json/user/key/add -F "title=#{hostname}" -F "key=#{sshkey}"`

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.