From cb084292db4f3dd1cbc8d886c35cd2860adbaac7 Mon Sep 17 00:00:00 2001 From: Chris Hunt Date: Thu, 24 Oct 2013 15:50:31 -0700 Subject: [PATCH 01/17] Add --tmux option --- lib/github/auth/cli.rb | 6 +++++- lib/github/auth/keys_file.rb | 12 ++++++++++-- lib/github/auth/options.rb | 9 +++++++++ spec/acceptance/github/auth/cli_spec.rb | 6 ++++++ spec/unit/github/auth/keys_file_spec.rb | 16 +++++++++++++++- 5 files changed, 45 insertions(+), 4 deletions(-) diff --git a/lib/github/auth/cli.rb b/lib/github/auth/cli.rb index f5ecb69..cf469aa 100644 --- a/lib/github/auth/cli.rb +++ b/lib/github/auth/cli.rb @@ -69,7 +69,11 @@ def keys_for(username) end def keys_file - Github::Auth::KeysFile.new path: keys_file_path + Github::Auth::KeysFile.new keys_file_options + end + + def keys_file_options + options.keys_file.merge path: keys_file_path end def keys_file_path diff --git a/lib/github/auth/keys_file.rb b/lib/github/auth/keys_file.rb index 25db857..63fb2c7 100644 --- a/lib/github/auth/keys_file.rb +++ b/lib/github/auth/keys_file.rb @@ -1,14 +1,22 @@ module Github::Auth # Write and delete keys from the authorized_keys file class KeysFile - attr_reader :path + attr_reader :path, :options PermissionDeniedError = Class.new StandardError FileDoesNotExistError = Class.new StandardError DEFAULT_PATH = '~/.ssh/authorized_keys' + TMUX_COMMAND = [ + 'command="tmux attach"', + 'no-port-forwarding', + 'no-X11-forwarding', + 'no-agent-forwarding' + ].join(',') + def initialize(options = {}) + @options = options @path = File.expand_path(options[:path] || DEFAULT_PATH) end @@ -19,7 +27,7 @@ def write!(keys) unless keys_file_content.empty? || keys_file_content.end_with?("\n") keys_file.write "\n" end - keys_file.write "#{key}\n" + keys_file.write "#{"#{TMUX_COMMAND} " if options[:tmux]}#{key}\n" end end end diff --git a/lib/github/auth/options.rb b/lib/github/auth/options.rb index e90332e..a59093d 100644 --- a/lib/github/auth/options.rb +++ b/lib/github/auth/options.rb @@ -11,6 +11,7 @@ def initialize 'usage: gh-auth', '[--version]', '[--list]', + '[--tmux]', '[--add|--remove]', '' ].join(' ') @@ -37,6 +38,10 @@ def initialize @command = 'list' end + opts.on('--tmux', 'Attach user to tmux session') do + keys_file.merge!(tmux: true) + end + opts.on('--version', 'Show version') do @command = 'version' end @@ -51,6 +56,10 @@ def usernames @usernames ||= [] end + def keys_file + @keys_file ||= {} + end + def usage parser.help end diff --git a/spec/acceptance/github/auth/cli_spec.rb b/spec/acceptance/github/auth/cli_spec.rb index 6a01b6d..f6d5fd0 100644 --- a/spec/acceptance/github/auth/cli_spec.rb +++ b/spec/acceptance/github/auth/cli_spec.rb @@ -52,6 +52,12 @@ def cli expect(output).to include Github::Auth::VERSION end + it 'can automatically attach users to a tmux session' do + cli.execute %w(--tmux --add chrishunt) + + expect(keys_file.read).to include Github::Auth::KeysFile::TMUX_COMMAND + end + it 'prints usage for invalid arguments' do [[], %w(invalid), %w(--add)].each do |invalid_arguments| expect( diff --git a/spec/unit/github/auth/keys_file_spec.rb b/spec/unit/github/auth/keys_file_spec.rb index ce175da..feb4785 100644 --- a/spec/unit/github/auth/keys_file_spec.rb +++ b/spec/unit/github/auth/keys_file_spec.rb @@ -4,9 +4,10 @@ require 'github/auth/keys_file' describe Github::Auth::KeysFile do - subject { described_class.new path: path } + subject { described_class.new(options) } let(:keys_file) { Tempfile.new 'authorized_keys' } + let(:options) {{ path: path }} let(:path) { keys_file.path } after { keys_file.unlink } # clean up, delete tempfile @@ -78,6 +79,19 @@ it_should_behave_like 'a successful key addition' end + context 'when the tmux option is provided' do + let(:options) {{ path: path, tmux: true }} + let(:keys) {[ Github::Auth::Key.new('chris', 'abc123') ]} + + it_should_behave_like 'a successful key addition' + + it 'prefixes the key with the tmux command' do + subject.write! keys + + expect(keys_file.read).to include Github::Auth::KeysFile::TMUX_COMMAND + end + end + context 'with existing keys in the keys file' do let(:existing_keys) { %w(abc123 def456 ghi789) } let(:keys) {[ Github::Auth::Key.new('chris', 'jkl012') ]} From 679fb7b39cc8256f677478e3a4adcf3b712f6625 Mon Sep 17 00:00:00 2001 From: Chris Hunt Date: Thu, 24 Oct 2013 16:02:00 -0700 Subject: [PATCH 02/17] Add --tmux usage to README --- README.md | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 96b28bf..20cbea4 100644 --- a/README.md +++ b/README.md @@ -27,6 +27,14 @@ $ gh-auth --add chrishunt Adding 2 key(s) to '/Users/chris/.ssh/authorized_keys' ``` +If you'd like me to automatically connect to your existing tmux session, then +provide the `--tmux` option as well: + +```bash +$ gh-auth --tmux --add chrishunt +Adding 2 key(s) to '/Users/chris/.ssh/authorized_keys' +``` + That was easy! When we're done working, you can revoke my access with: ```bash @@ -81,12 +89,13 @@ Added users: `gh-auth` can be used from the command line after the gem has been installed. ```bash -usage: gh-auth [--version] [--list] [--add|--remove] +usage: gh-auth [--version] [--list] [--tmux] [--add|--remove] options: --add doug,sally Add GitHub users --remove doug,sally Remove GitHub users --list List all GitHub users added + --tmux Attach user to tmux session --version Show version ``` @@ -113,12 +122,13 @@ Install the `github-auth` gem: $ gem install github-auth $ gh-auth -usage: gh-auth [--version] [--list] [--add|--remove] +usage: gh-auth [--version] [--list] [--tmux] [--add|--remove] options: --add doug,sally Add GitHub users --remove doug,sally Remove GitHub users --list List all GitHub users added + --tmux Attach user to tmux session --version Show version ``` From 5117dfbd9f5ce2cd92f3da859c9e6f419f49c7b7 Mon Sep 17 00:00:00 2001 From: Chris Hunt Date: Thu, 24 Oct 2013 16:14:34 -0700 Subject: [PATCH 03/17] Add 'pry' as development dependency --- Gemfile.lock | 8 ++++++++ github-auth.gemspec | 1 + spec/spec_helper.rb | 1 + 3 files changed, 10 insertions(+) diff --git a/Gemfile.lock b/Gemfile.lock index dd332e9..96c9f96 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -11,6 +11,7 @@ GEM parallel cane-hashcheck (1.2.0) cane + coderay (1.0.9) colorize (0.5.8) coveralls (0.6.7) colorize @@ -24,10 +25,15 @@ GEM httparty (0.11.0) multi_json (~> 1.0) multi_xml (>= 0.5.2) + method_source (0.8.2) mime-types (1.24) multi_json (1.7.9) multi_xml (0.5.5) parallel (0.9.0) + pry (0.9.12.2) + coderay (~> 1.0.5) + method_source (~> 0.8) + slop (~> 3.4) rack (1.5.2) rack-protection (1.5.0) rack @@ -50,6 +56,7 @@ GEM rack (~> 1.4) rack-protection (~> 1.4) tilt (~> 1.3, >= 1.3.4) + slop (3.4.6) thin (1.5.1) daemons (>= 1.0.9) eventmachine (>= 0.12.6) @@ -66,6 +73,7 @@ DEPENDENCIES cane-hashcheck (~> 1.2.0) coveralls (~> 0.6.7) github-auth! + pry (~> 0.9.12) rake (~> 10.1.0) rspec (~> 2.14) sinatra (~> 1.4.3) diff --git a/github-auth.gemspec b/github-auth.gemspec index 7045c44..0eef2fe 100644 --- a/github-auth.gemspec +++ b/github-auth.gemspec @@ -24,6 +24,7 @@ Gem::Specification.new do |spec| spec.add_development_dependency 'coveralls', '~> 0.6.7' spec.add_development_dependency 'rake', '~> 10.1.0' spec.add_development_dependency 'rspec', '~> 2.14' + spec.add_development_dependency 'pry', '~> 0.9.12' spec.add_development_dependency 'sinatra', '~> 1.4.3' spec.add_development_dependency 'thin', '~> 1.5.1' diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 54a6989..0894e9b 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,2 +1,3 @@ +require 'pry' require 'coveralls' Coveralls.wear! From d147ec626f520c96a373a0eb67462ce4023a0a8a Mon Sep 17 00:00:00 2001 From: Chris Hunt Date: Thu, 24 Oct 2013 16:33:07 -0700 Subject: [PATCH 04/17] Remove tmux enabled keys from keys file --- lib/github/auth/keys_file.rb | 2 +- spec/acceptance/github/auth/cli_spec.rb | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/github/auth/keys_file.rb b/lib/github/auth/keys_file.rb index 63fb2c7..1b0f6f7 100644 --- a/lib/github/auth/keys_file.rb +++ b/lib/github/auth/keys_file.rb @@ -70,7 +70,7 @@ def with_keys_file(mode, block) def keys_file_content_without(keys) keys_file_content.tap do |content| Array(keys).each do |key| - content.gsub! /#{Regexp.escape key.key}( .*)?$\n?/, '' + content.gsub! /(.*)?#{Regexp.escape key.key}(.*)?$\n?/, '' end content << "\n" unless content.empty? || content.end_with?("\n") diff --git a/spec/acceptance/github/auth/cli_spec.rb b/spec/acceptance/github/auth/cli_spec.rb index f6d5fd0..0f7820d 100644 --- a/spec/acceptance/github/auth/cli_spec.rb +++ b/spec/acceptance/github/auth/cli_spec.rb @@ -56,6 +56,11 @@ def cli cli.execute %w(--tmux --add chrishunt) expect(keys_file.read).to include Github::Auth::KeysFile::TMUX_COMMAND + + keys_file.rewind + cli.execute %w(--remove chrishunt) + + expect(keys_file.read.strip).to be_empty end it 'prints usage for invalid arguments' do From 064b52b11dfc750f6c26e9ee70bf93b222b53f95 Mon Sep 17 00:00:00 2001 From: Chris Hunt Date: Thu, 24 Oct 2013 18:07:28 -0700 Subject: [PATCH 05/17] Remove --tmux option; use --command instead --- lib/github/auth/cli.rb | 2 +- lib/github/auth/keys_file.rb | 13 +++---------- lib/github/auth/options.rb | 11 ++++++----- spec/acceptance/github/auth/cli_spec.rb | 22 +++++++++++----------- spec/unit/github/auth/keys_file_spec.rb | 8 ++++---- 5 files changed, 25 insertions(+), 31 deletions(-) diff --git a/lib/github/auth/cli.rb b/lib/github/auth/cli.rb index cf469aa..472d370 100644 --- a/lib/github/auth/cli.rb +++ b/lib/github/auth/cli.rb @@ -73,7 +73,7 @@ def keys_file end def keys_file_options - options.keys_file.merge path: keys_file_path + options.keys_file_options.merge path: keys_file_path end def keys_file_path diff --git a/lib/github/auth/keys_file.rb b/lib/github/auth/keys_file.rb index 1b0f6f7..a450468 100644 --- a/lib/github/auth/keys_file.rb +++ b/lib/github/auth/keys_file.rb @@ -1,23 +1,16 @@ module Github::Auth # Write and delete keys from the authorized_keys file class KeysFile - attr_reader :path, :options + attr_reader :path, :command PermissionDeniedError = Class.new StandardError FileDoesNotExistError = Class.new StandardError DEFAULT_PATH = '~/.ssh/authorized_keys' - TMUX_COMMAND = [ - 'command="tmux attach"', - 'no-port-forwarding', - 'no-X11-forwarding', - 'no-agent-forwarding' - ].join(',') - def initialize(options = {}) - @options = options @path = File.expand_path(options[:path] || DEFAULT_PATH) + @command = options[:command] end def write!(keys) @@ -27,7 +20,7 @@ def write!(keys) unless keys_file_content.empty? || keys_file_content.end_with?("\n") keys_file.write "\n" end - keys_file.write "#{"#{TMUX_COMMAND} " if options[:tmux]}#{key}\n" + keys_file.write "#{"command=\"#{command}\" " if command}#{key}\n" end end end diff --git a/lib/github/auth/options.rb b/lib/github/auth/options.rb index a59093d..6b9c4d6 100644 --- a/lib/github/auth/options.rb +++ b/lib/github/auth/options.rb @@ -11,7 +11,6 @@ def initialize 'usage: gh-auth', '[--version]', '[--list]', - '[--tmux]', '[--add|--remove]', '' ].join(' ') @@ -38,8 +37,10 @@ def initialize @command = 'list' end - opts.on('--tmux', 'Attach user to tmux session') do - keys_file.merge!(tmux: true) + opts.on( + '--command "tmux attach"', String, 'Command to execute on login' + ) do |command| + keys_file_options.merge!(command: command) end opts.on('--version', 'Show version') do @@ -56,8 +57,8 @@ def usernames @usernames ||= [] end - def keys_file - @keys_file ||= {} + def keys_file_options + @keys_file_options ||= {} end def usage diff --git a/spec/acceptance/github/auth/cli_spec.rb b/spec/acceptance/github/auth/cli_spec.rb index 0f7820d..20b6191 100644 --- a/spec/acceptance/github/auth/cli_spec.rb +++ b/spec/acceptance/github/auth/cli_spec.rb @@ -44,18 +44,10 @@ def cli expect(output).to include('chrishunt') end - it 'prints version information' do - output = capture_stdout do - cli.execute %w(--version) - end - - expect(output).to include Github::Auth::VERSION - end - - it 'can automatically attach users to a tmux session' do - cli.execute %w(--tmux --add chrishunt) + it 'supports ssh commands' do + cli.execute %w(--add chrishunt --command) << "tmux attach" - expect(keys_file.read).to include Github::Auth::KeysFile::TMUX_COMMAND + expect(keys_file.read).to include 'command="tmux attach"' keys_file.rewind cli.execute %w(--remove chrishunt) @@ -63,6 +55,14 @@ def cli expect(keys_file.read.strip).to be_empty end + it 'prints version information' do + output = capture_stdout do + cli.execute %w(--version) + end + + expect(output).to include Github::Auth::VERSION + end + it 'prints usage for invalid arguments' do [[], %w(invalid), %w(--add)].each do |invalid_arguments| expect( diff --git a/spec/unit/github/auth/keys_file_spec.rb b/spec/unit/github/auth/keys_file_spec.rb index feb4785..c6d046c 100644 --- a/spec/unit/github/auth/keys_file_spec.rb +++ b/spec/unit/github/auth/keys_file_spec.rb @@ -79,16 +79,16 @@ it_should_behave_like 'a successful key addition' end - context 'when the tmux option is provided' do - let(:options) {{ path: path, tmux: true }} + context 'with an ssh command' do + let(:options) {{ path: path, command: 'tmux attach' }} let(:keys) {[ Github::Auth::Key.new('chris', 'abc123') ]} it_should_behave_like 'a successful key addition' - it 'prefixes the key with the tmux command' do + it 'prefixes the key with the ssh command' do subject.write! keys - expect(keys_file.read).to include Github::Auth::KeysFile::TMUX_COMMAND + expect(keys_file.read).to include 'command="tmux attach"' end end From 4b0dfc09b62bc00dc378561b4b389dbde470f06b Mon Sep 17 00:00:00 2001 From: Chris Hunt Date: Thu, 24 Oct 2013 19:49:17 -0700 Subject: [PATCH 06/17] Add --command to README --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 20cbea4..37d8916 100644 --- a/README.md +++ b/README.md @@ -27,11 +27,11 @@ $ gh-auth --add chrishunt Adding 2 key(s) to '/Users/chris/.ssh/authorized_keys' ``` -If you'd like me to automatically connect to your existing tmux session, then -provide the `--tmux` option as well: +If you'd like me to automatically connect to your existing tmux session, you +can do that with a custom ssh command: ```bash -$ gh-auth --tmux --add chrishunt +$ gh-auth --add chrishunt --command "tmux attach" Adding 2 key(s) to '/Users/chris/.ssh/authorized_keys' ``` From 7b6cff55672c14879a0db646e62dea4114022570 Mon Sep 17 00:00:00 2001 From: Chris Hunt Date: Thu, 24 Oct 2013 19:49:56 -0700 Subject: [PATCH 07/17] Adjust CLI options banner --- lib/github/auth/options.rb | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/lib/github/auth/options.rb b/lib/github/auth/options.rb index 6b9c4d6..3477ea9 100644 --- a/lib/github/auth/options.rb +++ b/lib/github/auth/options.rb @@ -11,11 +11,16 @@ def initialize 'usage: gh-auth', '[--version]', '[--list]', - '[--add|--remove]', - '' + '[--add|--remove]', '', + '[--command]', '' ].join(' ') - opts.separator "\noptions:" + opts.separator "" + opts.separator "options:" + + opts.on('--list', 'List all GitHub users added') do + @command = 'list' + end opts.on( '--add doug,sally', Array, 'Add GitHub users' @@ -33,17 +38,13 @@ def initialize @usernames = usernames end - opts.on('--list', 'List all GitHub users added') do - @command = 'list' - end - opts.on( '--command "tmux attach"', String, 'Command to execute on login' ) do |command| keys_file_options.merge!(command: command) end - opts.on('--version', 'Show version') do + opts.on_tail('--version', 'Show version') do @command = 'version' end end From ae8250622feae7b90ff73cfa3397f64a0c3f5133 Mon Sep 17 00:00:00 2001 From: Chris Hunt Date: Thu, 24 Oct 2013 19:50:05 -0700 Subject: [PATCH 08/17] Add --help to CLI options --- lib/github/auth/options.rb | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lib/github/auth/options.rb b/lib/github/auth/options.rb index 3477ea9..41df616 100644 --- a/lib/github/auth/options.rb +++ b/lib/github/auth/options.rb @@ -47,6 +47,11 @@ def initialize opts.on_tail('--version', 'Show version') do @command = 'version' end + + opts.on_tail('-h', '--help', '--usage', 'Show this help message') do + puts opts.help + exit + end end end From c3a7527e047426867618aa7674c4b0971a7dfb9b Mon Sep 17 00:00:00 2001 From: Chris Hunt Date: Thu, 24 Oct 2013 19:51:12 -0700 Subject: [PATCH 09/17] Add 'thor' as runtime dependency --- Gemfile.lock | 1 + github-auth.gemspec | 1 + 2 files changed, 2 insertions(+) diff --git a/Gemfile.lock b/Gemfile.lock index 96c9f96..afc103d 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -3,6 +3,7 @@ PATH specs: github-auth (2.0.0) httparty (~> 0.11.0) + thor (~> 0.18) GEM remote: https://rubygems.org/ diff --git a/github-auth.gemspec b/github-auth.gemspec index 0eef2fe..cf78ccb 100644 --- a/github-auth.gemspec +++ b/github-auth.gemspec @@ -29,4 +29,5 @@ Gem::Specification.new do |spec| spec.add_development_dependency 'thin', '~> 1.5.1' spec.add_runtime_dependency 'httparty', '~> 0.11.0' + spec.add_runtime_dependency 'thor', '~> 0.18' end From ef8663a7fdbe55ed75ef2cd5f50c238d04cfeaf1 Mon Sep 17 00:00:00 2001 From: Chris Hunt Date: Thu, 24 Oct 2013 20:21:17 -0700 Subject: [PATCH 10/17] Replace OptionsParser with Thor --- bin/gh-auth | 2 +- lib/github/auth/cli.rb | 73 ++++++++++++++++++++++++++++-------------- 2 files changed, 50 insertions(+), 25 deletions(-) diff --git a/bin/gh-auth b/bin/gh-auth index ecf0893..0fef4d7 100755 --- a/bin/gh-auth +++ b/bin/gh-auth @@ -2,4 +2,4 @@ require 'github/auth' -Github::Auth::CLI.new.execute(ARGV) +Github::Auth::CLI.start ARGV diff --git a/lib/github/auth/cli.rb b/lib/github/auth/cli.rb index 472d370..75a5d26 100644 --- a/lib/github/auth/cli.rb +++ b/lib/github/auth/cli.rb @@ -1,40 +1,72 @@ +require 'thor' + module Github::Auth # Command Line Interface for parsing and executing commands - class CLI - attr_reader :options + class CLI < Thor + option :users, type: :array, required: true + option :command, type: :string + desc 'add', 'Add GitHub users to authorized keys' + long_desc <<-LONGDESC + `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. - def execute(args) - @options = Options.new.parse(args) - send options.command - end + > $ gh-auth add --users=chrishunt zachmargolis + \x5> Adding 6 key(s) to '/Users/chris/.ssh/authorized_keys' - private + 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. + > $ gh-auth add --users=chrishunt --command="tmux attach" + LONGDESC def add on_keys_file :write!, - "Adding #{keys.count} key(s) to '#{keys_file.path}'" + "Adding #{keys(options[:users]).count} key(s) to '#{keys_file.path}'", + { command: options[:command] } end + option :users, type: :array, required: true + desc 'remove', 'Remove GitHub users from authorized keys' + long_desc <<-LONGDESC + `gh-auth remove` is used to remove one or more GitHub user's public SSH + keys from ~/.ssh/authorized_keys. All keys stored on github.com for + that user will be removed. + + > $ gh-auth remove --users=chrishunt zachmargolis + \x5> Removing 6 key(s) to '/Users/chris/.ssh/authorized_keys' + LONGDESC def remove on_keys_file :delete!, - "Removing #{keys.count} key(s) from '#{keys_file.path}'" + "Removing #{keys(options[:users]).count} key(s) from '#{keys_file.path}'" end + desc 'list', 'List all GitHub users already added to authorized keys' + long_desc <<-LONGDESC + `gh-auth list` will list all GitHub users that have been added to + ~/.ssh/authorized_keys by `gh-auth`. + + > $ gh-auth list + \x5> chrishunt, zachmargolis + LONGDESC def list - puts "Added users: #{keys_file.github_users.join(', ')}" + puts keys_file.github_users.join(', ') end + desc 'version', 'Show gh-auth version' def version puts Github::Auth::VERSION end - def usage - puts options.usage + private + + def keys(usernames = []) + @keys ||= Array(usernames).map { |user| keys_for user }.flatten.compact end - def on_keys_file(action, message) + def on_keys_file(action, message, options = {}) puts message - rescue_keys_file_errors { keys_file.send action, keys } + rescue_keys_file_errors { keys_file(options).send action, keys } end def rescue_keys_file_errors @@ -51,10 +83,6 @@ def rescue_keys_file_errors puts " $ touch #{keys_file.path}" end - def keys - @keys ||= options.usernames.map { |user| keys_for user }.flatten.compact - end - def keys_for(username) Github::Auth::KeysClient.new( hostname: github_hostname, @@ -68,12 +96,9 @@ def keys_for(username) puts "https://status.github.com" end - def keys_file - Github::Auth::KeysFile.new keys_file_options - end - - def keys_file_options - options.keys_file_options.merge path: keys_file_path + def keys_file(options = {}) + Github::Auth::KeysFile.new \ + options.merge path: keys_file_path end def keys_file_path From ac840947b7fe42904d2fbf7c107815eac6078fd2 Mon Sep 17 00:00:00 2001 From: Chris Hunt Date: Thu, 24 Oct 2013 21:20:24 -0700 Subject: [PATCH 11/17] Remove unused Github::Auth::Options --- lib/github/auth.rb | 1 - lib/github/auth/options.rb | 80 -------------------------------------- 2 files changed, 81 deletions(-) delete mode 100644 lib/github/auth/options.rb diff --git a/lib/github/auth.rb b/lib/github/auth.rb index cd4b159..37946e2 100644 --- a/lib/github/auth.rb +++ b/lib/github/auth.rb @@ -2,5 +2,4 @@ require 'github/auth/key' require 'github/auth/keys_client' require 'github/auth/keys_file' -require 'github/auth/options' require 'github/auth/cli' diff --git a/lib/github/auth/options.rb b/lib/github/auth/options.rb deleted file mode 100644 index 41df616..0000000 --- a/lib/github/auth/options.rb +++ /dev/null @@ -1,80 +0,0 @@ -require 'optparse' - -module Github::Auth - # Parses command line options - class Options - attr_reader :parser - - def initialize - @parser = OptionParser.new do |opts| - opts.banner = [ - 'usage: gh-auth', - '[--version]', - '[--list]', - '[--add|--remove]', '', - '[--command]', '' - ].join(' ') - - opts.separator "" - opts.separator "options:" - - opts.on('--list', 'List all GitHub users added') do - @command = 'list' - end - - opts.on( - '--add doug,sally', Array, 'Add GitHub users' - ) do |usernames| - raise OptionParser::MissingArgument if usernames.empty? - @command = 'add' - @usernames = usernames - end - - opts.on( - '--remove doug,sally', Array, 'Remove GitHub users' - ) do |usernames| - raise OptionParser::MissingArgument if usernames.empty? - @command = 'remove' - @usernames = usernames - end - - opts.on( - '--command "tmux attach"', String, 'Command to execute on login' - ) do |command| - keys_file_options.merge!(command: command) - end - - opts.on_tail('--version', 'Show version') do - @command = 'version' - end - - opts.on_tail('-h', '--help', '--usage', 'Show this help message') do - puts opts.help - exit - end - end - end - - def command - @command ||= 'usage' - end - - def usernames - @usernames ||= [] - end - - def keys_file_options - @keys_file_options ||= {} - end - - def usage - parser.help - end - - def parse(args) - parser.parse(args) - ensure - return self - end - end -end From e141b01060409fd68017f430ac4b608f88c459b6 Mon Sep 17 00:00:00 2001 From: Chris Hunt Date: Thu, 24 Oct 2013 21:21:16 -0700 Subject: [PATCH 12/17] Remove usernames param from Cli#keys --- lib/github/auth/cli.rb | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/github/auth/cli.rb b/lib/github/auth/cli.rb index 75a5d26..431b3f9 100644 --- a/lib/github/auth/cli.rb +++ b/lib/github/auth/cli.rb @@ -22,7 +22,7 @@ class CLI < Thor LONGDESC def add on_keys_file :write!, - "Adding #{keys(options[:users]).count} key(s) to '#{keys_file.path}'", + "Adding #{keys.count} key(s) to '#{keys_file.path}'", { command: options[:command] } end @@ -38,7 +38,7 @@ def add LONGDESC def remove on_keys_file :delete!, - "Removing #{keys(options[:users]).count} key(s) from '#{keys_file.path}'" + "Removing #{keys.count} key(s) from '#{keys_file.path}'" end desc 'list', 'List all GitHub users already added to authorized keys' @@ -60,8 +60,10 @@ def version private - def keys(usernames = []) - @keys ||= Array(usernames).map { |user| keys_for user }.flatten.compact + def keys + @keys ||= begin + Array(options[:users]).map { |user| keys_for user }.flatten.compact + end end def on_keys_file(action, message, options = {}) From 7c3ad622e4d4a62cc8b099baed6deb3938511be5 Mon Sep 17 00:00:00 2001 From: Chris Hunt Date: Thu, 24 Oct 2013 21:21:41 -0700 Subject: [PATCH 13/17] Add option for GitHub host and keys path --- lib/github/auth/cli.rb | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/github/auth/cli.rb b/lib/github/auth/cli.rb index 431b3f9..fba1718 100644 --- a/lib/github/auth/cli.rb +++ b/lib/github/auth/cli.rb @@ -3,6 +3,9 @@ module Github::Auth # Command Line Interface for parsing and executing commands class CLI < Thor + class_option :host, type: :string + class_option :path, type: :string + option :users, type: :array, required: true option :command, type: :string desc 'add', 'Add GitHub users to authorized keys' @@ -104,11 +107,11 @@ def keys_file(options = {}) end def keys_file_path - Github::Auth::KeysFile::DEFAULT_PATH + options[:path] || Github::Auth::KeysFile::DEFAULT_PATH end def github_hostname - Github::Auth::KeysClient::DEFAULT_HOSTNAME + options[:host] || Github::Auth::KeysClient::DEFAULT_HOSTNAME end end end From 5bb2fe75838ad7a588a11717f3cff9cad4b18c58 Mon Sep 17 00:00:00 2001 From: Chris Hunt Date: Thu, 24 Oct 2013 21:22:01 -0700 Subject: [PATCH 14/17] Update Cli acceptance test to match new syntax --- spec/acceptance/github/auth/cli_spec.rb | 35 +++++++++---------------- 1 file changed, 13 insertions(+), 22 deletions(-) diff --git a/spec/acceptance/github/auth/cli_spec.rb b/spec/acceptance/github/auth/cli_spec.rb index 20b6191..889af5e 100644 --- a/spec/acceptance/github/auth/cli_spec.rb +++ b/spec/acceptance/github/auth/cli_spec.rb @@ -11,23 +11,22 @@ after { keys_file.unlink } - def cli - described_class.new.tap do |cli| - cli.stub( - github_hostname: hostname, - keys_file_path: keys_file.path - ) - end + def cli(args = []) + described_class.start \ + args + [ + "--path=#{keys_file.path}", + "--host=#{hostname}" + ] end it 'adds and removes keys from the keys file' do - cli.execute %w(--add chrishunt) + cli %w(add --users=chrishunt) keys_file.read.tap do |keys_file_content| keys.each { |key| expect(keys_file_content).to include key.to_s } end - cli.execute %w(--remove chrishunt) + cli %w(remove --users=chrishunt) expect(keys_file.read).to be_empty @@ -35,40 +34,32 @@ def cli end it 'lists users from the keys file' do - cli.execute %w(--add chrishunt) + cli %w(add --users=chrishunt) output = capture_stdout do - cli.execute %w(--list) + cli %w(list) end expect(output).to include('chrishunt') end it 'supports ssh commands' do - cli.execute %w(--add chrishunt --command) << "tmux attach" + cli %w(add --users=chrishunt) << '--command=tmux attach' expect(keys_file.read).to include 'command="tmux attach"' keys_file.rewind - cli.execute %w(--remove chrishunt) + cli %w(remove --users=chrishunt) expect(keys_file.read.strip).to be_empty end it 'prints version information' do output = capture_stdout do - cli.execute %w(--version) + cli %w(version) end expect(output).to include Github::Auth::VERSION end - - it 'prints usage for invalid arguments' do - [[], %w(invalid), %w(--add)].each do |invalid_arguments| - expect( - capture_stdout { cli.execute invalid_arguments } - ).to include 'usage: gh-auth' - end - end end end From e06fe4ea4019f0ee6dce28d7c3b9efe06f279261 Mon Sep 17 00:00:00 2001 From: Chris Hunt Date: Thu, 24 Oct 2013 21:28:20 -0700 Subject: [PATCH 15/17] Update README with new command line syntax --- README.md | 85 +++++++++++++++++++++++++++++++++---------------------- 1 file changed, 51 insertions(+), 34 deletions(-) diff --git a/README.md b/README.md index 37d8916..6d79400 100644 --- a/README.md +++ b/README.md @@ -23,7 +23,7 @@ After you've [installed](#installation) `gh-auth`, you can give me ssh access with: ```bash -$ gh-auth --add chrishunt +$ gh-auth add --users=chrishunt Adding 2 key(s) to '/Users/chris/.ssh/authorized_keys' ``` @@ -31,37 +31,37 @@ If you'd like me to automatically connect to your existing tmux session, you can do that with a custom ssh command: ```bash -$ gh-auth --add chrishunt --command "tmux attach" +$ gh-auth add --users=chrishunt --command="tmux attach" Adding 2 key(s) to '/Users/chris/.ssh/authorized_keys' ``` That was easy! When we're done working, you can revoke my access with: ```bash -$ gh-auth --remove chrishunt +$ gh-auth remove --users=chrishunt Removing 2 key(s) from '/Users/chris/.ssh/authorized_keys' ``` You can add and remove any number of users at the same time. ```bash -$ gh-auth --add chrishunt,zachmargolis +$ gh-auth add --users=chrishunt zachmargolis Adding 4 key(s) to '/Users/chris/.ssh/authorized_keys' -$ gh-auth --list -Added users: chrishunt, zachmargolis +$ gh-auth list +chrishunt, zachmargolis -$ gh-auth --remove chrishunt +$ gh-auth remove --users=chrishunt Removing 2 key(s) from '/Users/chris/.ssh/authorized_keys' -$ gh-auth --list -Added users: zachmargolis +$ gh-auth list +zachmargolis -$ gh-auth --remove zachmargolis +$ gh-auth remove --users=zachmargolis Removing 2 key(s) from '/Users/chris/.ssh/authorized_keys' -$ gh-auth --list -Added users: +$ gh-auth list + ``` ## Sections @@ -89,14 +89,41 @@ Added users: `gh-auth` can be used from the command line after the gem has been installed. ```bash -usage: gh-auth [--version] [--list] [--tmux] [--add|--remove] - -options: - --add doug,sally Add GitHub users - --remove doug,sally Remove GitHub users - --list List all GitHub users added - --tmux Attach user to tmux session - --version Show version +$ gh-auth +Commands: + gh-auth add --users=one two three # Add GitHub users to authorized keys + gh-auth help [COMMAND] # Describe available commands or one specific command + gh-auth list # List all GitHub users already added to authorized keys + gh-auth remove --users=one two three # Remove GitHub users from authorized keys + gh-auth version # Show gh-auth version + +Options: + [--host=HOST] + [--path=PATH] +``` + +Use the `help` command for help on a specific command. + +```bash +$ gh-auth help add +Usage: + gh-auth add --users=one two three + +Options: + --users=one two three + [--command=COMMAND] + [--host=HOST] + [--path=PATH] + +Description: + `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. + + > $ gh-auth add --users=chrishunt zachmargolis + > Adding 6 key(s) to '/Users/chris/.ssh/authorized_keys' + + 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. + + > $ gh-auth add --users=chrishunt --command="tmux attach" ``` ### In Your Project @@ -108,10 +135,10 @@ too. require 'github/auth' # Add keys for GitHub user 'chrishunt' -Github::Auth::CLI.new.execute %w(--add chrishunt) +Github::Auth::CLI.start %w(add --users=chrishunt) # Remove keys for GitHub user 'chrishunt' -Github::Auth::CLI.new.execute %w(--remove chrishunt) +Github::Auth::CLI.start %w(remove --users=chrishunt) ``` ## Installation @@ -120,16 +147,6 @@ Install the `github-auth` gem: ```bash $ gem install github-auth - -$ gh-auth -usage: gh-auth [--version] [--list] [--tmux] [--add|--remove] - -options: - --add doug,sally Add GitHub users - --remove doug,sally Remove GitHub users - --list List all GitHub users added - --tmux Attach user to tmux session - --version Show version ``` ### SSH Public Key Authentication (Mac OS X) @@ -157,7 +174,7 @@ First, authorize yourself for ssh. (Make sure to replace 'chrishunt' with *your* GitHub username) ```bash -$ gh-auth --add chrishunt +$ gh-auth add --users=chrishunt Adding 2 key(s) to '/Users/chris/.ssh/authorized_keys' ``` @@ -173,7 +190,7 @@ $ ssh -o PreferredAuthentications=publickey localhost Next, remove your public keys from the keys file: ```bash -$ gh-auth --remove chrishunt +$ gh-auth remove --users=chrishunt Removing 2 key(s) from '/Users/chris/.ssh/authorized_keys' ``` From 0f98851af088b4940cda62658384419a31eb3450 Mon Sep 17 00:00:00 2001 From: Chris Hunt Date: Thu, 24 Oct 2013 21:31:07 -0700 Subject: [PATCH 16/17] Remove comma in list output --- README.md | 2 +- lib/github/auth/cli.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 6d79400..a84cc9b 100644 --- a/README.md +++ b/README.md @@ -49,7 +49,7 @@ $ gh-auth add --users=chrishunt zachmargolis Adding 4 key(s) to '/Users/chris/.ssh/authorized_keys' $ gh-auth list -chrishunt, zachmargolis +chrishunt zachmargolis $ gh-auth remove --users=chrishunt Removing 2 key(s) from '/Users/chris/.ssh/authorized_keys' diff --git a/lib/github/auth/cli.rb b/lib/github/auth/cli.rb index fba1718..1f17882 100644 --- a/lib/github/auth/cli.rb +++ b/lib/github/auth/cli.rb @@ -53,7 +53,7 @@ def remove \x5> chrishunt, zachmargolis LONGDESC def list - puts keys_file.github_users.join(', ') + puts keys_file.github_users.join(' ') end desc 'version', 'Show gh-auth version' From 0b53b409309c0517fbc716ff9cb022b5b034e56c Mon Sep 17 00:00:00 2001 From: Chris Hunt Date: Thu, 24 Oct 2013 21:33:02 -0700 Subject: [PATCH 17/17] Remove bash type for usage examples --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index a84cc9b..c1a9e33 100644 --- a/README.md +++ b/README.md @@ -88,7 +88,7 @@ $ gh-auth list `gh-auth` can be used from the command line after the gem has been installed. -```bash +``` $ gh-auth Commands: gh-auth add --users=one two three # Add GitHub users to authorized keys @@ -104,7 +104,7 @@ Options: Use the `help` command for help on a specific command. -```bash +``` $ gh-auth help add Usage: gh-auth add --users=one two three