Skip to content

Commit

Permalink
Create the pgpass file if it does not exist.
Browse files Browse the repository at this point in the history
  • Loading branch information
wayneeseguin committed Apr 24, 2012
1 parent e1a5dfa commit c7d27b3
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions lib/psql-cm/database.rb
Expand Up @@ -36,19 +36,20 @@ def close!
end end


def pgpass # Ensure a pgpass entry exists for this connection. 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], pgpass_line = [ @config[:host], @config[:port], @config[:dbname],
@config[:user], @config[:password] ].join(':') @config[:user], @config[:password] ].join(':')


content = File.open(File.join(ENV['HOME'], '.pgpass'), 'r') do |file| content = File.read(pgpass_file).split("\n")
file.read
end.split("\n")


unless content.detect{ |line| line == pgpass_line } 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 content << pgpass_line
file.write(content.join("\n") + "\n") file.write(content.join("\n") + "\n")
end end
File.chmod(0600, File.join(ENV['HOME'], '.pgpass')) File.chmod(0600, pgpass_file)
end end
pgpass_line pgpass_line
end # def pgpass end # def pgpass
Expand Down

0 comments on commit c7d27b3

Please sign in to comment.