class Github::Auth::KeysFile

Write and delete keys from the authorized_keys file

Constants

DEFAULT_PATH
FileDoesNotExistError
PermissionDeniedError

Attributes

command[R]
path[R]

Public Class Methods

new(options = {}) click to toggle source
# File lib/github/auth/keys_file.rb, line 11
def initialize(options = {})
  @path = File.expand_path(options[:path] || DEFAULT_PATH)
  @command = options[:command]
end

Public Instance Methods

delete!(keys) click to toggle source
# File lib/github/auth/keys_file.rb, line 29
def delete!(keys)
  new_content = keys_file_content_without keys

  write_keys_file { |keys_file| keys_file.write new_content }
end
github_users() click to toggle source
# File lib/github/auth/keys_file.rb, line 35
def github_users
  # http://rubular.com/r/zXCkewmm0i
  regex = %r{github\.com/(\S+)}
  keys_file_content.scan(regex).flatten.uniq.sort
end
write!(keys) click to toggle source
# File lib/github/auth/keys_file.rb, line 16
def write!(keys)
  Array(keys).each do |key|
    unless keys_file_content.include? key.key
      append_keys_file do |keys_file|
        unless keys_file_content.empty? || keys_file_content.end_with?("\n")
          keys_file.write "\n"
        end
        keys_file.write "#{"command=\"#{command}\" " if command}#{key}\n"
      end
    end
  end
end

Private Instance Methods

append_keys_file(&block) click to toggle source
# File lib/github/auth/keys_file.rb, line 43
def append_keys_file(&block)
  with_keys_file 'a', block
end
keys_file_content() click to toggle source
# File lib/github/auth/keys_file.rb, line 51
def keys_file_content
  with_keys_file 'r', Proc.new { |keys_file| keys_file.read }
end
keys_file_content_without(keys) click to toggle source
# File lib/github/auth/keys_file.rb, line 63
def keys_file_content_without(keys)
  keys_file_content.tap do |content|
    Array(keys).each do |key|
      content.gsub! /(.*)?#{Regexp.escape key.key}(.*)?$\n?/, ''
    end

    content << "\n" unless content.empty? || content.end_with?("\n")
  end
end
with_keys_file(mode, block) click to toggle source
# File lib/github/auth/keys_file.rb, line 55
def with_keys_file(mode, block)
  File.open(path, mode) { |keys_file| block.call keys_file }
rescue Errno::EACCES => e
  raise PermissionDeniedError, e
rescue Errno::ENOENT => e
  raise FileDoesNotExistError, e
end
write_keys_file(&block) click to toggle source
# File lib/github/auth/keys_file.rb, line 47
def write_keys_file(&block)
  with_keys_file 'w', block
end