1 | 'use strict'
|
2 |
|
3 | const co = require('co')
|
4 | const cli = require('heroku-cli-util')
|
5 |
|
6 | function * run (context, heroku) {
|
7 | const fetcher = require('../lib/fetcher')(heroku)
|
8 | const psql = require('../lib/psql')
|
9 |
|
10 | const {app, args, flags} = context
|
11 |
|
12 | let db = yield fetcher.database(app, args.database)
|
13 | cli.console.error(`--> Connecting to ${cli.color.addon(db.attachment.addon.name)}`)
|
14 | if (flags.command) {
|
15 | process.stdout.write(yield psql.exec(db, flags.command))
|
16 | } else {
|
17 | yield psql.interactive(db)
|
18 | }
|
19 | }
|
20 |
|
21 | let cmd = {
|
22 | description: 'open a psql shell to the database',
|
23 | needsApp: true,
|
24 | needsAuth: true,
|
25 | flags: [{name: 'command', char: 'c', description: 'SQL command to run', hasValue: true}],
|
26 | args: [{name: 'database', optional: true}],
|
27 | run: cli.command({preauth: true}, co.wrap(run))
|
28 | }
|
29 |
|
30 | module.exports = [
|
31 | Object.assign({topic: 'pg', command: 'psql'}, cmd),
|
32 | Object.assign({topic: 'psql'}, cmd)
|
33 | ]
|