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)


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

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.



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

Instance Method Details

#git_repo?Boolean



33
34
35
36
37
38
39
40
# File 'lib/git_switch/switcher.rb', line 33

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


58
59
60
61
62
63
64
65
66
67
# File 'lib/git_switch/switcher.rb', line 58

def print_settings
  if options.verbose?
    puts "\nGit Config:"
    puts `git config #{git_config_flag} -l --show-origin | grep user`
    puts "\nSSH:"
    puts `ssh-add -l`
  else
    puts "Switched to profile #{profile}"
  end
end


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

def print_usage
  puts usage
end


54
55
56
# File 'lib/git_switch/switcher.rb', line 54

def print_version
  puts GitSwitch::VERSION
end

#runObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/git_switch/switcher.rb', line 16

def run
  return unless options.valid_args?
  if usage?
    print_usage
  elsif config?
    configure!
  elsif edit?
    edit!
  elsif list?
    print_list
  elsif version?
    print_version
  else
    set!
  end
end

#set!Object



42
43
44
45
46
47
48
# File 'lib/git_switch/switcher.rb', line 42

def set!
  return unless valid_profile? && git_repo?

  set_git_config
  set_ssh
  print_settings
end