Class: NcsNavigator::Warehouse::PostgreSQL::Pgpass
- Inherits:
-
Object
- Object
- NcsNavigator::Warehouse::PostgreSQL::Pgpass
- Extended by:
- Forwardable
- Defined in:
- lib/ncs_navigator/warehouse/postgresql/pgpass.rb
Instance Attribute Summary collapse
-
#file ⇒ Object
readonly
Returns the value of attribute file.
Class Method Summary collapse
-
.line(entry) ⇒ Array<String>
Converts a database configuration hash into the elements of the corresponding .pgpass line.
Instance Method Summary collapse
-
#initialize ⇒ Pgpass
constructor
A new instance of Pgpass.
-
#update(entry)
Updates the
.pgpass
file so that it includes the current password for the given configuration.
Constructor Details
#initialize ⇒ Pgpass
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
#file ⇒ Object (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.
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.
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 |