Skip to content

Commit d65b00b

Browse files
committed
Merge pull request #18 from chrishunt/add-tmux-option
Replace OptionParser with Thor, add --command option
2 parents 8f2dd78 + 0b53b40 commit d65b00b

File tree

11 files changed

+167
-142
lines changed

11 files changed

+167
-142
lines changed

Gemfile.lock

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ PATH
33
specs:
44
github-auth (2.0.0)
55
httparty (~> 0.11.0)
6+
thor (~> 0.18)
67

78
GEM
89
remote: https://rubygems.org/
@@ -11,6 +12,7 @@ GEM
1112
parallel
1213
cane-hashcheck (1.2.0)
1314
cane
15+
coderay (1.0.9)
1416
colorize (0.5.8)
1517
coveralls (0.6.7)
1618
colorize
@@ -24,10 +26,15 @@ GEM
2426
httparty (0.11.0)
2527
multi_json (~> 1.0)
2628
multi_xml (>= 0.5.2)
29+
method_source (0.8.2)
2730
mime-types (1.24)
2831
multi_json (1.7.9)
2932
multi_xml (0.5.5)
3033
parallel (0.9.0)
34+
pry (0.9.12.2)
35+
coderay (~> 1.0.5)
36+
method_source (~> 0.8)
37+
slop (~> 3.4)
3138
rack (1.5.2)
3239
rack-protection (1.5.0)
3340
rack
@@ -50,6 +57,7 @@ GEM
5057
rack (~> 1.4)
5158
rack-protection (~> 1.4)
5259
tilt (~> 1.3, >= 1.3.4)
60+
slop (3.4.6)
5361
thin (1.5.1)
5462
daemons (>= 1.0.9)
5563
eventmachine (>= 0.12.6)
@@ -66,6 +74,7 @@ DEPENDENCIES
6674
cane-hashcheck (~> 1.2.0)
6775
coveralls (~> 0.6.7)
6876
github-auth!
77+
pry (~> 0.9.12)
6978
rake (~> 10.1.0)
7079
rspec (~> 2.14)
7180
sinatra (~> 1.4.3)

README.md

Lines changed: 58 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -23,37 +23,45 @@ After you've [installed](#installation) `gh-auth`, you can give me ssh access
2323
with:
2424

2525
```bash
26-
$ gh-auth --add chrishunt
26+
$ gh-auth add --users=chrishunt
27+
Adding 2 key(s) to '/Users/chris/.ssh/authorized_keys'
28+
```
29+
30+
If you'd like me to automatically connect to your existing tmux session, you
31+
can do that with a custom ssh command:
32+
33+
```bash
34+
$ gh-auth add --users=chrishunt --command="tmux attach"
2735
Adding 2 key(s) to '/Users/chris/.ssh/authorized_keys'
2836
```
2937

3038
That was easy! When we're done working, you can revoke my access with:
3139

3240
```bash
33-
$ gh-auth --remove chrishunt
41+
$ gh-auth remove --users=chrishunt
3442
Removing 2 key(s) from '/Users/chris/.ssh/authorized_keys'
3543
```
3644

3745
You can add and remove any number of users at the same time.
3846

3947
```bash
40-
$ gh-auth --add chrishunt,zachmargolis
48+
$ gh-auth add --users=chrishunt zachmargolis
4149
Adding 4 key(s) to '/Users/chris/.ssh/authorized_keys'
4250

43-
$ gh-auth --list
44-
Added users: chrishunt, zachmargolis
51+
$ gh-auth list
52+
chrishunt zachmargolis
4553

46-
$ gh-auth --remove chrishunt
54+
$ gh-auth remove --users=chrishunt
4755
Removing 2 key(s) from '/Users/chris/.ssh/authorized_keys'
4856

49-
$ gh-auth --list
50-
Added users: zachmargolis
57+
$ gh-auth list
58+
zachmargolis
5159

52-
$ gh-auth --remove zachmargolis
60+
$ gh-auth remove --users=zachmargolis
5361
Removing 2 key(s) from '/Users/chris/.ssh/authorized_keys'
5462

55-
$ gh-auth --list
56-
Added users:
63+
$ gh-auth list
64+
5765
```
5866

5967
## Sections
@@ -80,14 +88,42 @@ Added users:
8088

8189
`gh-auth` can be used from the command line after the gem has been installed.
8290

83-
```bash
84-
usage: gh-auth [--version] [--list] [--add|--remove] <username>
91+
```
92+
$ gh-auth
93+
Commands:
94+
gh-auth add --users=one two three # Add GitHub users to authorized keys
95+
gh-auth help [COMMAND] # Describe available commands or one specific command
96+
gh-auth list # List all GitHub users already added to authorized keys
97+
gh-auth remove --users=one two three # Remove GitHub users from authorized keys
98+
gh-auth version # Show gh-auth version
99+
100+
Options:
101+
[--host=HOST]
102+
[--path=PATH]
103+
```
85104

86-
options:
87-
--add doug,sally Add GitHub users
88-
--remove doug,sally Remove GitHub users
89-
--list List all GitHub users added
90-
--version Show version
105+
Use the `help` command for help on a specific command.
106+
107+
```
108+
$ gh-auth help add
109+
Usage:
110+
gh-auth add --users=one two three
111+
112+
Options:
113+
--users=one two three
114+
[--command=COMMAND]
115+
[--host=HOST]
116+
[--path=PATH]
117+
118+
Description:
119+
`gh-auth add` is used to add one or more GitHub user's public SSH keys to ~/.ssh/authorized_keys. All keys stored on github.com for that user will be added.
120+
121+
> $ gh-auth add --users=chrishunt zachmargolis
122+
> Adding 6 key(s) to '/Users/chris/.ssh/authorized_keys'
123+
124+
By default, users will be granted normal shell access. If you'd like to specify an ssh command that should execute when the user connects, use the `--command` option.
125+
126+
> $ gh-auth add --users=chrishunt --command="tmux attach"
91127
```
92128

93129
### In Your Project
@@ -99,10 +135,10 @@ too.
99135
require 'github/auth'
100136

101137
# Add keys for GitHub user 'chrishunt'
102-
Github::Auth::CLI.new.execute %w(--add chrishunt)
138+
Github::Auth::CLI.start %w(add --users=chrishunt)
103139

104140
# Remove keys for GitHub user 'chrishunt'
105-
Github::Auth::CLI.new.execute %w(--remove chrishunt)
141+
Github::Auth::CLI.start %w(remove --users=chrishunt)
106142
```
107143

108144
## Installation
@@ -111,15 +147,6 @@ Install the `github-auth` gem:
111147

112148
```bash
113149
$ gem install github-auth
114-
115-
$ gh-auth
116-
usage: gh-auth [--version] [--list] [--add|--remove] <username>
117-
118-
options:
119-
--add doug,sally Add GitHub users
120-
--remove doug,sally Remove GitHub users
121-
--list List all GitHub users added
122-
--version Show version
123150
```
124151

125152
### SSH Public Key Authentication (Mac OS X)
@@ -147,7 +174,7 @@ First, authorize yourself for ssh. (Make sure to replace 'chrishunt' with
147174
*your* GitHub username)
148175

149176
```bash
150-
$ gh-auth --add chrishunt
177+
$ gh-auth add --users=chrishunt
151178
Adding 2 key(s) to '/Users/chris/.ssh/authorized_keys'
152179
```
153180

@@ -163,7 +190,7 @@ $ ssh -o PreferredAuthentications=publickey localhost
163190
Next, remove your public keys from the keys file:
164191

165192
```bash
166-
$ gh-auth --remove chrishunt
193+
$ gh-auth remove --users=chrishunt
167194
Removing 2 key(s) from '/Users/chris/.ssh/authorized_keys'
168195
```
169196

bin/gh-auth

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22

33
require 'github/auth'
44

5-
Github::Auth::CLI.new.execute(ARGV)
5+
Github::Auth::CLI.start ARGV

github-auth.gemspec

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,10 @@ Gem::Specification.new do |spec|
2424
spec.add_development_dependency 'coveralls', '~> 0.6.7'
2525
spec.add_development_dependency 'rake', '~> 10.1.0'
2626
spec.add_development_dependency 'rspec', '~> 2.14'
27+
spec.add_development_dependency 'pry', '~> 0.9.12'
2728
spec.add_development_dependency 'sinatra', '~> 1.4.3'
2829
spec.add_development_dependency 'thin', '~> 1.5.1'
2930

3031
spec.add_runtime_dependency 'httparty', '~> 0.11.0'
32+
spec.add_runtime_dependency 'thor', '~> 0.18'
3133
end

lib/github/auth.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,4 @@
22
require 'github/auth/key'
33
require 'github/auth/keys_client'
44
require 'github/auth/keys_file'
5-
require 'github/auth/options'
65
require 'github/auth/cli'

lib/github/auth/cli.rb

Lines changed: 55 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,77 @@
1+
require 'thor'
2+
13
module Github::Auth
24
# Command Line Interface for parsing and executing commands
3-
class CLI
4-
attr_reader :options
5+
class CLI < Thor
6+
class_option :host, type: :string
7+
class_option :path, type: :string
58

6-
def execute(args)
7-
@options = Options.new.parse(args)
8-
send options.command
9-
end
9+
option :users, type: :array, required: true
10+
option :command, type: :string
11+
desc 'add', 'Add GitHub users to authorized keys'
12+
long_desc <<-LONGDESC
13+
`gh-auth add` is used to add one or more GitHub user's public SSH keys
14+
to ~/.ssh/authorized_keys. All keys stored on github.com for that
15+
user will be added.
1016
11-
private
17+
> $ gh-auth add --users=chrishunt zachmargolis
18+
\x5> Adding 6 key(s) to '/Users/chris/.ssh/authorized_keys'
1219
20+
By default, users will be granted normal shell access. If you'd like to
21+
specify an ssh command that should execute when the user connects, use
22+
the `--command` option.
23+
24+
> $ gh-auth add --users=chrishunt --command="tmux attach"
25+
LONGDESC
1326
def add
1427
on_keys_file :write!,
15-
"Adding #{keys.count} key(s) to '#{keys_file.path}'"
28+
"Adding #{keys.count} key(s) to '#{keys_file.path}'",
29+
{ command: options[:command] }
1630
end
1731

