class Gaman::GitAccountManager

Public Instance Methods

current_user() click to toggle source
# File lib/gaman/cli.rb, line 27
def current_user
  check_current_user options.fetch('server', 'github')
end
display_ssh_keys(ssh_keys) click to toggle source
# File lib/gaman/cli.rb, line 67
def display_ssh_keys(ssh_keys)
  fail ArgumentError, 'ssh_keys must be an Array' unless ssh_keys.is_a? Array

  notice_message('You have no ssh key.') if ssh_keys.empty?

  ssh_keys.each_with_index do |key, index|
    puts "[#{Rainbow(index).underline.bright.cyan}] - #{key}"
  end
end
list() click to toggle source
# File lib/gaman/cli.rb, line 41
def list
  notice_message('Checking ssh keys in your machine...')
  display_ssh_keys(all_public_ssh_file)
end
new() click to toggle source
# File lib/gaman/cli.rb, line 48
def new
  system("ssh-keygen -t rsa -b 4096 -C #{options[:email]}")
end
show() click to toggle source
# File lib/gaman/cli.rb, line 32
def show
  eval_ssh_agent_s

  get_user_input_number(all_public_ssh_file) do |number, ssh_keys|
    show_ssh_key(number, ssh_keys)
  end
end
switch(key_index = nil) click to toggle source
# File lib/gaman/cli.rb, line 58
def switch(key_index = nil)
  if key_index.nil?
    switch_by_showing_list_keys
  else
    switch_by_key_index(key_index.to_i)
  end
end
version() click to toggle source
# File lib/gaman/cli.rb, line 14
def version
  puts "Gaman version #{::Gaman::VERSION} on Ruby #{RUBY_VERSION}"
end

Private Instance Methods

all_public_ssh_file() click to toggle source
# File lib/gaman/cli.rb, line 100
def all_public_ssh_file
  if File.exist?(ssh_path)
    Dir["#{ssh_path}/*.pub"]
  else
    notice_message('You have no ssh key. To create new one, run this command:')
    puts Rainbow('$ gaman new -e your_email@domain.com').blue
    []
  end
end
check_current_user(server) click to toggle source
# File lib/gaman/cli.rb, line 154
def check_current_user(server)
  servers = { 'github' => 'github.com', 'bitbucket' => 'bitbucket.org' }
  notice_message("Checking ssh conection to #{server}...")
  system("ssh -T git@#{servers[server]}")
end
check_number_and_yield_if_valid(number, ssh_keys) { |number, ssh_keys| ... } click to toggle source
# File lib/gaman/cli.rb, line 124
def check_number_and_yield_if_valid(number, ssh_keys)
  if number_valid?(number, ssh_keys)
    block_given? ? yield(number, ssh_keys) : [number, ssh_keys]
  else
    error_message('Wrong value. Exiting...')
  end
end
eval_ssh_agent_s() click to toggle source
# File lib/gaman/cli.rb, line 96
def eval_ssh_agent_s
  system('eval "$(ssh-agent -s)"')
end
get_user_input_number(ssh_keys) { |number, ssh_keys| ... } click to toggle source
# File lib/gaman/cli.rb, line 110
def get_user_input_number(ssh_keys)
  return error_message('There are no ssh keys. Exiting...') if ssh_keys.empty?

  notice_message('Current ssh keys on your system:')
  message = 'Which key do you want to switch? [Input number]'
  number = input_number(ssh_keys, message)
  if number_valid?(number, ssh_keys)
    block_given? ? yield(number, ssh_keys) : [number, ssh_keys]
  else
    error_message('Wrong value. Exiting...')
  end
  # check_number_and_yield_if_valid(number, ssh_keys)
end
input_number(ssh_keys, message) click to toggle source
# File lib/gaman/cli.rb, line 132
def input_number(ssh_keys, message)
  display_ssh_keys(ssh_keys)
  number = ask(notice_message(message))
  begin
    Integer(number)
  rescue
    nil
  end
end
number_valid?(number, ssh_keys) click to toggle source
# File lib/gaman/cli.rb, line 142
def number_valid?(number, ssh_keys)
  !number.nil? && number >= 0 && number <= ssh_keys.size - 1
end
show_ssh_key(number, ssh_keys) click to toggle source
# File lib/gaman/cli.rb, line 160
def show_ssh_key(number, ssh_keys)
  key = ssh_keys[number]
  system("cat #{key}")
end
switch_by_key_index(key_index) click to toggle source
# File lib/gaman/cli.rb, line 80
def switch_by_key_index(key_index)
  ssh_keys = all_public_ssh_file
  return error_message('There are no ssh keys. Exiting...') if ssh_keys.empty?
  check_number_and_yield_if_valid(key_index, ssh_keys) do |number, ssh_keys|
    switch_ssh_key(number, ssh_keys)
  end
end
switch_by_showing_list_keys() click to toggle source
# File lib/gaman/cli.rb, line 88
def switch_by_showing_list_keys
  eval_ssh_agent_s

  get_user_input_number(all_public_ssh_file) do |number, ssh_keys|
    switch_ssh_key(number, ssh_keys)
  end
end
switch_ssh_key(number, ssh_keys) click to toggle source
# File lib/gaman/cli.rb, line 146
def switch_ssh_key(number, ssh_keys)
  key = ssh_keys[number][0..-5]
  system('ssh-add -D')
  notice_message("Adding #{key}")
  system("ssh-add #{key}")
  current_user
end