diff --git a/README.md b/README.md index 591277b..e61879d 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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] +usage: gh-auth [--version] [add|remove|list] ``` ### In Your Project @@ -102,7 +108,7 @@ Install the `github-auth` gem: $ gem install github-auth $ gh-auth -usage: gh-auth [--version] [add|remove] +usage: gh-auth [--version] [add|remove|list] ``` ### SSH Public Key Authentication (Mac OS X) diff --git a/lib/github/auth/cli.rb b/lib/github/auth/cli.rb index ee54cbd..81a486a 100644 --- a/lib/github/auth/cli.rb +++ b/lib/github/auth/cli.rb @@ -3,7 +3,7 @@ module Github::Auth class CLI attr_reader :command, :usernames - COMMANDS = %w(add remove) + COMMANDS = %w(add remove list) def initialize(argv) @command = argv.shift @@ -11,7 +11,7 @@ def initialize(argv) end def execute - if COMMANDS.include?(command) && !usernames.empty? + if COMMANDS.include?(command) send command elsif command == '--version' print_version @@ -23,15 +23,29 @@ def execute private def add + if usernames.empty? + 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 } diff --git a/lib/github/auth/keys_file.rb b/lib/github/auth/keys_file.rb index bcca720..25db857 100644 --- a/lib/github/auth/keys_file.rb +++ b/lib/github/auth/keys_file.rb @@ -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) diff --git a/spec/acceptance/github/auth/cli_spec.rb b/spec/acceptance/github/auth/cli_spec.rb index ced94be..495b419 100644 --- a/spec/acceptance/github/auth/cli_spec.rb +++ b/spec/acceptance/github/auth/cli_spec.rb @@ -1,4 +1,5 @@ require 'spec_helper' +require 'support/capture_stdout' require 'support/mock_github_server' require 'github/auth' @@ -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 diff --git a/spec/support/capture_stdout.rb b/spec/support/capture_stdout.rb new file mode 100644 index 0000000..3892054 --- /dev/null +++ b/spec/support/capture_stdout.rb @@ -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 diff --git a/spec/unit/github/auth/keys_file_spec.rb b/spec/unit/github/auth/keys_file_spec.rb index 29554aa..ce175da 100644 --- a/spec/unit/github/auth/keys_file_spec.rb +++ b/spec/unit/github/auth/keys_file_spec.rb @@ -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