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