fcoury (owner)

Forks

Revisions

gist: 102278 Download_button fork
public
Description:
Automates the generation and inclusing of a SSH Public Key to GitHub
Public Clone URL: git://gist.github.com/102278.git
Embed All Files: show embed
ghkey.rb #
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
63
64
65
66
#!/usr/bin/env ruby
 
require 'rubygems'
require 'octopi'
require 'choice'
 
include Octopi
 
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
 
authenticated do |g|
  sshkey = File.open(pubkey, 'r') { |f| f.read }
  g.user.add_key `hostname`, sshkey
end