-
Notifications
You must be signed in to change notification settings - Fork 228
/
Copy pathindex.ts
60 lines (57 loc) · 2 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import color from '@heroku-cli/color'
import {Command, flags} from '@heroku-cli/command'
import {Args, ux} from '@oclif/core'
import {getAddon, all} from '../../../lib/pg/fetcher'
import pgHost from '../../../lib/pg/host'
import type {Link} from '../../../lib/pg/types'
import {nls} from '../../../nls'
export default class Index extends Command {
static topic = 'pg';
static description = 'lists all databases and information on link';
static flags = {
app: flags.app({required: true}),
remote: flags.remote(),
};
static args = {
database: Args.string({description: `${nls('pg:database:arg:description')} ${nls('pg:database:arg:description:default:suffix')}`}),
};
public async run(): Promise<void> {
const {flags, args} = await this.parse(Index)
const {app} = flags
const {database} = args
let dbs
if (database)
dbs = await Promise.all([getAddon(this.heroku, app, database)])
else
dbs = await all(this.heroku, app)
if (dbs.length === 0)
throw new Error(`No databases on ${color.app(app)}`)
dbs = await Promise.all(dbs.map(async db => {
const {body: links} = await this.heroku.get<Link[]>(`/client/v11/databases/${db.id}/links`, {hostname: pgHost()})
db.links = links
return db
}))
let once: boolean
dbs.forEach(db => {
if (once)
ux.log()
else
once = true
ux.styledHeader(color.yellow(db.name))
if (db.links.message)
return ux.log(db.links.message)
if (db.links.length === 0)
return ux.log('No data sources are linked into this database')
db.links.forEach((link: Link) => {
ux.log(` * ${color.cyan(link.name)}`)
const remoteAttachmentName = link.remote?.attachment_name || ''
const remoteName = link.remote?.name || ''
const remoteLinkText = `${color.green(remoteAttachmentName)} (${color.yellow(remoteName)})`
ux.styledObject({
created_at: link.created_at,
remote: remoteLinkText,
})
})
})
}
}