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)


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

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

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



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

def config
  @config
end

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

Instance Method Details

#emailObject



35
36
37
# File 'lib/git_switch/switcher.rb', line 35

def email
  config.email
end

#git_repo?Boolean

Returns:

  • (Boolean)


55
56
57
58
59
60
61
62
# File 'lib/git_switch/switcher.rb', line 55

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)


51
52
53
# File 'lib/git_switch/switcher.rb', line 51

def global?
  options.global?
end

#list?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/git_switch/switcher.rb', line 47

def list?
  options.list?
end

#nameObject



27
28
29
# File 'lib/git_switch/switcher.rb', line 27

def name
  config.name
end


83
84
85
# File 'lib/git_switch/switcher.rb', line 83

def print_list
  config.print_list
end


87
88
89
90
91
92
93
94
95
96
# File 'lib/git_switch/switcher.rb', line 87

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


79
80
81
# File 'lib/git_switch/switcher.rb', line 79

def print_usage
  puts usage
end

#profileObject



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

def profile
  config.profile
end

#runObject



13
14
15
16
17
18
19
20
21
# File 'lib/git_switch/switcher.rb', line 13

def run
  if usage?
    print_usage
  elsif list?
    print_list
  else
    set!
  end
end

#set!Object



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

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

#sshObject



39
40
41
# File 'lib/git_switch/switcher.rb', line 39

def ssh
  config.ssh
end

#usage?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/git_switch/switcher.rb', line 43

def usage?
  options.usage?
end

#usernameObject



31
32
33
# File 'lib/git_switch/switcher.rb', line 31

def username
  config.username
end

#valid_profile?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/git_switch/switcher.rb', line 64

def valid_profile?
  config.valid_profile?
end