Last active
June 13, 2018 12:35
-
-
Save jan-osch/102e36b7b8e18a02f7dd443041e98143 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
const url = require('url'); | |
const parse = (connectionString) => { | |
if (!connectionString) { | |
return 'Connection string is needed'; | |
} | |
const parsed = url.parse(connectionString); | |
const result = ['psql']; | |
if (parsed.auth) { | |
const [userName, password] = parsed.auth.split(':'); | |
result.unshift(`PGPASSWORD=${password}`); | |
result.push('-w'); | |
result.push(`-U ${userName}`); | |
} | |
if (parsed.port) { | |
result.push(`-p ${parsed.port}`); | |
} | |
if (parsed.hostname) { | |
result.push(`-h ${parsed.hostname}`); | |
} | |
if (parsed.pathname) { | |
result.push(parsed.pathname.slice(1)); | |
} | |
return result.join(' '); | |
}; | |
console.log(parse(process.argv[2])); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment