class Github::Auth::CLI

Command Line Interface for parsing and executing commands

Public Instance Methods

add() click to toggle source
# File lib/github/auth/cli.rb, line 26
def add
  on_keys_file :write!,
    "Adding #{keys.count} key(s) to '#{keys_file.path}'",
    { command: options[:command] }
end
list() click to toggle source
# File lib/github/auth/cli.rb, line 55
def list
  rescue_keys_file_errors { puts keys_file.github_users.join(' ') }
end
remove() click to toggle source
# File lib/github/auth/cli.rb, line 42
def remove
  on_keys_file :delete!,
    "Removing #{keys.count} key(s) from '#{keys_file.path}'"
end
version() click to toggle source
# File lib/github/auth/cli.rb, line 60
def version
  puts Github::Auth::VERSION
end

Private Instance Methods

github_hostname() click to toggle source
# File lib/github/auth/cli.rb, line 113
def github_hostname
  options[:host] || Github::Auth::KeysClient::DEFAULT_HOSTNAME
end
keys() click to toggle source
# File lib/github/auth/cli.rb, line 66
def keys
  @keys ||= begin
    Array(options[:users]).map { |user| keys_for user }.flatten.compact
  end
end
keys_file(options = {}) click to toggle source
# File lib/github/auth/cli.rb, line 104
def keys_file(options = {})
  Github::Auth::KeysFile.new \
    options.merge path: keys_file_path
end
keys_file_path() click to toggle source
# File lib/github/auth/cli.rb, line 109
def keys_file_path
  options[:path] || Github::Auth::KeysFile::DEFAULT_PATH
end
keys_for(username) click to toggle source
# File lib/github/auth/cli.rb, line 91
def keys_for(username)
  Github::Auth::KeysClient.new(
    hostname: github_hostname,
    username: username
  ).keys
rescue Github::Auth::KeysClient::GithubUserDoesNotExistError
  puts "Github user '#{username}' does not exist"
rescue Github::Auth::KeysClient::GithubUnavailableError
  puts "Github appears to be unavailable :("
  puts
  puts "https://status.github.com"
end
on_keys_file(action, message, options = {}) click to toggle source
# File lib/github/auth/cli.rb, line 72
def on_keys_file(action, message, options = {})
  puts message
  rescue_keys_file_errors { keys_file(options).send action, keys }
end
rescue_keys_file_errors() { || ... } click to toggle source
# File lib/github/auth/cli.rb, line 77
def rescue_keys_file_errors
  yield
rescue KeysFile::PermissionDeniedError
  puts 'Permission denied!'
  puts
  puts "Make sure you have write permissions for '#{keys_file.path}'"
rescue KeysFile::FileDoesNotExistError
  puts "Keys file does not exist!"
  puts
  puts "Create one now and try again:"
  puts
  puts "  $ touch #{keys_file.path}"
end