Class: GitSwitch::Config
- Inherits:
-
Object
- Object
- GitSwitch::Config
- Defined in:
- lib/git_switch/config.rb
Instance Attribute Summary collapse
-
#profiles ⇒ Object
readonly
Returns the value of attribute profiles.
-
#selected_profile ⇒ Object
readonly
Returns the value of attribute selected_profile.
Instance Method Summary collapse
- #build_profiles ⇒ Object
- #configure! ⇒ Object
- #email ⇒ Object
- #get_profile(args) ⇒ Object
-
#initialize(args) ⇒ Config
constructor
A new instance of Config.
- #name ⇒ Object
- #ordinal(number) ⇒ Object
- #print_list ⇒ Object
- #profile ⇒ Object
- #ssh ⇒ Object
- #ssh_command ⇒ Object
- #username ⇒ Object
- #valid_profile? ⇒ Boolean
- #write_profiles_to_config_file ⇒ Object
Constructor Details
#initialize(args) ⇒ Config
Returns a new instance of Config.
6 7 8 9 10 |
# File 'lib/git_switch/config.rb', line 6 def initialize(args) @profiles = load! @args = args @selected_profile = get_profile(args) end |
Instance Attribute Details
#profiles ⇒ Object (readonly)
Returns the value of attribute profiles.
5 6 7 |
# File 'lib/git_switch/config.rb', line 5 def profiles @profiles end |
#selected_profile ⇒ Object (readonly)
Returns the value of attribute selected_profile.
5 6 7 |
# File 'lib/git_switch/config.rb', line 5 def selected_profile @selected_profile end |
Instance Method Details
#build_profiles ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/git_switch/config.rb', line 54 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 |
#configure! ⇒ Object
49 50 51 52 |
# File 'lib/git_switch/config.rb', line 49 def configure! @profiles = build_profiles write_profiles_to_config_file if @profiles.any? end |
#email ⇒ Object
20 21 22 |
# File 'lib/git_switch/config.rb', line 20 def email profiles[selected_profile]["email"] end |
#get_profile(args) ⇒ Object
12 13 14 |
# File 'lib/git_switch/config.rb', line 12 def get_profile(args) args.detect {|a| !a.start_with? '-'} end |
#name ⇒ Object
24 25 26 |
# File 'lib/git_switch/config.rb', line 24 def name profiles[selected_profile]["name"] end |
#ordinal(number) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/git_switch/config.rb', line 78 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 |
#print_list ⇒ Object
106 107 108 109 110 111 112 113 |
# File 'lib/git_switch/config.rb', line 106 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 |
#profile ⇒ Object
36 37 38 |
# File 'lib/git_switch/config.rb', line 36 def profile @selected_profile end |
#ssh ⇒ Object
28 29 30 |
# File 'lib/git_switch/config.rb', line 28 def ssh profiles[selected_profile]["ssh"] end |
#ssh_command ⇒ Object
32 33 34 |
# File 'lib/git_switch/config.rb', line 32 def ssh_command "ssh -i #{ssh}" end |
#username ⇒ Object
16 17 18 |
# File 'lib/git_switch/config.rb', line 16 def username profiles[selected_profile]["username"] end |
#valid_profile? ⇒ Boolean
40 41 42 43 44 45 46 47 |
# File 'lib/git_switch/config.rb', line 40 def valid_profile? if profiles.has_key?(selected_profile) return true else puts "Profile '#{selected_profile}' not found!" return false end end |
#write_profiles_to_config_file ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/git_switch/config.rb', line 94 def write_profiles_to_config_file File.open(File.('~/.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 |