1 | 'use strict'
|
2 |
|
3 | const cli = require('heroku-cli-util')
|
4 | const _ = require('lodash')
|
5 |
|
6 | let styles = {
|
7 | app: 'cyan',
|
8 | attachment: 'green',
|
9 | addon: 'magenta'
|
10 | }
|
11 |
|
12 | module.exports = {
|
13 |
|
14 | style: function style (s, t) {
|
15 | if (!t) return (text) => style(s, text)
|
16 | return cli.color[styles[s] || s](t)
|
17 | },
|
18 |
|
19 | table: function (data, options) {
|
20 | return cli.table(data, _.merge(options, {
|
21 | printLine: cli.log
|
22 | }))
|
23 | },
|
24 |
|
25 | grandfatheredPrice: function (addon) {
|
26 | const price = addon.plan.price
|
27 | return Object.assign({}, price, {
|
28 | cents: addon.billed_price.cents,
|
29 | contract: addon.billed_price.contract
|
30 | })
|
31 | },
|
32 |
|
33 | formatPrice: function (price) {
|
34 | const printf = require('printf')
|
35 |
|
36 | if (!price) return
|
37 | if (price.contract) return 'contract'
|
38 | if (price.cents === 0) return 'free'
|
39 |
|
40 | let fmt = price.cents % 100 === 0 ? '$%.0f/%s' : '$%.02f/%s'
|
41 | return printf(fmt, price.cents / 100, price.unit)
|
42 | },
|
43 |
|
44 | trapConfirmationRequired: function * (app, confirm, fn) {
|
45 | return yield fn(confirm)
|
46 | .catch((err) => {
|
47 | if (!err.body || err.body.id !== 'confirmation_required') throw err
|
48 | return cli.confirmApp(app, confirm, err.body.message)
|
49 | .then(() => fn(app))
|
50 | })
|
51 | },
|
52 |
|
53 | formatState: function (state) {
|
54 | switch (state) {
|
55 | case 'provisioned':
|
56 | state = 'created'
|
57 | break
|
58 | case 'provisioning':
|
59 | state = 'creating'
|
60 | break
|
61 | case 'deprovisioned':
|
62 | state = 'errored'
|
63 | break
|
64 | default:
|
65 | state = ''
|
66 | }
|
67 | return state
|
68 | }
|
69 | }
|