32+
option :users, type: :array, required: true
33+
desc 'remove', 'Remove GitHub users from authorized keys'
34+
long_desc <<-LONGDESC
35+
`gh-auth remove` is used to remove one or more GitHub user's public SSH
36+
keys from ~/.ssh/authorized_keys. All keys stored on github.com for
37+
that user will be removed.
38+
39+
> $ gh-auth remove --users=chrishunt zachmargolis
40+
\x5> Removing 6 key(s) to '/Users/chris/.ssh/authorized_keys'
41+
LONGDESC
1842
def remove
1943
on_keys_file :delete!,
2044
"Removing #{keys.count} key(s) from '#{keys_file.path}'"
2145
end
2246

47+
desc 'list', 'List all GitHub users already added to authorized keys'
48+
long_desc <<-LONGDESC
49+
`gh-auth list` will list all GitHub users that have been added to
50+
~/.ssh/authorized_keys by `gh-auth`.
51+
52+
> $ gh-auth list
53+
\x5> chrishunt, zachmargolis
54+
LONGDESC
2355
def list
24-
puts "Added users: #{keys_file.github_users.join(', ')}"
56+
puts keys_file.github_users.join(' ')
2557
end
2658

59+
desc 'version', 'Show gh-auth version'
2760
def version
2861
puts Github::Auth::VERSION
2962
end
3063

31-
def usage
32-
puts options.usage
64+
private
65+
66+
def keys
67+
@keys ||= begin
68+
Array(options[:users]).map { |user| keys_for user }.flatten.compact
69+
end
3370
end
3471

35-
def on_keys_file(action, message)
72+
def on_keys_file(action, message, options = {})
3673
puts message
37-
rescue_keys_file_errors { keys_file.send action, keys }
74+
rescue_keys_file_errors { keys_file(options).send action, keys }
3875
end
3976

4077
def rescue_keys_file_errors
@@ -51,10 +88,6 @@ def rescue_keys_file_errors
5188
puts " $ touch #{keys_file.path}"
5289
end
5390

54-
def keys
55-
@keys ||= options.usernames.map { |user| keys_for user }.flatten.compact
56-
end
57-
5891
def keys_for(username)
5992
Github::Auth::KeysClient.new(
6093
hostname: github_hostname,
@@ -68,16 +101,17 @@ def keys_for(username)
68101
puts "https://status.github.com"
69102
end
70103

71-
def keys_file
72-
Github::Auth::KeysFile.new path: keys_file_path
104+
def keys_file(options = {})
105+
Github::Auth::KeysFile.new \
106+
options.merge path: keys_file_path
73107
end
74108

75109
def keys_file_path
76-
Github::Auth::KeysFile::DEFAULT_PATH
110+
options[:path] || Github::Auth::KeysFile::DEFAULT_PATH
77111
end
78112

79113
def github_hostname
80-
Github::Auth::KeysClient::DEFAULT_HOSTNAME
114+
options[:host] || Github::Auth::KeysClient::DEFAULT_HOSTNAME
81115
end
82116
end
83117
end

lib/github/auth/keys_file.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module Github::Auth
22
# Write and delete keys from the authorized_keys file
33
class KeysFile
4-
attr_reader :path
4+
attr_reader :path, :command
55

66
PermissionDeniedError = Class.new StandardError
77
FileDoesNotExistError = Class.new StandardError
@@ -10,6 +10,7 @@ class KeysFile
1010

1111
def initialize(options = {})
1212
@path = File.expand_path(options[:path] || DEFAULT_PATH)
13+
@command = options[:command]
1314
end
1415

1516
def write!(keys)
@@ -19,7 +20,7 @@ def write!(keys)
1920
unless keys_file_content.empty? || keys_file_content.end_with?("\n")
2021
keys_file.write "\n"
2122
end
22-
keys_file.write "#{key}\n"
23+
keys_file.write "#{"command=\"#{command}\" " if command}#{key}\n"
2324
end
2425
end
2526
end
@@ -62,7 +63,7 @@ def with_keys_file(mode, block)
6263
def keys_file_content_without(keys)
6364
keys_file_content.tap do |content|
6465
Array(keys).each do |key|
65-
content.gsub! /#{Regexp.escape key.key}( .*)?$\n?/, ''
66+
content.gsub! /(.*)?#{Regexp.escape key.key}(.*)?$\n?/, ''
6667
end
6768

6869
content << "\n" unless content.empty? || content.end_with?("\n")

0 commit comments

Comments
 (0)