1 | 'use strict'
|
2 |
|
3 | const cli = require('heroku-cli-util')
|
4 | const co = require('co')
|
5 |
|
6 | function * run (context, heroku) {
|
7 | const resolve = require('../../lib/resolve')
|
8 | const groupBy = require('lodash.groupby')
|
9 | const toPairs = require('lodash.topairs')
|
10 |
|
11 | let force = context.flags.force || process.env.HEROKU_FORCE === '1'
|
12 | if (context.args.length === 0) throw new Error('Missing add-on name')
|
13 |
|
14 | let addons = yield context.args.map(name => resolve.addon(heroku, context.app, name))
|
15 | for (let addon of addons) {
|
16 |
|
17 | let app = addon.app.name
|
18 | if (context.app && app !== context.app) throw new Error(`${cli.color.addon(addon.name)} is on ${cli.color.app(app)} not ${cli.color.app(context.app)}`)
|
19 | }
|
20 | for (let app of toPairs(groupBy(addons, 'app.name'))) {
|
21 | addons = app[1]
|
22 | app = app[0]
|
23 | yield cli.confirmApp(app, context.flags.confirm)
|
24 | for (let addon of addons) {
|
25 | let msg = `Destroying ${cli.color.addon(addon.name)} on ${cli.color.app(addon.app.name)}`
|
26 | yield cli.action(msg, heroku.request({
|
27 | method: 'DELETE',
|
28 | path: `/apps/${addon.app.id}/addons/${addon.id}`,
|
29 | headers: {'Accept-Expansion': 'plan'},
|
30 | body: {force}
|
31 | }))
|
32 | }
|
33 | }
|
34 | }
|
35 |
|
36 | let cmd = {
|
37 | topic: 'addons',
|
38 | description: 'destroy add-on resources',
|
39 | needsAuth: true,
|
40 | wantsApp: true,
|
41 | flags: [
|
42 | {name: 'force', char: 'f', description: 'allow destruction even if connected to other apps'},
|
43 | {name: 'confirm', char: 'c', hasValue: true}
|
44 | ],
|
45 | variableArgs: true,
|
46 | run: cli.command({preauth: true}, co.wrap(run))
|
47 | }
|
48 |
|
49 | module.exports = [
|
50 | Object.assign({command: 'destroy'}, cmd),
|
51 | Object.assign({command: 'remove', hidden: true}, cmd)
|
52 | ]
|