UNPKG

2 kBJavaScriptView Raw
1'use strict'
2
3const cli = require('heroku-cli-util')
4const { sortBy } = require('lodash')
5const util = require('../lib/util')
6
7async function run(context, heroku) {
8 const fetcher = require('../lib/fetcher')(heroku)
9
10 const { app, args, flags } = context
11
12 let showCredentials = async function () {
13 const host = require('../lib/host')
14 let addon = await fetcher.addon(app, args.database)
15 let attachments = []
16 let credentials = []
17
18 function presentCredential (cred) {
19 let credAttachments = []
20 if (cred !== 'default') {
21 credAttachments = attachments.filter(a => a.namespace === `credential:${cred}`)
22 } else {
23 credAttachments = attachments.filter(a => a.namespace === null)
24 }
25 return util.presentCredentialAttachments(app, credAttachments, credentials, cred)
26 }
27
28 credentials = await heroku.get(`/postgres/v0/databases/${addon.name}/credentials`,
29 { host: host(addon) })
30 let isDefaultCredential = (cred) => cred.name !== 'default'
31 credentials = sortBy(credentials, isDefaultCredential, 'name')
32 attachments = await heroku.get(`/addons/${addon.name}/addon-attachments`)
33
34 cli.warn(`${cli.color.cmd('pg:credentials')} has recently changed. Please use ${cli.color.cmd('pg:credentials:url')} for the previous output.`)
35 cli.table(credentials, {
36 columns: [
37 { key: 'name', label: 'Credential', format: presentCredential },
38 { key: 'state', label: 'State' }
39 ]
40 })
41 }
42
43 if (flags.reset) {
44 cli.error(`${cli.color.cmd('pg:credentials --reset')} is deprecated. Please use ${cli.color.cmd('pg:credentials:rotate')} instead.`)
45 } else {
46 await showCredentials()
47 }
48}
49
50module.exports = {
51 topic: 'pg',
52 command: 'credentials',
53 description: 'show information on credentials in the database',
54 needsApp: true,
55 needsAuth: true,
56 flags: [{ name: 'reset', description: 'DEPRECATED' }],
57 args: [{ name: 'database', optional: true }],
58 run: cli.command({ preauth: true }, run)
59}