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