class GitSwitch::Config

Attributes

profiles[R]
selected_profile[R]

Public Class Methods

new(args) click to toggle source
# File lib/git_switch/config.rb, line 6
def initialize(args)
  @profiles = load!
  @args = args
  @selected_profile = get_profile(args)
end

Public Instance Methods

build_profiles() click to toggle source
# File lib/git_switch/config.rb, line 58
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!() click to toggle source
# File lib/git_switch/config.rb, line 49
def configure!
  @profiles = build_profiles
  write_profiles_to_config_file if @profiles.any?
end
edit!() click to toggle source
# File lib/git_switch/config.rb, line 54
def edit!
  system("#{ENV['EDITOR']} '#{File.expand_path('~/.gitswitch')}'")
end
email() click to toggle source
# File lib/git_switch/config.rb, line 20
def email
  profiles[selected_profile]["email"]
end
get_profile(args) click to toggle source
# File lib/git_switch/config.rb, line 12
def get_profile(args)
  args.detect {|a| !a.start_with? '-'}
end
name() click to toggle source
# File lib/git_switch/config.rb, line 24
def name
  profiles[selected_profile]["name"]
end
ordinal(number) click to toggle source
# File lib/git_switch/config.rb, line 82
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() click to toggle source
profile() click to toggle source
# File lib/git_switch/config.rb, line 36
def profile
  @selected_profile
end
ssh() click to toggle source
# File lib/git_switch/config.rb, line 28
def ssh
  profiles[selected_profile]["ssh"]
end
ssh_command() click to toggle source
# File lib/git_switch/config.rb, line 32
def ssh_command
  "ssh -i #{ssh}"
end
username() click to toggle source
# File lib/git_switch/config.rb, line 16
def username
  profiles[selected_profile]["username"]
end
valid_profile?() click to toggle source
# 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() click to toggle source
# File lib/git_switch/config.rb, line 98
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

Private Instance Methods

current_git_username() click to toggle source
# File lib/git_switch/config.rb, line 126
def current_git_username
  `git config user.username`.chomp
end
load!() click to toggle source
# File lib/git_switch/config.rb, line 121
def load!
  # TODO: RCR - Handle missing or empty config file
  YAML.load_file(File.expand_path('~/.gitswitch')) || {}
end