diff --git a/lib/git_switch/config.rb b/lib/git_switch/config.rb index 3c3f8f7..d92b692 100644 --- a/lib/git_switch/config.rb +++ b/lib/git_switch/config.rb @@ -51,6 +51,10 @@ def configure! write_profiles_to_config_file if @profiles.any? end + def edit! + system("#{ENV['EDITOR']} '#{File.expand_path('~/.gitswitch')}'") + end + def build_profiles puts "How many profiles would you like to create?" profile_count = STDIN.gets.chomp.to_i diff --git a/lib/git_switch/options.rb b/lib/git_switch/options.rb index 4270694..a8d5b5a 100644 --- a/lib/git_switch/options.rb +++ b/lib/git_switch/options.rb @@ -7,14 +7,16 @@ def initialize(args) def flags @flags ||= args.select do |arg| - arg.match(/\A\-[cglv]{1}\z/) || - arg.match(/\A\-\-(config|global|list|verbose|version){1}\z/) + arg.match(/\A\-[ceglv]{1}\z/) || + arg.match(/\A\-\-(config|edit|global|list|verbose|version){1}\z/) end end def valid_args? if config? return true + elsif edit? + return true elsif list? return true elsif version? @@ -41,6 +43,10 @@ def config? config_flag? && args.count == 1 end + def edit? + edit_flag? && args.count == 1 + end + def list? list_flag? && args.count == 1 end @@ -63,6 +69,10 @@ def config_flag? (flags.include? '-c') || (flags.include? '--config') end + def edit_flag? + (flags.include? '-e') || (flags.include? '--edit') + end + def list_flag? (flags.include? '-l') || (flags.include? '--list') end diff --git a/lib/git_switch/switcher.rb b/lib/git_switch/switcher.rb index 56429cb..754acf9 100644 --- a/lib/git_switch/switcher.rb +++ b/lib/git_switch/switcher.rb @@ -4,8 +4,8 @@ module GitSwitch class Switcher attr_reader :config, :options - delegate :usage?, :config?, :list?, :version?, :global?, to: :options - delegate :profile, :name, :username, :email, :ssh, :ssh_command, :print_list, :configure!, :valid_profile?, to: :config + delegate :usage?, :config?, :edit?, :list?, :version?, :global?, to: :options + delegate :profile, :name, :username, :email, :ssh, :ssh_command, :print_list, :configure!, :edit!, :valid_profile?, to: :config def initialize(args) raise ArgumentError unless args.is_a? Array @@ -19,6 +19,8 @@ def run print_usage elsif config? configure! + elsif edit? + edit! elsif list? print_list elsif version? @@ -87,12 +89,15 @@ def set_ssh def usage <<~USAGE - usage: git switch [-c | --config] [-l | --list] [-v | --version] + usage: git switch [-c | --config] [-e | --edit] [-l | --list] [-v | --version] [-v | --verbose] [-g | --global] configure profiles git switch -c + open configuration file in editor + git switch -e + switch to a profile for local development only git switch diff --git a/lib/git_switch/version.rb b/lib/git_switch/version.rb index 433d9d9..b8fe3d2 100644 --- a/lib/git_switch/version.rb +++ b/lib/git_switch/version.rb @@ -1,3 +1,3 @@ module GitSwitch - VERSION = "0.4.3" + VERSION = "0.5.0" end diff --git a/spec/lib/git_switch/config_spec.rb b/spec/lib/git_switch/config_spec.rb index a84e1fc..2e14c95 100644 --- a/spec/lib/git_switch/config_spec.rb +++ b/spec/lib/git_switch/config_spec.rb @@ -129,6 +129,19 @@ end end + describe 'edit!' do + let(:config) { GitSwitch::Config.new(['-e']) } + before do + allow(ENV).to receive(:[]).and_call_original + allow(ENV).to receive(:[]).with('EDITOR').and_return('code -w') + end + + it 'opens the config file in the editor' do + expect(config).to receive(:system).with("code -w '#{File.expand_path('spec/fixtures/.gitswitch')}'") + config.edit! + end + end + describe 'ordinal' do let(:config) { GitSwitch::Config.new(['-c']) } diff --git a/spec/lib/git_switch/options_spec.rb b/spec/lib/git_switch/options_spec.rb index 6ba117f..431441d 100644 --- a/spec/lib/git_switch/options_spec.rb +++ b/spec/lib/git_switch/options_spec.rb @@ -12,6 +12,13 @@ end end + context 'when run in edit mode' do + let(:args) { ['-e'] } + it 'returns an array of the flags' do + expect(options.flags).to eq ['-e'] + end + end + context 'when run in list mode' do let(:args) { ['-l'] } it 'returns an array of the flags' do @@ -63,6 +70,13 @@ end end + context 'when run in edit mode' do + let(:args) { ['--edit'] } + it 'returns an array of the flags' do + expect(options.flags).to eq ['--edit'] + end + end + context 'when run in list mode' do let(:args) { ['--list'] } it 'returns an array of the flags' do @@ -153,6 +167,14 @@ end end + context 'when run with edit flag' do + let(:args) { ['-e'] } + + it 'returns true' do + expect(options.valid_args?).to be true + end + end + context 'when run with list flag' do let(:args) { ['-l'] } @@ -288,6 +310,36 @@ end end + describe 'edit?' do + context 'when args includes -e' do + let(:args) { ['-e'] } + it 'returns true' do + expect(options.edit?).to be true + end + end + + context 'when args includes --edit' do + let(:args) { ['--edit'] } + it 'returns true' do + expect(options.edit?).to be true + end + end + + context 'when there are multiple args' do + let(:args) { ['-e', 'foo'] } + it 'returns false' do + expect(options.edit?).to be false + end + end + + context 'when args do not include -e or --edit' do + let(:args) { [] } + it 'returns false' do + expect(options.edit?).to be false + end + end + end + describe 'list?' do context 'when args includes -l' do let(:args) { ['-l'] } diff --git a/spec/lib/git_switch/switcher_spec.rb b/spec/lib/git_switch/switcher_spec.rb index a5e6de1..138e824 100644 --- a/spec/lib/git_switch/switcher_spec.rb +++ b/spec/lib/git_switch/switcher_spec.rb @@ -97,6 +97,50 @@ end end + describe '#edit?' do + context 'when -e is passed as only argument' do + it 'returns true' do + expect(GitSwitch::Switcher.new(['-e']).edit?).to be true + end + end + + context 'when -e is passed as first argument' do + it 'returns false' do + expect(GitSwitch::Switcher.new(['-e','foo']).edit?).to be false + end + end + + context 'when -e is passed as second argument' do + it 'returns false' do + expect(GitSwitch::Switcher.new(['foo','-e']).edit?).to be false + end + end + + context 'when --edit is passed as only argument' do + it 'returns true' do + expect(GitSwitch::Switcher.new(['--edit']).edit?).to be true + end + end + + context 'when --edit is passsed as first argument' do + it 'returns false' do + expect(GitSwitch::Switcher.new(['--edit', 'foo']).edit?).to be false + end + end + + context 'when --edit is passsed as second argument' do + it 'returns false' do + expect(GitSwitch::Switcher.new(['foo','--edit']).edit?).to be false + end + end + + context 'when no flag is passed' do + it 'returns false' do + expect(GitSwitch::Switcher.new(['foo']).edit?).to be false + end + end + end + describe '#list?' do context 'when -l is passed as only argument' do it 'returns true' do @@ -152,13 +196,22 @@ context 'in config mode' do let(:switcher) { GitSwitch::Switcher.new(['-c']) } - it 'calls configure' do + it 'calls configure!' do expect(switcher).to receive(:configure!).and_call_original expect(switcher.config).to receive(:configure!) switcher.run end end + context 'in edit mode' do + let(:switcher) { GitSwitch::Switcher.new(['-e']) } + it 'calls edit!' do + expect(switcher).to receive(:edit!).and_call_original + expect(switcher.config).to receive(:edit!) + switcher.run + end + end + context 'in list mode' do let(:switcher) { GitSwitch::Switcher.new(['-l']) } it 'calls print_list' do @@ -266,12 +319,15 @@ let(:switcher) { GitSwitch::Switcher.new([]) } let(:expected_output) do <<~USAGE - usage: git switch [-c | --config] [-l | --list] [-v | --version] + usage: git switch [-c | --config] [-e | --edit] [-l | --list] [-v | --version] [-v | --verbose] [-g | --global] configure profiles git switch -c + open configuration file in editor + git switch -e + switch to a profile for local development only git switch diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index a36cda4..1851138 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -5,12 +5,20 @@ # Enable flags like --only-failures and --next-failure config.example_status_persistence_file_path = ".rspec_status" + # These two settings work together to allow you to limit a spec run + # to individual examples or groups you care about by tagging them with + # `:focus` metadata. When nothing is tagged with `:focus`, all examples + # get run. + config.filter_run :focus + config.run_all_when_everything_filtered = true + # Disable RSpec exposing methods globally on `Module` and `main` config.disable_monkey_patching! config.expect_with :rspec do |c| c.syntax = :expect end + config.before(:each) do allow(File).to receive(:expand_path).and_call_original allow(File).to receive(:expand_path).with('~/.gitswitch').and_return(File.expand_path('spec/fixtures/.gitswitch'))