UNPKG

1.94 kBJavaScriptView Raw
1'use strict'
2
3const co = require('co')
4const cli = require('heroku-cli-util')
5const url = require('url')
6
7function * run (context, heroku) {
8 const host = require('../../lib/host')
9 const fetcher = require('../../lib/fetcher')(heroku)
10 const util = require('../../lib/util')
11
12 const {app, args, flags} = context
13
14 let db = yield fetcher.addon(app, args.database)
15 let cred = flags.name || 'default'
16 if (util.starterPlan(db) && cred !== 'default') {
17 throw new Error(`Only one default credential is supported for Hobby tier databases.`)
18 }
19 let credInfo = yield heroku.get(`/postgres/v0/databases/${db.name}/credentials/${encodeURIComponent(cred)}`,
20 { host: host(db) })
21
22 let activeCreds = credInfo.credentials.find((c) => c.state === 'active')
23 if (!activeCreds) {
24 cli.exit(1, `could not find any active credentials for ${cred}`)
25 }
26
27 let creds = Object.assign({}, db, {
28 database: credInfo.database,
29 host: credInfo.host,
30 port: credInfo.port
31 }, {
32 user: activeCreds.user,
33 password: activeCreds.password
34 })
35
36 let connUrl = url.format({
37 pathname: `/${creds.database}`,
38 host: `${creds.host}:${creds.port}`,
39 auth: `${creds.user}:${creds.password}`,
40 protocol: 'postgres:',
41 slashes: true
42 })
43 cli.log(`Connection information for ${cred} credential.
44Connection info string:
45 "dbname=${creds.database} host=${creds.host} port=${creds.port} user=${creds.user} password=${creds.password} sslmode=require"
46Connection URL:
47 ${connUrl}`)
48}
49
50module.exports = {
51 topic: 'pg',
52 command: 'credentials:url',
53 description: 'show information on a database credential',
54 needsApp: true,
55 needsAuth: true,
56 flags: [
57 {name: 'name', char: 'n', description: 'which credentials to show (default credentials if not specified)', hasValue: true}
58 ],
59 args: [{name: 'database', optional: true}],
60 run: cli.command({preauth: true}, co.wrap(run))
61}