Skip to content

Commit d03fab3

Browse files
committed
Merge pull request #14 from iamvery/add-list-command
Add list command
2 parents e0cfd3a + 252e94f commit d03fab3

6 files changed

Lines changed: 70 additions & 4 deletions

File tree

README.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,17 @@ You can add and remove any number of users at the same time.
4040
$ gh-auth add chrishunt zachmargolis
4141
Adding 4 key(s) to '/Users/chris/.ssh/authorized_keys'
4242

43+
$ gh-auth list
44+
Added users: chrishunt, zachmargolis
45+
4346
$ gh-auth remove chrishunt
4447
Removing 2 key(s) from '/Users/chris/.ssh/authorized_keys'
4548

4649
$ gh-auth remove zachmargolis
4750
Removing 2 key(s) from '/Users/chris/.ssh/authorized_keys'
51+
52+
$ gh-auth list
53+
Added users:
4854
```
4955

5056
## Sections
@@ -72,7 +78,7 @@ Removing 2 key(s) from '/Users/chris/.ssh/authorized_keys'
7278
`gh-auth` can be used from the command line after the gem has been installed.
7379

7480
```bash
75-
usage: gh-auth [--version] [add|remove] <username>
81+
usage: gh-auth [--version] [add|remove|list] <username>
7682
```
7783

7884
### In Your Project
@@ -102,7 +108,7 @@ Install the `github-auth` gem:
102108
$ gem install github-auth
103109

104110
$ gh-auth
105-
usage: gh-auth [--version] [add|remove] <username>
111+
usage: gh-auth [--version] [add|remove|list] <username>
106112
```
107113

108114
### SSH Public Key Authentication (Mac OS X)

lib/github/auth/cli.rb

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@ module Github::Auth
33
class CLI
44
attr_reader :command, :usernames
55

6-
COMMANDS = %w(add remove)
6+
COMMANDS = %w(add remove list)
77

88
def initialize(argv)
99
@command = argv.shift
1010
@usernames = argv
1111
end
1212

1313
def execute
14-
if COMMANDS.include?(command) && !usernames.empty?
14+
if COMMANDS.include?(command)
1515
send command
1616
elsif command == '--version'
1717
print_version
@@ -23,15 +23,29 @@ def execute
2323
private
2424

2525
def add
26+
if usernames.empty?
27+
print_usage
28+
return
29+
end
30+
2631
on_keys_file :write!,
2732
"Adding #{keys.count} key(s) to '#{keys_file.path}'"
2833
end
2934

3035
def remove
36+
if usernames.empty?
37+
print_usage
38+
return
39+
end
40+
3141
on_keys_file :delete!,
3242
"Removing #{keys.count} key(s) from '#{keys_file.path}'"
3343
end
3444

45+
def list
46+
puts "Added users: #{keys_file.github_users.join(', ')}"
47+
end
48+
3549
def on_keys_file(action, message)
3650
puts message
3751
rescue_keys_file_errors { keys_file.send action, keys }

lib/github/auth/keys_file.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ def delete!(keys)
3131
write_keys_file { |keys_file| keys_file.write new_content }
3232
end
3333

34+
def github_users
35+
# http://rubular.com/r/zXCkewmm0i
36+
regex = %r{github\.com/(\S+)}
37+
keys_file_content.scan(regex).flatten.uniq.sort
38+
end
39+
3440
private
3541

3642
def append_keys_file(&block)

spec/acceptance/github/auth/cli_spec.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require 'spec_helper'
2+
require 'support/capture_stdout'
23
require 'support/mock_github_server'
34
require 'github/auth'
45

@@ -32,5 +33,15 @@ def cli(argv)
3233

3334
keys_file.unlink
3435
end
36+
37+
it 'lists users from the keys file' do
38+
cli(%w(add chrishunt)).execute
39+
40+
output = capture_stdout do
41+
cli(%w(list)).execute
42+
end
43+
44+
expect(output).to include('chrishunt')
45+
end
3546
end
3647
end

spec/support/capture_stdout.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
require 'stringio'
2+
3+
def capture_stdout
4+
captured_output = StringIO.new
5+
real_stdout = $stdout
6+
$stdout = captured_output
7+
yield
8+
captured_output.string
9+
ensure
10+
$stdout = real_stdout
11+
end

spec/unit/github/auth/keys_file_spec.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,4 +197,22 @@
197197
end
198198
end
199199
end
200+
201+
describe '#github_users' do
202+
let(:keys) {[
203+
Github::Auth::Key.new('jay', 'abc123'),
204+
Github::Auth::Key.new('chris', 'def456'),
205+
Github::Auth::Key.new('chris', 'ghi789'),
206+
]}
207+
208+
before do
209+
keys_file.write keys.join("\n")
210+
keys_file.write "\n"
211+
keys_file.rewind
212+
end
213+
214+
it 'returns a uniq, ordered list of github users' do
215+
expect(subject.github_users).to eq(%w(chris jay))
216+
end
217+
end
200218
end

0 commit comments

Comments
 (0)