Class: GitSwitch::Switcher

Inherits:
Object
  • Object
show all
Defined in:
lib/git_switch/switcher.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Switcher

Returns a new instance of Switcher.

Raises:

  • (ArgumentError)


8
9
10
11
12
13
# File 'lib/git_switch/switcher.rb', line 8

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

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



6
7
8
# File 'lib/git_switch/switcher.rb', line 6

def config
  @config
end

#optionsObject (readonly)

Returns the value of attribute options.



6
7
8
# File 'lib/git_switch/switcher.rb', line 6

def options
  @options
end

#profileObject (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

Returns:

  • (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

Returns:

  • (Boolean)


23
24
25
# File 'lib/git_switch/switcher.rb', line 23

def global?
  options.global?
end

#list?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/git_switch/switcher.rb', line 19

def list?
  options.list?
end


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


69
70
71
72
73
74
75
76
77
78
# File 'lib/git_switch/switcher.rb', line 69

def print_settings(flag = '')
  if options.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

#runObject



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 options.valid_args? && valid_profile?
  return unless git_repo?

  flag = global? ? '--global' : ''

  set_git_config(flag)
  set_ssh
  print_settings(flag)
end

#valid_profile?Boolean

Returns:

  • (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