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

Commit

Permalink
"Allow passing -l or --list to print profiles"
Browse files Browse the repository at this point in the history
[fixes #7]
  • Loading branch information
randallreedjr committed Aug 21, 2018
1 parent c8c0f46 commit 7cb48dd
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 5 deletions.
2 changes: 1 addition & 1 deletion bin/git-switch
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env ruby
require_relative '../lib/git_switch'

GitSwitch::Switcher.new(ARGV).set!
GitSwitch::Switcher.new(ARGV).run
17 changes: 15 additions & 2 deletions lib/git_switch/switcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,29 @@

module GitSwitch
class Switcher
attr_reader :config, :profile, :valid, :global
# Your code goes here...
attr_reader :config, :profile, :valid, :global, :list

def initialize(args)
raise ArgumentError unless args.is_a? Array
@config = YAML.load_file(File.expand_path('~/.gitswitch'))
@global = check_global(args)
@profile = get_profile(args)
@valid = valid_args?(args)
@list = check_list(args)
end

def run
list ? print_list : set!
end

def check_global(args)
(args.include? '-g') || (args.include? '--global')
end

def check_list(args)
(args.include? '-l') || (args.include? '--list')
end

def get_profile(args)
# TODO: RCR - Verify profile exists in config file
# TODO: RCR - Handle missing or empty config file
Expand Down Expand Up @@ -52,6 +61,10 @@ def set!
puts `ssh-add -l`
end

def print_list
puts config.keys
end

private

def name
Expand Down
75 changes: 73 additions & 2 deletions spec/lib/git_switch/switcher_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
expect(GitSwitch::Switcher.new(['-g','foo']).global).to be true
end
end
context 'when -g is passed as first argument' do

context 'when -g is passed as second argument' do
it 'sets to true' do
expect(GitSwitch::Switcher.new(['foo','-g']).global).to be true
end
Expand All @@ -23,7 +24,7 @@
end
end

context 'when --global is passsed as first argument' do
context 'when --global is passsed as second argument' do
it 'sets to true' do
expect(GitSwitch::Switcher.new(['foo','--global']).global).to be true
end
Expand Down Expand Up @@ -54,4 +55,74 @@
end
end
end

describe '#list' do
context 'when -l is passed as only argument' do
it 'sets to true' do
expect(GitSwitch::Switcher.new(['-l']).list).to be true
end
end

context 'when -l is passed as first argument' do
it 'sets to true' do
expect(GitSwitch::Switcher.new(['-l','foo']).list).to be true
end
end

context 'when -l is passed as second argument' do
it 'sets to true' do
expect(GitSwitch::Switcher.new(['foo','-l']).list).to be true
end
end

context 'when --list is passed as only argument' do
it 'sets to true' do
expect(GitSwitch::Switcher.new(['-l']).list).to be true
end
end

context 'when --list is passsed as first argument' do
it 'sets to true' do
expect(GitSwitch::Switcher.new(['--list']).list).to be true
end
end

context 'when --list is passsed as second argument' do
it 'sets to true' do
expect(GitSwitch::Switcher.new(['foo','--list']).list).to be true
end
end

context 'when no flag is passed' do
it 'sets to false' do
expect(GitSwitch::Switcher.new(['foo']).list).to be false
end
end
end

describe '#run' do
context 'in list mode' do
let(:switcher) { GitSwitch::Switcher.new(['-l']) }
it 'calls print_list' do
expect(switcher).to receive(:print_list)
switcher.run
end
end

context 'in set mode' do
let(:switcher) { GitSwitch::Switcher.new(['foo']) }
it 'calls print_list' do
expect(switcher).to receive(:set!)
switcher.run
end
end
end

describe '#print_list' do
let(:switcher) { GitSwitch::Switcher.new(['-l']) }
let(:expected_output) { "personal\nwork\n" }
it 'outputs available profiles' do
expect{switcher.print_list}.to output(expected_output).to_stdout
end
end
end

0 comments on commit 7cb48dd

Please sign in to comment.