1 | 'use strict'
|
2 |
|
3 | const cli = require('heroku-cli-util')
|
4 | const co = require('co')
|
5 | const { flags } = require('@heroku-cli/command')
|
6 | const _ = require('lodash')
|
7 |
|
8 | function display (spaces) {
|
9 | cli.table(spaces, {
|
10 | columns: [
|
11 | { key: 'name', label: 'Name' },
|
12 | { key: 'team.name', label: 'Team' },
|
13 | { key: 'region.name', label: 'Region' },
|
14 | { key: 'state', label: 'State' },
|
15 | { key: 'created_at', label: 'Created At' }
|
16 | ]
|
17 | })
|
18 | }
|
19 |
|
20 | function displayJSON (spaces) {
|
21 | cli.log(JSON.stringify(spaces, null, 2))
|
22 | }
|
23 |
|
24 | function * run (context, heroku) {
|
25 | let team = context.org || context.team || context.flags.team
|
26 |
|
27 | let spaces = yield heroku.get('/spaces')
|
28 | if (team) {
|
29 | spaces = spaces.filter((s) => s.team.name === team)
|
30 | }
|
31 | spaces = _.sortBy(spaces, 'name')
|
32 | if (context.flags.json) displayJSON(spaces)
|
33 | else if (spaces.length === 0) {
|
34 | if (team) throw new Error(`No spaces in ${cli.color.cyan(team)}.`)
|
35 | else throw new Error('You do not have access to any spaces.')
|
36 | } else {
|
37 | display(spaces)
|
38 | }
|
39 | }
|
40 |
|
41 | module.exports = {
|
42 | topic: 'spaces',
|
43 | description: 'list available spaces',
|
44 | needsAuth: true,
|
45 | wantsOrg: true,
|
46 | flags: [
|
47 | { name: 'json', description: 'output in json format' },
|
48 | flags.team({ name: 'team', hasValue: true })
|
49 | ],
|
50 | run: cli.command(co.wrap(run))
|
51 | }
|