diff --git a/lib/github/auth/cli.rb b/lib/github/auth/cli.rb index ee54cbd..1d0b0f1 100644 --- a/lib/github/auth/cli.rb +++ b/lib/github/auth/cli.rb @@ -6,6 +6,7 @@ class CLI COMMANDS = %w(add remove) def initialize(argv) + @tmux = argv.delete('--tmux') @command = argv.shift @usernames = argv end @@ -46,7 +47,8 @@ def rescue_keys_file_errors end def print_usage - puts "usage: gh-auth [--version] [#{COMMANDS.join '|'}] " + puts "usage: gh-auth [--version] [--tmux] " + + "[#{COMMANDS.join '|'}] " end def print_version @@ -81,6 +83,10 @@ def keys @keys ||= usernames.map { |username| keys_for username }.flatten.compact end + def tmux + @tmux || false + end + def keys_for(username) Github::Auth::KeysClient.new( hostname: github_hostname, @@ -93,7 +99,7 @@ def keys_for(username) end def keys_file - Github::Auth::KeysFile.new path: keys_file_path + Github::Auth::KeysFile.new path: keys_file_path, tmux: tmux end def keys_file_path diff --git a/lib/github/auth/keys_file.rb b/lib/github/auth/keys_file.rb index fda83f3..4073bc0 100644 --- a/lib/github/auth/keys_file.rb +++ b/lib/github/auth/keys_file.rb @@ -7,9 +7,12 @@ class KeysFile FileDoesNotExistError = Class.new StandardError DEFAULT_PATH = '~/.ssh/authorized_keys' + TMUX_COMMAND = 'command="/usr/local/bin/tmux attach",' + + 'no-port-forwarding,no-X11-forwarding,no-agent-forwarding ' def initialize(options = {}) @path = File.expand_path(options[:path] || DEFAULT_PATH) + @tmux = options[:tmux] || false end def write!(keys) @@ -17,6 +20,7 @@ def write!(keys) unless keys_file_content.include? key.key append_keys_file do |keys_file| keys_file.write "\n" unless keys_file_content.empty? + keys_file.write TMUX_COMMAND if @tmux keys_file.write key end end @@ -54,6 +58,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! /#{TMUX_COMMAND}/, '' content.gsub! /#{Regexp.escape key.key}( .*)?$\n?/, '' end diff --git a/spec/unit/github/auth/cli_spec.rb b/spec/unit/github/auth/cli_spec.rb index 57abd51..1e4439c 100644 --- a/spec/unit/github/auth/cli_spec.rb +++ b/spec/unit/github/auth/cli_spec.rb @@ -47,5 +47,15 @@ subject.execute end end + + context 'with the --tmux option' do + let(:action) { 'add' } + let(:argv) { ['--tmux', action, 'chrishunt'] } + + it 'calls the method matching the action name' do + subject.should_receive(action) + subject.execute + end + end end end diff --git a/spec/unit/github/auth/keys_file_spec.rb b/spec/unit/github/auth/keys_file_spec.rb index 4edddf4..90a44d0 100644 --- a/spec/unit/github/auth/keys_file_spec.rb +++ b/spec/unit/github/auth/keys_file_spec.rb @@ -4,10 +4,11 @@ require 'github/auth/keys_file' describe Github::Auth::KeysFile do - subject { described_class.new path: path } + subject { described_class.new path: path, tmux: tmux } let(:keys_file) { Tempfile.new 'authorized_keys' } let(:path) { keys_file.path } + let(:tmux) { false } after { keys_file.unlink } # clean up, delete tempfile @@ -121,6 +122,24 @@ }.to raise_error Github::Auth::KeysFile::FileDoesNotExistError end end + + context 'when the tmux option is set' do + let(:keys) {[ Github::Auth::Key.new('chris', 'abc124') ]} + let(:tmux) { true } + let(:command) { Github::Auth::KeysFile::TMUX_COMMAND } + + it_should_behave_like 'a successful key addition' + + it 'prefixes the tmux commands when the option is set' do + subject.write! keys + + keys_file.read.tap do |keys_file_content| + keys.each do |key| + expect(keys_file_content).to start_with "#{command}" + end + end + end + end end describe '#delete!' do @@ -143,6 +162,7 @@ end it 'does not remove the other keys from the keys file' do + subject.delete! key keys_file.read.tap do |keys_file_content| @@ -192,5 +212,21 @@ end end end + + context 'when the key starts with the tmux command' do + let(:key) { keys.last } + + it 'removes the tmux key' do + keys_file.write Github::Auth::KeysFile::TMUX_COMMAND + keys_file.write key + keys_file.rewind + + subject.delete! key + keys_file.read.tap do |key_file_content| + expect(key_file_content).to_not include + Github::Auth::KeysFile::TMUX_COMMAND + end + end + end end end