UNPKG

1.37 kBJavaScriptView Raw
1'use strict'
2
3const cli = require('heroku-cli-util')
4const co = require('co')
5
6function * run (context, heroku) {
7 const host = require('../../lib/host')
8 const fetcher = require('../../lib/fetcher')(heroku)
9 let {app, args} = context
10
11 let dbs
12 if (args.database) dbs = yield [fetcher.addon(app, args.database)]
13 else dbs = yield fetcher.all(app)
14
15 if (!dbs.length) throw new Error(`No databases on ${cli.color.app(app)}`)
16
17 dbs = yield dbs.map(db => {
18 db.links = heroku.get(`/client/v11/databases/${db.id}/links`, {host: host(db)})
19 return db
20 })
21
22 let once
23 dbs.forEach(db => {
24 if (once) cli.log()
25 else once = true
26 cli.styledHeader(cli.color.addon(db.name))
27 if (db.links.message) return cli.log(db.links.message)
28 if (!db.links.length) return cli.log('No data sources are linked into this database')
29 db.links.forEach(link => {
30 cli.log(`\n * ${cli.color.cyan(link.name)}`)
31 link.remote = `${cli.color.configVar(link.remote.attachment_name)} (${cli.color.addon(link.remote.name)})`
32 delete link.id
33 delete link.name
34 cli.styledObject(link)
35 })
36 })
37}
38
39module.exports = {
40 topic: 'pg',
41 command: 'links',
42 description: 'lists all databases and information on link',
43 needsApp: true,
44 needsAuth: true,
45 args: [{name: 'database', optional: true}],
46 run: cli.command({preauth: true}, co.wrap(run))
47}