Last active
October 11, 2017 20:07
-
-
Save nickcoutsos/af9d394f43c2b2ed6c1adc4496216daa to your computer and use it in GitHub Desktop.
psql via vagrant ssh with help from knex.js
This file contains hidden or 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 | |
// Usage: vpsql [<environment>] [-- [<psql args>]] | |
// | |
// This script looks up the appropriate PostgreSQL database information in your | |
// local Vagrant development environment and opens a psql session via the | |
// vagrant ssh command. Without specifying the name of a database (as per your | |
// knexfile) it will default to "development" or "dev" (whichever exists). In | |
// addition to that you can pass arbitrary arguments through to the psql command | |
// by including them after "--". | |
// | |
// Examples: | |
// | |
// Connect to your configured `test` database instead of `dev`: | |
// $ vpsql test | |
// | |
// Show the schema for `myTable`: | |
// $ vpsql -- -c '\d myTable' | |
// | |
// Try out some query: | |
// $ vpsql -- -c 'select * from users where id=1;' | |
// | |
// Note that psql likes to page larger outputs. If you have a custom PAGER | |
// environment variable (or specify one when using vpsql) it will be passed in | |
// through the vagrant ssh call, so you don't need to include unnecessary | |
// customizations in your VM. | |
// | |
// Installation: | |
// This script should live somewhere in your PATH and run from a directory that | |
// 1) exists under a vagrant VM directory | |
// 2) includes a file by the name `knexfile.js` | |
const fs = require('fs') | |
const process = require('process') | |
const childProcess = require('child_process') | |
let knexFile, knexEnv | |
function parseArgs(args) { | |
const split = args.indexOf('--') | |
return split !== -1 | |
? [args.slice(0, split), args.slice(split + 1)] | |
: [args, []] | |
} | |
function psqlCommand(dbEnv, extraArgs) { | |
const pager = 'PAGER=${PAGER:-"' + (process.env.PAGER || 'less -S') + '"}' | |
const extraArgString = extraArgs.map(arg => `'${arg}'`).join(' ') | |
const { connection } = dbEnv | |
if (typeof connection === 'string') { | |
return `${pager} psql ${connection} ${extraArgString}` | |
} | |
const { database } = connection | |
const user = connection.user || connection.username | |
return `${pager} psql -U ${user} -d ${database} ${extraArgString}` | |
} | |
try { | |
knexFile = require(`${process.cwd()}/knexfile.js`) | |
} catch (err) { | |
console.error('Could not open "knexfile.js". Perhaps you are in the wrong directory?') | |
process.exit(1) | |
} | |
const [ scriptArgs, extraArgs ] = parseArgs(process.argv.slice(2)) | |
const [ desiredEnv ] = scriptArgs | |
if (desiredEnv) { | |
knexEnv = knexFile[desiredEnv] | |
} else { | |
knexEnv = knexFile.development || knexFile.dev | |
} | |
if (!knexEnv) { | |
console.error('Could not find an appropriate knex database') | |
process.exit(1) | |
} | |
childProcess.spawn( | |
'vagrant', | |
['ssh', '-c', psqlCommand(knexEnv, extraArgs)], | |
{ stdio: 'inherit' } | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment