Skip to content
Closed
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 lib/github/auth/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class CLI
COMMANDS = %w(add remove)

def initialize(argv)
@tmux = argv.delete('--tmux')
@command = argv.shift
@usernames = argv
end
Expand Down Expand Up @@ -46,7 +47,8 @@ def rescue_keys_file_errors
end

def print_usage
puts "usage: gh-auth [--version] [#{COMMANDS.join '|'}] <username>"
puts "usage: gh-auth [--version] [--tmux] " +
"[#{COMMANDS.join '|'}] <username>"
end

def print_version
Expand Down Expand Up @@ -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,
Expand All @@ -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
Expand Down
5 changes: 5 additions & 0 deletions lib/github/auth/keys_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,20 @@ 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)
Array(keys).each do |key|
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
Expand Down Expand Up @@ -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

Expand Down
10 changes: 10 additions & 0 deletions spec/unit/github/auth/cli_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
38 changes: 37 additions & 1 deletion spec/unit/github/auth/keys_file_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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|
Expand Down Expand Up @@ -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