This repository has been archived by the owner on Sep 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
switcher.rb
100 lines (82 loc) · 2.3 KB
/
switcher.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
require_relative './version'
require 'active_support/core_ext/module/delegation'
module GitSwitch
class Switcher
attr_reader :config, :options
delegate :usage?, :config?, :list?, :global?, to: :options
delegate :profile, :name, :username, :email, :ssh, :ssh_command, :print_list, :configure!, :valid_profile?, to: :config
def initialize(args)
raise ArgumentError unless args.is_a? Array
@options = GitSwitch::Options.new(args)
@config = GitSwitch::Config.new(args)
end
def run
return unless options.valid_args?
if usage?
print_usage
elsif config?
configure!
elsif list?
print_list
else
set!
end
end
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
def set!
return unless valid_profile? && git_repo?
set_git_config
set_ssh
print_settings
end
def print_usage
puts usage
end
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
private
def git_config_flag
@git_config_flag ||= global? ? '--global' : ''
end
def set_git_config
`git config #{git_config_flag} user.name "#{name}"`
`git config #{git_config_flag} user.username "#{username}"`
`git config #{git_config_flag} user.email "#{email}"`
end
def set_ssh
`git config #{git_config_flag} core.sshCommand "#{ssh_command}"`
`ssh-add #{ssh}`
end
def usage
<<~USAGE
usage: git switch [-l | --list] [-c | --config]
<profile> [-v | --verbose] [-g | --global]
configure profiles
git switch -c
switch to a profile for local development only
git switch <profile>
switch to a profile globally
git switch -g <profile>
switch to a profile and see all output
git switch -v <profile>
see available profiles
git switch -l
USAGE
end
end
end