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 host = require('../../lib/host')
|
9 | const util = require('../../lib/util')
|
10 |
|
11 | const {app, args, flags} = context
|
12 |
|
13 | let db = yield fetcher.addon(app, args.database)
|
14 | if (util.starterPlan(db)) throw new Error('This operation is not supported by Hobby tier databases.')
|
15 |
|
16 | let data = {
|
17 | name: flags.name
|
18 | }
|
19 | yield cli.action(`Creating credential ${cli.color.cmd(flags.name)}`, co(function * () {
|
20 | yield heroku.post(`/postgres/v0/databases/${db.name}/credentials`, {host: host(db), body: data})
|
21 | }))
|
22 | let attachCmd = `heroku addons:attach ${db.name} --credential ${flags.name} -a app_name`
|
23 | let psqlCmd = `heroku pg:psql ${db.name} -a ${app}`
|
24 | cli.log(`
|
25 | Please attach the credential to the apps you want to use it in by running ${cli.color.cmd(attachCmd)}.
|
26 | Please define the new grants for the credential within Postgres: ${cli.color.cmd(psqlCmd)}.`)
|
27 | }
|
28 |
|
29 | module.exports = {
|
30 | topic: 'pg',
|
31 | command: 'credentials:create',
|
32 | description: 'create credential within database',
|
33 | needsApp: true,
|
34 | needsAuth: true,
|
35 | help: `
|
36 | Example Usage:
|
37 | heroku pg:credentials:create postgresql-something-12345 --name new-cred-name
|
38 | `,
|
39 | args: [{name: 'database', optional: true}],
|
40 | flags: [{name: 'name', char: 'n', hasValue: true, required: true, description: 'name of the new credential within the database'}],
|
41 | run: cli.command({preauth: true}, co.wrap(run))
|
42 | }
|