Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,17 @@ You can add and remove any number of users at the same time.
$ gh-auth add chrishunt zachmargolis
Adding 4 key(s) to '/Users/chris/.ssh/authorized_keys'

$ gh-auth list
Added users: chrishunt, zachmargolis

$ gh-auth remove chrishunt
Removing 2 key(s) from '/Users/chris/.ssh/authorized_keys'

$ gh-auth remove zachmargolis
Removing 2 key(s) from '/Users/chris/.ssh/authorized_keys'

$ gh-auth list
Added users:
```

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

```bash
usage: gh-auth [--version] [add|remove] <username>
usage: gh-auth [--version] [add|remove|list] <username>
```

### In Your Project
Expand Down Expand Up @@ -102,7 +108,7 @@ Install the `github-auth` gem:
$ gem install github-auth

$ gh-auth
usage: gh-auth [--version] [add|remove] <username>
usage: gh-auth [--version] [add|remove|list] <username>
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome, thanks for updating the docs too. 🎉

```

### SSH Public Key Authentication (Mac OS X)
Expand Down
18 changes: 16 additions & 2 deletions lib/github/auth/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ module Github::Auth
class CLI
attr_reader :command, :usernames

COMMANDS = %w(add remove)
COMMANDS = %w(add remove list)

def initialize(argv)
@command = argv.shift
@usernames = argv
end

def execute
if COMMANDS.include?(command) && !usernames.empty?
if COMMANDS.include?(command)
send command
elsif command == '--version'
print_version
Expand All @@ -23,15 +23,29 @@ def execute
private

def add
if usernames.empty?
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm kind of bummed we have to to check usernames.empty? twice now in both add and remove, but that's bad design on my part. Sorry about that. I should switch over to OptionParser after we get this merged in.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds like a plan 👍

print_usage
return
end

on_keys_file :write!,
"Adding #{keys.count} key(s) to '#{keys_file.path}'"
end

def remove
if usernames.empty?
print_usage
return
end

on_keys_file :delete!,
"Removing #{keys.count} key(s) from '#{keys_file.path}'"
end

def list
puts "Added users: #{keys_file.github_users.join(', ')}"
end

def on_keys_file(action, message)
puts message
rescue_keys_file_errors { keys_file.send action, keys }
Expand Down
6 changes: 6 additions & 0 deletions lib/github/auth/keys_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ def delete!(keys)
write_keys_file { |keys_file| keys_file.write new_content }
end

def github_users
# http://rubular.com/r/zXCkewmm0i
regex = %r{github\.com/(\S+)}
keys_file_content.scan(regex).flatten.uniq.sort
end

private

def append_keys_file(&block)
Expand Down
11 changes: 11 additions & 0 deletions spec/acceptance/github/auth/cli_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'spec_helper'
require 'support/capture_stdout'
require 'support/mock_github_server'
require 'github/auth'

Expand Down Expand Up @@ -32,5 +33,15 @@ def cli(argv)

keys_file.unlink
end

it 'lists users from the keys file' do
cli(%w(add chrishunt)).execute

output = capture_stdout do
cli(%w(list)).execute
end

expect(output).to include('chrishunt')
end
end
end
11 changes: 11 additions & 0 deletions spec/support/capture_stdout.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require 'stringio'

def capture_stdout
captured_output = StringIO.new
real_stdout = $stdout
$stdout = captured_output
yield
captured_output.string
ensure
$stdout = real_stdout
end
18 changes: 18 additions & 0 deletions spec/unit/github/auth/keys_file_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -197,4 +197,22 @@
end
end
end

describe '#github_users' do
let(:keys) {[
Github::Auth::Key.new('jay', 'abc123'),
Github::Auth::Key.new('chris', 'def456'),
Github::Auth::Key.new('chris', 'ghi789'),
]}

before do
keys_file.write keys.join("\n")
keys_file.write "\n"
keys_file.rewind
end

it 'returns a uniq, ordered list of github users' do
expect(subject.github_users).to eq(%w(chris jay))
end
end
end