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 | const addons = require('@heroku-cli/plugin-addons').resolve
|
9 | let { app, args, flags } = context
|
10 |
|
11 | let service = async function (name) {
|
12 | let addon = await addons.addon(heroku, app, name)
|
13 | if (!addon.plan.name.match(/^heroku-(redis|postgresql)/)) throw new Error('Remote database must be heroku-redis or heroku-postgresql')
|
14 | return addon
|
15 | }
|
16 |
|
17 | const [db, target] = await Promise.all([
|
18 | fetcher.addon(app, args.database),
|
19 | service(args.remote)
|
20 | ])
|
21 |
|
22 | await cli.action(`Adding link from ${cli.color.addon(target.name)} to ${cli.color.addon(db.name)}`, async function () {
|
23 | let link = await heroku.post(`/client/v11/databases/${db.id}/links`, {
|
24 | body: {
|
25 | target: target.name,
|
26 | as: flags.as
|
27 | },
|
28 | host: host(db)
|
29 | })
|
30 | if (link.message) throw new Error(link.message)
|
31 | cli.action.done(`done, ${cli.color.cyan(link.name)}`)
|
32 | }())
|
33 | }
|
34 |
|
35 | module.exports = {
|
36 | topic: 'pg',
|
37 | command: 'links:create',
|
38 | description: 'create a link between data stores',
|
39 | help: `Example:
|
40 |
|
41 | heroku pg:links:create HEROKU_REDIS_RED HEROKU_POSTGRESQL_CERULEAN`,
|
42 | needsApp: true,
|
43 | needsAuth: true,
|
44 | args: [{ name: 'remote' }, { name: 'database' }],
|
45 | flags: [{ name: 'as', hasValue: true, description: 'name of link to create' }],
|
46 | run: cli.command({ preauth: true }, run)
|
47 | }
|