UNPKG

1.51 kBJavaScriptView Raw
1'use strict'
2
3const cli = require('heroku-cli-util')
4
5async function run(context, heroku) {
6 const fetcher = require('../lib/fetcher')(heroku)
7 const psql = require('../lib/psql')
8
9 const { app, args, flags } = context
10
11 const namespace = flags.credential ? `credential:${flags.credential}` : null
12 let db
13 try {
14 db = await fetcher.database(app, args.database, namespace)
15 } catch (err) {
16 if (namespace && err.message === 'Couldn\'t find that addon.') {
17 throw new Error('Credential doesn\'t match, make sure credential is attached')
18 }
19 throw err
20 }
21 cli.console.error(`--> Connecting to ${cli.color.addon(db.attachment.addon.name)}`)
22 if (flags.command) {
23 const output = await psql.exec(db, flags.command)
24 process.stdout.write(output)
25 } else if (flags.file) {
26 const output = await psql.execFile(db, flags.file)
27 process.stdout.write(output)
28 } else {
29 await psql.interactive(db)
30 }
31}
32
33const cmd = {
34 description: 'open a psql shell to the database',
35 needsApp: true,
36 needsAuth: true,
37 flags: [
38 { name: 'command', char: 'c', description: 'SQL command to run', hasValue: true },
39 { name: 'file', char: 'f', description: 'SQL file to run', hasValue: true },
40 { name: 'credential', description: 'credential to use', hasValue: true }
41 ],
42 args: [{ name: 'database', optional: true }],
43 run: cli.command({ preauth: true }, run)
44}
45
46module.exports = [
47 Object.assign({ topic: 'pg', command: 'psql' }, cmd),
48 Object.assign({ topic: 'psql' }, cmd)
49]