Class: GitSwitch::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/git_switch/config.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#profilesObject (readonly)

Returns the value of attribute profiles.



5
6
7
# File 'lib/git_switch/config.rb', line 5

def profiles
  @profiles
end

#selected_profileObject (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_profilesObject



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

#emailObject



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

#nameObject



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


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

#profileObject



36
37
38
# File 'lib/git_switch/config.rb', line 36

def profile
  @selected_profile
end

#sshObject



28
29
30
# File 'lib/git_switch/config.rb', line 28

def ssh
  profiles[selected_profile]["ssh"]
end

#ssh_commandObject



32
33
34
# File 'lib/git_switch/config.rb', line 32

def ssh_command
  "ssh -i #{ssh}"
end

#usernameObject



16
17
18
# File 'lib/git_switch/config.rb', line 16

def username
  profiles[selected_profile]["username"]
end

#valid_profile?Boolean

Returns:

  • (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_fileObject



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.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