This repository has been archived by the owner on Sep 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split out some functionality to new Config class (#38)
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
1 parent
9e830b1
commit 2bfa38a
Showing
5 changed files
with
157 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters