Sha256: 91d5e3f4e45413edd673a2d4ff39052c7adff6c6762b6d8588ee35a9509a63ef

Contents?: true

Size: 787 Bytes

Versions: 4

Compression:

Stored size: 787 Bytes

Contents

require "authorized_keys"

module AuthorizedKeys
  class File
    attr_accessor :location
    private       :location=

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

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

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

    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

  private
    def modify(mode, &block)
      ::File.open(location, mode, &block)
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
authorized_keys-1.1.1 lib/authorized_keys/file.rb
authorized_keys-1.1.0 lib/authorized_keys/file.rb
authorized_keys-1.0.1 lib/authorized_keys/file.rb
authorized_keys-1.0.0 lib/authorized_keys/file.rb