Class: GitSwitch::Switcher
- Inherits:
-
Object
- Object
- GitSwitch::Switcher
- Defined in:
- lib/git_switch/switcher.rb
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#profile ⇒ Object
readonly
Returns the value of attribute profile.
Instance Method Summary collapse
- #get_profile(args) ⇒ Object
- #git_repo? ⇒ Boolean
- #global? ⇒ Boolean
-
#initialize(args) ⇒ Switcher
constructor
A new instance of Switcher.
- #list? ⇒ Boolean
- #print_list ⇒ Object
- #print_settings(flag = '') ⇒ Object
- #run ⇒ Object
- #set! ⇒ Object
- #valid_profile? ⇒ Boolean
Constructor Details
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
6 7 8 |
# File 'lib/git_switch/switcher.rb', line 6 def config @config end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
6 7 8 |
# File 'lib/git_switch/switcher.rb', line 6 def @options end |
#profile ⇒ Object (readonly)
Returns the value of attribute profile.
6 7 8 |
# File 'lib/git_switch/switcher.rb', line 6 def profile @profile end |
Instance Method Details
#get_profile(args) ⇒ Object
27 28 29 |
# File 'lib/git_switch/switcher.rb', line 27 def get_profile(args) args.detect {|a| !a.start_with? '-'} end |
#git_repo? ⇒ Boolean
40 41 42 43 44 45 46 47 |
# File 'lib/git_switch/switcher.rb', line 40 def git_repo? if GitHelper.git_repo? || global? return true else puts "Not a git repo. Please run from a git repo or run with `-g` to update global settings." return false end end |
#global? ⇒ Boolean
23 24 25 |
# File 'lib/git_switch/switcher.rb', line 23 def global? .global? end |
#list? ⇒ Boolean
19 20 21 |
# File 'lib/git_switch/switcher.rb', line 19 def list? .list? end |
#print_list ⇒ Object
60 61 62 63 64 65 66 67 |
# File 'lib/git_switch/switcher.rb', line 60 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 |
#print_settings(flag = '') ⇒ Object
69 70 71 72 73 74 75 76 77 78 |
# File 'lib/git_switch/switcher.rb', line 69 def print_settings(flag = '') if .verbose? puts "\nGit Config:" puts `git config #{flag} -l --show-origin | grep user` puts "\nSSH:" puts `ssh-add -l` else puts "Switched to profile #{profile}" end end |
#run ⇒ Object
15 16 17 |
# File 'lib/git_switch/switcher.rb', line 15 def run list? ? print_list : set! end |
#set! ⇒ Object
49 50 51 52 53 54 55 56 57 58 |
# File 'lib/git_switch/switcher.rb', line 49 def set! return unless .valid_args? && valid_profile? return unless git_repo? flag = global? ? '--global' : '' set_git_config(flag) set_ssh print_settings(flag) end |
#valid_profile? ⇒ Boolean
31 32 33 34 35 36 37 38 |
# File 'lib/git_switch/switcher.rb', line 31 def valid_profile? if config.has_key?(profile) return true else puts "Profile '#{profile}' not found!" return false end end |