Class: NcsNavigator::Warehouse::PostgreSQL::Pgpass

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/ncs_navigator/warehouse/postgresql/pgpass.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePgpass

Returns a new instance of Pgpass.



30
31
32
# File 'lib/ncs_navigator/warehouse/postgresql/pgpass.rb', line 30

def initialize
  @file = Pathname.new(ENV['HOME']) + '.pgpass'
end

Instance Attribute Details

#fileObject (readonly)

Returns the value of attribute file.



28
29
30
# File 'lib/ncs_navigator/warehouse/postgresql/pgpass.rb', line 28

def file
  @file
end

Class Method Details

.line(entry) ⇒ Array<String>

Converts a database configuration hash into the elements of the corresponding .pgpass line.

Parameters:

  • the (Hash<String, String>)

    configuration

Returns:

  • (Array<String>)


16
17
18
19
20
21
22
23
24
# File 'lib/ncs_navigator/warehouse/postgresql/pgpass.rb', line 16

def self.line(entry)
  [
    entry['host'] || 'localhost',
    (entry['port'] || 5432).to_s,
    '*',
    entry['username'] || fail("No username in configuration #{entry.inspect}"),
    entry['password'] || fail("No password in configuration #{entry.inspect}")
  ]
end

Instance Method Details

#update(entry)

This method returns an undefined value.

Updates the .pgpass file so that it includes the current password for the given configuration. This may involve adding an entry, replacing an entry, or even creating the file entirely.

Parameters:

  • entry (Hash)

    a database configuration hash



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/ncs_navigator/warehouse/postgresql/pgpass.rb', line 41

def update(entry)
  ensure_file_exists_and_is_writable

  new_line = self.line(entry)
  contents = file.readlines.collect { |l| l.chomp.split(':') }
  match = contents.detect { |line| line[0..3] == new_line[0..3] }

  if match
    match[4] = entry['password']
  else
    contents << new_line
  end

  file.open('w') do |f|
    contents.each do |l|
      f.puts l.join(':')
    end
  end
  file.chmod(0600)
end