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