From 3332d866077aeb958edba5b1e426a51c1a973c47 Mon Sep 17 00:00:00 2001 From: Jay Hayes Date: Fri, 4 Oct 2013 09:31:42 -0500 Subject: [PATCH 1/9] Move username check to methods that require usernames * We're introducing a "list" method which requires no username argument --- lib/github/auth/cli.rb | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/github/auth/cli.rb b/lib/github/auth/cli.rb index ee54cbd..d0cbb9e 100644 --- a/lib/github/auth/cli.rb +++ b/lib/github/auth/cli.rb @@ -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,11 +23,21 @@ 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 From 31a5d51ac3862e9380807dcac1e1071659b76e25 Mon Sep 17 00:00:00 2001 From: Jay Hayes Date: Fri, 4 Oct 2013 09:32:53 -0500 Subject: [PATCH 2/9] Add spec helper for capturing stdout to a string * Shout out to @avdi for his awesome RubyTapas! * http://www.rubytapas.com/episodes/29-Redirecting-Output --- spec/support/capture_stdout.rb | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 spec/support/capture_stdout.rb diff --git a/spec/support/capture_stdout.rb b/spec/support/capture_stdout.rb new file mode 100644 index 0000000..f603dd5 --- /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 +ensure + $stdout = real_stdout + return captured_output.string +end From f54ce8d3e37639972e24e8a76630eb54d4d7cf40 Mon Sep 17 00:00:00 2001 From: Jay Hayes Date: Fri, 4 Oct 2013 09:37:56 -0500 Subject: [PATCH 3/9] Add method to KeysFile for getting github users * This returns a list of only the github users which have been added to your keys file. --- lib/github/auth/keys_file.rb | 6 ++++++ spec/unit/github/auth/keys_file_spec.rb | 18 ++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/lib/github/auth/keys_file.rb b/lib/github/auth/keys_file.rb index bcca720..b216ee1 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/2LjIGaKObz + regex = %r{github\.com/(.+)} + keys_file_content.scan(regex).flatten.uniq.sort + end + private def append_keys_file(&block) 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 From db7dfd49bfad8bf6d0653a7539f6a490c404a7df Mon Sep 17 00:00:00 2001 From: Jay Hayes Date: Fri, 4 Oct 2013 09:40:43 -0500 Subject: [PATCH 4/9] Add 'list' command to CLI * This will list all the users you've added to your keys file with this gem. --- lib/github/auth/cli.rb | 6 +++++- spec/acceptance/github/auth/cli_spec.rb | 11 +++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/lib/github/auth/cli.rb b/lib/github/auth/cli.rb index d0cbb9e..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 @@ -42,6 +42,10 @@ def remove "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/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 From 447795aa6745072024be3552e96f92f6856fd15a Mon Sep 17 00:00:00 2001 From: Jay Hayes Date: Fri, 4 Oct 2013 09:43:42 -0500 Subject: [PATCH 5/9] Updated README to include 'list' example and usage --- README.md | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 591277b..530f121 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,9 @@ with: ```bash $ gh-auth add chrishunt Adding 2 key(s) to '/Users/chris/.ssh/authorized_keys' + +$ gh-auth list +Added users: chrishunt, zachmargolis ``` That was easy! When we're done working, you can revoke my access with: @@ -72,7 +75,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 +105,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) From 52a5cc352bba78c813119fc5d5e84abd5d770ecc Mon Sep 17 00:00:00 2001 From: Jay Hayes Date: Tue, 22 Oct 2013 05:50:20 -0500 Subject: [PATCH 6/9] Move "list" example further down in README * This improves the "flow" of the examples by showing the use of `list` at the point in the example where two users have been added. [#14] --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 530f121..4c2923f 100644 --- a/README.md +++ b/README.md @@ -25,9 +25,6 @@ with: ```bash $ gh-auth add chrishunt Adding 2 key(s) to '/Users/chris/.ssh/authorized_keys' - -$ gh-auth list -Added users: chrishunt, zachmargolis ``` That was easy! When we're done working, you can revoke my access with: @@ -43,6 +40,9 @@ 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' From 99ea0a56356887f905579170c5c3477493213225 Mon Sep 17 00:00:00 2001 From: Jay Hayes Date: Tue, 22 Oct 2013 05:54:27 -0500 Subject: [PATCH 7/9] Add final `list` example to README * Per the natural flow of the example: 1. Add users 2. List users 3. Remove users 4. List users [#14] --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index 4c2923f..e61879d 100644 --- a/README.md +++ b/README.md @@ -48,6 +48,9 @@ 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 From 38577a108f6e8288d15ce83533b850a486e4fcae Mon Sep 17 00:00:00 2001 From: Jay Hayes Date: Tue, 22 Oct 2013 05:59:01 -0500 Subject: [PATCH 8/9] Use slightly more strict \S for gh user regex [#14] --- lib/github/auth/keys_file.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/github/auth/keys_file.rb b/lib/github/auth/keys_file.rb index b216ee1..25db857 100644 --- a/lib/github/auth/keys_file.rb +++ b/lib/github/auth/keys_file.rb @@ -32,8 +32,8 @@ def delete!(keys) end def github_users - # http://rubular.com/r/2LjIGaKObz - regex = %r{github\.com/(.+)} + # http://rubular.com/r/zXCkewmm0i + regex = %r{github\.com/(\S+)} keys_file_content.scan(regex).flatten.uniq.sort end From 252e94f23963d01ca754d759a3953f14f789b422 Mon Sep 17 00:00:00 2001 From: Jay Hayes Date: Tue, 22 Oct 2013 11:36:21 -0500 Subject: [PATCH 9/9] Remove explicit return from `ensure` clause * There is a subtle difference in functionality, but it doesn't affect the implementation here. * See https://github.com/chrishunt/github-auth/pull/14#discussion_r7122394 for more information. [#14] --- spec/support/capture_stdout.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/support/capture_stdout.rb b/spec/support/capture_stdout.rb index f603dd5..3892054 100644 --- a/spec/support/capture_stdout.rb +++ b/spec/support/capture_stdout.rb @@ -5,7 +5,7 @@ def capture_stdout real_stdout = $stdout $stdout = captured_output yield + captured_output.string ensure $stdout = real_stdout - return captured_output.string end