Class: Github::Auth::KeysFile

Inherits:
Object
  • Object
show all
Defined in:
lib/github/auth/keys_file.rb

Overview

Write and delete keys from the authorized_keys file

Constant Summary collapse

PermissionDeniedError =
Class.new StandardError
FileDoesNotExistError =
Class.new StandardError
DEFAULT_PATH =
'~/.ssh/authorized_keys'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ KeysFile

Returns a new instance of KeysFile.



11
12
13
14
# File 'lib/github/auth/keys_file.rb', line 11

def initialize(options = {})
  @path = File.expand_path(options[:path] || DEFAULT_PATH)
  @command = options[:command]
end

Instance Attribute Details

#commandObject (readonly)

Returns the value of attribute command.



4
5
6
# File 'lib/github/auth/keys_file.rb', line 4

def command
  @command
end

#pathObject (readonly)

Returns the value of attribute path.



4
5
6
# File 'lib/github/auth/keys_file.rb', line 4

def path
  @path
end

Instance Method Details

#delete!(keys) ⇒ Object



29
30
31
32
33
# 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_usersObject



35
36
37
38
39
# 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) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
# 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