Skip to content
This repository has been archived by the owner on Sep 13, 2021. It is now read-only.

Commit

Permalink
Merge pull request #52 from randallreedjr/feature/git-ssh-command
Browse files Browse the repository at this point in the history
Update handling of ssh keys
  • Loading branch information
randallreedjr committed Jan 22, 2019
2 parents 1eadf36 + d76c2a6 commit a88c293
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 31 deletions.
6 changes: 6 additions & 0 deletions README.md
Expand Up @@ -5,6 +5,12 @@

Git Switch is a command line utility to easily switch between multiple git profiles. It uses a `.gitswitch` YAML file to configure each profile (name, username, and email) and ssh key.

## Prerequisites

This gem has been developed for, and only tested on, Mac OS.

Additionally, it requires git version 2.10+. Run `git --version` to check your currently install version. If you are using homebrew, you can install a newer version of git with `brew upgrade git`.

## Installation

This gem is not intended to be installed via a Gemfile. Instead, install it yourself:
Expand Down
12 changes: 8 additions & 4 deletions lib/git_switch/config.rb
Expand Up @@ -13,10 +13,6 @@ def get_profile(args)
args.detect {|a| !a.start_with? '-'}
end

def name
profiles[selected_profile]["name"]
end

def username
profiles[selected_profile]["username"]
end
Expand All @@ -25,10 +21,18 @@ def email
profiles[selected_profile]["email"]
end

def name
profiles[selected_profile]["name"]
end

def ssh
profiles[selected_profile]["ssh"]
end

def ssh_command
"ssh -i #{ssh}"
end

def profile
@selected_profile
end
Expand Down
26 changes: 14 additions & 12 deletions lib/git_switch/switcher.rb
Expand Up @@ -5,7 +5,7 @@ module GitSwitch
class Switcher
attr_reader :config, :options
delegate :usage?, :config?, :list?, :global?, to: :options
delegate :profile, :name, :username, :email, :ssh, :print_list, :configure!, :valid_profile?, to: :config
delegate :profile, :name, :username, :email, :ssh, :ssh_command, :print_list, :configure!, :valid_profile?, to: :config

def initialize(args)
raise ArgumentError unless args.is_a? Array
Expand Down Expand Up @@ -38,21 +38,19 @@ def git_repo?
def set!
return unless valid_profile? && git_repo?

flag = global? ? '--global' : ''

set_git_config(flag)
set_git_config
set_ssh
print_settings(flag)
print_settings
end

def print_usage
puts usage
end

def print_settings(flag = '')
def print_settings
if options.verbose?
puts "\nGit Config:"
puts `git config #{flag} -l --show-origin | grep user`
puts `git config #{git_config_flag} -l --show-origin | grep user`
puts "\nSSH:"
puts `ssh-add -l`
else
Expand All @@ -62,14 +60,18 @@ def print_settings(flag = '')

private

def set_git_config(flag)
`git config #{flag} user.name "#{name}"`
`git config #{flag} user.username "#{username}"`
`git config #{flag} user.email "#{email}"`
def git_config_flag
@git_config_flag ||= global? ? '--global' : ''
end

def set_git_config
`git config #{git_config_flag} user.name "#{name}"`
`git config #{git_config_flag} user.username "#{username}"`
`git config #{git_config_flag} user.email "#{email}"`
end

def set_ssh
`ssh-add -D`
`git config #{git_config_flag} core.sshCommand "#{ssh_command}"`
`ssh-add #{ssh}`
end

Expand Down
2 changes: 1 addition & 1 deletion lib/git_switch/version.rb
@@ -1,3 +1,3 @@
module GitSwitch
VERSION = "0.4.0"
VERSION = "0.4.1"
end
34 changes: 34 additions & 0 deletions spec/lib/git_switch/config_spec.rb
@@ -1,6 +1,40 @@
require 'spec_helper'

RSpec.describe GitSwitch::Config do
describe 'profile attributes' do
let(:config) { GitSwitch::Config.new(['personal']) }

describe '#username' do
it 'returns the username for the selected profile' do
expect(config.username).to eq 'johnnyfive'
end
end

describe '#email' do
it 'returns the email for the selected profile' do
expect(config.email).to eq 'me@johnsmith.com'
end
end

describe '#name' do
it 'returns the name for the selected profile' do
expect(config.name).to eq 'Johnny Smith'
end
end

describe '#ssh' do
it 'returns the ssh path for the selected profile' do
expect(config.ssh).to eq '~/.ssh/id_rsa'
end
end

describe '#ssh_command' do
it 'returns the ssh command for the selected profile' do
expect(config.ssh_command).to eq 'ssh -i ~/.ssh/id_rsa'
end
end
end

describe 'valid_profile?' do
context 'when profile is configured' do
let(:config) { GitSwitch::Config.new(['personal']) }
Expand Down
17 changes: 3 additions & 14 deletions spec/lib/git_switch/switcher_spec.rb
Expand Up @@ -210,20 +210,9 @@

let(:switcher) { GitSwitch::Switcher.new(['personal']) }

context 'when run with -g flag' do
let(:switcher) { GitSwitch::Switcher.new(['personal', '-g']) }
it 'calls set_git_config with global flag' do
expect(switcher).to receive(:set_git_config).with('--global')
switcher.run
end
end

context 'when run with --global flag' do
let(:switcher) { GitSwitch::Switcher.new(['personal', '--global']) }
it 'calls set_git_config with global flag' do
expect(switcher).to receive(:set_git_config).with('--global')
switcher.run
end
it 'calls set_git_config' do
expect(switcher).to receive(:set_git_config)
switcher.run
end

it 'calls set_ssh' do
Expand Down

0 comments on commit a88c293

Please sign in to comment.