Class: AuthorizedKeys::File

Inherits:
Object
  • Object
show all
Defined in:
lib/authorized_keys/file.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(location = nil) ⇒ File

Returns a new instance of File.



8
9
10
# File 'lib/authorized_keys/file.rb', line 8

def initialize(location=nil)
  self.location = location || "#{ENV['HOME']}/.ssh/authorized_keys"
end

Instance Attribute Details

#locationObject

Returns the value of attribute location.



5
6
7
# File 'lib/authorized_keys/file.rb', line 5

def location
  @location
end

Instance Method Details

#add(key) ⇒ Object



12
13
14
15
16
17
18
# File 'lib/authorized_keys/file.rb', line 12

def add(key)
  key = Key.new(key) if key.is_a?(String)

  modify 'a+' do |file|
    file.puts key
  end
end

#remove(key) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/authorized_keys/file.rb', line 20

def remove(key)
  key = Key.new(key) if key.is_a?(String)

  modify 'r' do |file|
    ::File.unlink(location)

    modify 'w' do |new_file|
      file.each do |line|
        new_file.puts line unless key == Key.new(line)
        new_file.flush
      end
    end
  end
end