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

Commit

Permalink
Split out some functionality to new Config class (#38)
Browse files Browse the repository at this point in the history
The Switcher class is growing too large. The new Config class will be responsible for interfacing with the .gitswitch config file and managing the configured profiles.
  • Loading branch information
randallreedjr committed Jan 15, 2019
1 parent 9e830b1 commit 2bfa38a
Show file tree
Hide file tree
Showing 5 changed files with 157 additions and 109 deletions.
1 change: 1 addition & 0 deletions lib/git_switch.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require_relative 'git_switch/switcher'
require_relative 'git_switch/git_helper'
require_relative 'git_switch/options'
require_relative 'git_switch/config'
65 changes: 65 additions & 0 deletions lib/git_switch/config.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
require 'yaml'

module GitSwitch
class Config
attr_reader :profiles, :selected_profile
def initialize(args)
@profiles = load!
@args = args
@selected_profile = get_profile(args)
end

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

def email
profiles[selected_profile]["email"]
end

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

def profile
@selected_profile
end

def valid_profile?
if profiles.has_key?(selected_profile)
return true
else
puts "Profile '#{selected_profile}' not found!"
return false
end
end

def print_list
profiles = @profiles.map do |key, value|
prefix = value["username"] == current_git_username ? "=>" : " "
"#{prefix} #{key}"
end
puts profiles
puts "\n# => - current" if @profiles.any? {|key, value| value["username"] == current_git_username}
end

private

def load!
# TODO: RCR - Handle missing or empty config file
YAML.load_file(File.expand_path('~/.gitswitch')) || {}
end

def current_git_username
`git config user.username`.chomp
end
end
end
81 changes: 30 additions & 51 deletions lib/git_switch/switcher.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
require 'yaml'
require_relative './version'

module GitSwitch
class Switcher
attr_reader :config, :profile, :options
attr_reader :config, :options

def initialize(args)
raise ArgumentError unless args.is_a? Array
@config = load_config
@options = GitSwitch::Options.new(args)
@profile = get_profile(args)
@config = GitSwitch::Config.new(args)
end

def run
Expand All @@ -22,6 +20,26 @@ def run
end
end

def profile
config.profile
end

def name
config.name
end

def username
config.username
end

def email
config.email
end

def ssh
config.ssh
end

def usage?
options.usage?
end
Expand All @@ -34,19 +52,6 @@ def global?
options.global?
end

def get_profile(args)
args.detect {|a| !a.start_with? '-'}
end

def valid_profile?
if config.has_key?(profile)
return true
else
puts "Profile '#{profile}' not found!"
return false
end
end

def git_repo?
if GitHelper.git_repo? || global?
return true
Expand All @@ -56,6 +61,10 @@ def git_repo?
end
end

def valid_profile?
config.valid_profile?
end

def set!
return unless options.valid_args? && valid_profile?
return unless git_repo?
Expand All @@ -67,19 +76,14 @@ def set!
print_settings(flag)
end

def print_list
profiles = config.map do |key, value|
prefix = value["username"] == current_git_username ? "=>" : " "
"#{prefix} #{key}"
end
puts profiles
puts "\n# => - current" if config.any? {|key, value| value["username"] == current_git_username}
end

def print_usage
puts usage
end

def print_list
config.print_list
end

def print_settings(flag = '')
if options.verbose?
puts "\nGit Config:"
Expand All @@ -93,31 +97,6 @@ def print_settings(flag = '')

private

def load_config
# TODO: RCR - Handle missing or empty config file
YAML.load_file(File.expand_path('~/.gitswitch')) || {}
end

def current_git_username
`git config user.username`.chomp
end

def name
config[profile]["name"]
end

def username
config[profile]["username"]
end

def email
config[profile]["email"]
end

def ssh
config[profile]["ssh"]
end

def set_git_config(flag)
`git config #{flag} user.name "#{name}"`
`git config #{flag} user.username "#{username}"`
Expand Down
59 changes: 59 additions & 0 deletions spec/lib/git_switch/config_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
require 'spec_helper'

RSpec.describe GitSwitch::Config do
describe 'valid_profile?' do
context 'when profile is configured' do
let(:config) { GitSwitch::Config.new(['personal']) }
it 'returns true' do
expect(config.valid_profile?).to be true
end
end

context 'when profile is not configured' do
let(:config) { GitSwitch::Config.new(['foo']) }
let(:expected_output) { "Profile 'foo' not found!\n" }
it 'returns false' do
expect(config.valid_profile?).to be false
end

it 'prints error message' do
expect{config.valid_profile?}.to output(expected_output).to_stdout
end
end
end

describe '#print_list' do
let(:config) { GitSwitch::Config.new(['-l']) }
context 'when profiles have been configured' do
context 'when no profiles are active' do
let(:expected_output) { " personal\n work\n" }
it 'outputs available profiles' do
expect{config.print_list}.to output(expected_output).to_stdout
end
end

context 'when a profile is active' do
before do
allow(config).to receive(:current_git_username).and_return('johnnyfive')
end
let(:expected_output) { "=> personal\n work\n\n# => - current\n" }
it 'indicates the active profile' do
expect{config.print_list}.to output(expected_output).to_stdout
end
end
end

context 'when no profiles have been configured' do
let(:expected_output) { '' }
before do
# unstub
allow(File).to receive(:expand_path).and_call_original
allow(File).to receive(:expand_path).and_return(File.expand_path('spec/fixtures/.empty'))
end

it 'outputs an empty string' do
expect{config.print_list}.to output(expected_output).to_stdout
end
end
end
end
60 changes: 2 additions & 58 deletions spec/lib/git_switch/switcher_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@
context 'in list mode' do
let(:switcher) { GitSwitch::Switcher.new(['-l']) }
it 'calls print_list' do
expect(switcher).to receive(:print_list)
expect(switcher.config).to receive(:print_list)
switcher.run
end
end
Expand All @@ -129,7 +129,7 @@
end

it 'checks for valid profile' do
expect(switcher).to receive(:valid_profile?)
expect(switcher.config).to receive(:valid_profile?)
switcher.run
end

Expand Down Expand Up @@ -211,41 +211,6 @@
end
end

describe '#print_list' do
let(:switcher) { GitSwitch::Switcher.new(['-l']) }
context 'when profiles have been configured' do
context 'when no profiles are active' do
let(:expected_output) { " personal\n work\n" }
it 'outputs available profiles' do
expect{switcher.print_list}.to output(expected_output).to_stdout
end
end

context 'when a profile is active' do
before do
allow(switcher).to receive(:current_git_username).and_return('johnnyfive')
end
let(:expected_output) { "=> personal\n work\n\n# => - current\n" }
it 'indicates the active profile' do
expect{switcher.print_list}.to output(expected_output).to_stdout
end
end
end

context 'when no profiles have been configured' do
let(:expected_output) { '' }
before do
# unstub
allow(File).to receive(:expand_path).and_call_original
allow(File).to receive(:expand_path).and_return(File.expand_path('spec/fixtures/.empty'))
end

it 'outputs an empty string' do
expect{switcher.print_list}.to output(expected_output).to_stdout
end
end
end

describe '#print_usage' do
let(:switcher) { GitSwitch::Switcher.new([]) }
let(:expected_output) do
Expand All @@ -271,27 +236,6 @@
end
end

describe 'valid_profile?' do
context 'when profile is configured' do
let(:switcher) { GitSwitch::Switcher.new(['personal']) }
it 'returns true' do
expect(switcher.valid_profile?).to be true
end
end

context 'when profile is not configured' do
let(:switcher) { GitSwitch::Switcher.new(['foo']) }
let(:expected_output) { "Profile 'foo' not found!\n" }
it 'returns false' do
expect(switcher.valid_profile?).to be false
end

it 'prints error message' do
expect{switcher.valid_profile?}.to output(expected_output).to_stdout
end
end
end

describe 'git_repo?' do
let(:switcher) { GitSwitch::Switcher.new(['personal']) }
context 'when GitHelper returns true' do
Expand Down

0 comments on commit 2bfa38a

Please sign in to comment.