Skip to content

Instantly share code, notes, and snippets.

@djromero
Created April 14, 2015 21:38
Show Gist options
  • Save djromero/fd10ff072cbc941ce0e4 to your computer and use it in GitHub Desktop.
Save djromero/fd10ff072cbc941ce0e4 to your computer and use it in GitHub Desktop.
Write the psql command for a .env line in URL format.
#!ruby
# Usage:
# ruby psql.rb <VAR_NAME>
#
require 'URI'
name = ARGV[0]
starts_with_name = Regexp.new('^' + name)
uri = nil
File.open('.env') do |file|
file.each do |line|
if line =~ starts_with_name
uri = URI.parse(line.split('=')[1])
end
end
end
port_part = " -p #{uri.port}" if uri.port != 5432
puts "psql -h #{uri.host} -U #{uri.user}#{port_part} #{uri.path.sub(/^\//, '')}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment