Skip to content
Browse files

Create the pgpass file if it does not exist.

  • Loading branch information...
1 parent e1a5dfa commit c7d27b3c2905cbee3a784d421e6ff5f4ad3bbb9e @wayneeseguin committed Apr 24, 2012
Showing with 6 additions and 5 deletions.
  1. +6 −5 lib/psql-cm/database.rb
View
11 lib/psql-cm/database.rb
@@ -36,19 +36,20 @@ def close!
end
def pgpass # Ensure a pgpass entry exists for this connection.
+ pgpass_file = File.join(ENV['HOME'], '.pgpass')
+ File.touch(pgpass_file) unless File.exists?(pgpass_file)
+
pgpass_line = [ @config[:host], @config[:port], @config[:dbname],
@config[:user], @config[:password] ].join(':')
- content = File.open(File.join(ENV['HOME'], '.pgpass'), 'r') do |file|
- file.read
- end.split("\n")
+ content = File.read(pgpass_file).split("\n")
unless content.detect{ |line| line == pgpass_line }
- File.open(File.join(ENV['HOME'], '.pgpass'), 'w') do |file|
+ File.open(pgpass_file, 'w') do |file|
content << pgpass_line
file.write(content.join("\n") + "\n")
end
- File.chmod(0600, File.join(ENV['HOME'], '.pgpass'))
+ File.chmod(0600, pgpass_file)
end
pgpass_line
end # def pgpass

0 comments on commit c7d27b3

Please sign in to comment.
Something went wrong with that request. Please try again.