1 | 'use strict'
|
2 |
|
3 | const co = require('co')
|
4 | const cli = require('heroku-cli-util')
|
5 |
|
6 | function * run (context, heroku) {
|
7 | const host = require('../../lib/host')()
|
8 | const pgbackups = require('../../lib/pgbackups')(context, heroku)
|
9 | const sortBy = require('lodash.sortby')
|
10 |
|
11 | const {app, args} = context
|
12 |
|
13 | let num
|
14 |
|
15 | if (args.backup_id) {
|
16 | num = yield pgbackups.transfer.num(args.backup_id)
|
17 | if (!num) throw new Error(`Invalid Backup: ${args.backup_id}`)
|
18 | } else {
|
19 | let transfers = yield heroku.get(`/client/v11/apps/${app}/transfers`, {host})
|
20 | let lastBackup = sortBy(transfers.filter(t => t.succeeded && t.to_type === 'gof3r'), 'created_at').pop()
|
21 | if (!lastBackup) throw new Error(`No backups on ${cli.color.app(app)}. Capture one with ${cli.color.cmd('heroku pg:backups:capture')}`)
|
22 | num = lastBackup.num
|
23 | }
|
24 |
|
25 | let info = yield heroku.post(`/client/v11/apps/${app}/transfers/${num}/actions/public-url`, {host})
|
26 | cli.log(info.url)
|
27 | }
|
28 |
|
29 | let cmd = {
|
30 | topic: 'pg',
|
31 | description: 'get secret but publicly accessible URL of a backup',
|
32 | needsApp: true,
|
33 | needsAuth: true,
|
34 | args: [{name: 'backup_id', optional: true}],
|
35 | flags: [
|
36 |
|
37 | {name: 'quiet', char: 'q', hidden: true}
|
38 | ],
|
39 | run: cli.command({preauth: true}, co.wrap(run))
|
40 | }
|
41 |
|
42 | module.exports = [
|
43 | Object.assign({command: 'backups:url'}, cmd),
|
44 | Object.assign({command: 'backups:public-url', hidden: true}, cmd),
|
45 | Object.assign({command: 'backups:publicurl', hidden: true}, cmd)
|
46 | ]
|