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
/
config.rb
122 lines (105 loc) · 3.1 KB
/
config.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
require 'yaml'
module GitSwitch
class Config
attr_reader :profiles, :selected_profile
def initialize(args)
@profiles = load!
@args = args
@selected_profile = get_profile(args)
end
def get_profile(args)
args.detect {|a| !a.start_with? '-'}
end
def name
profiles[selected_profile]["name"]
end
def username
profiles[selected_profile]["username"]
end
def email
profiles[selected_profile]["email"]
end
def ssh
profiles[selected_profile]["ssh"]
end
def profile
@selected_profile
end
def valid_profile?
if profiles.has_key?(selected_profile)
return true
else
puts "Profile '#{selected_profile}' not found!"
return false
end
end
def configure!
@profiles = build_profiles
write_profiles_to_config_file if @profiles.any?
end
def build_profiles
puts "How many profiles would you like to create?"
profile_count = STDIN.gets.chomp.to_i
profiles = Array.new(profile_count, {})
current = 1
profiles.map do |profile|
puts "\n#{ordinal(current)} profile name (e.g. 'work' or 'personal'):"
profile[:profile_name] = STDIN.gets.chomp
puts "Git username for #{profile[:profile_name]}:"
profile[:git_username] = STDIN.gets.chomp
puts "Git email for #{profile[:profile_name]}:"
profile[:git_email] = STDIN.gets.chomp
puts "Git name for #{profile[:profile_name]}:"
profile[:git_name] = STDIN.gets.chomp
puts "Path to ssh key for #{profile[:profile_name]} (e.g. '~/.ssh/id_rsa'):"
profile[:ssh_key] = STDIN.gets.chomp
current +=1
profile.dup
end
rescue Interrupt
return {}
end
def ordinal(number)
# Source: https://github.com/infertux/ordinalize_full
abs_number = number.abs
suffix = if (11..13).cover?(abs_number % 100)
"th"
else
case abs_number % 10
when 1 then "st"
when 2 then "nd"
when 3 then "rd"
else "th"
end
end
"#{abs_number}#{suffix}"
end
def write_profiles_to_config_file
File.open(File.expand_path('~/.gitswitch'), 'w') do |file|
profiles.each do |profile|
file.write "#{profile[:profile_name]}:\n"
file.write " username: #{profile[:git_username]}\n"
file.write " email: #{profile[:git_email]}\n"
file.write " name: #{profile[:git_name]}\n"
file.write " ssh: #{profile[:ssh_key]}\n"
end
end
end
def print_list
profiles = @profiles.map do |key, value|
prefix = value["username"] == current_git_username ? "=>" : " "
"#{prefix} #{key}"
end
puts profiles
puts "\n# => - current" if @profiles.any? {|key, value| value["username"] == current_git_username}
end
private
def load!
# TODO: RCR - Handle missing or empty config file
YAML.load_file(File.expand_path('~/.gitswitch')) || {}
end
def current_git_username
`git config user.username`.chomp
end
end
end