-
Notifications
You must be signed in to change notification settings - Fork 228
/
Copy pathurl.ts
72 lines (66 loc) · 2.51 KB
/
url.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
61
62
63
64
65
66
67
68
69
70
71
72
import color from '@heroku-cli/color'
import {Command, flags} from '@heroku-cli/command'
import {Args, ux} from '@oclif/core'
import {legacyEssentialPlan} from '../../../lib/pg/util'
import {getAddon} from '../../../lib/pg/fetcher'
import pgHost from '../../../lib/pg/host'
import {URL} from 'url'
import type {CredentialInfo} from '../../../lib/pg/types'
import heredoc from 'tsheredoc'
import {nls} from '../../../nls'
export default class Url extends Command {
static topic = 'pg'
static description = 'show information on a database credential'
static flags = {
name: flags.string({
char: 'n',
description: 'which credential to show (default credentials if not specified)',
default: 'default',
}),
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(Url)
const {app, name} = flags
const {database} = args
const db = await getAddon(this.heroku, app, database)
if (legacyEssentialPlan(db) && name !== 'default') {
ux.error('Legacy Essential-tier databases do not support named credentials.')
}
const {body: credInfo} = await this.heroku.get<CredentialInfo>(
`/postgres/v0/databases/${db.name}/credentials/${encodeURIComponent(name)}`,
{
hostname: pgHost(),
headers: {
Authorization: `Basic ${Buffer.from(`:${this.heroku.auth}`).toString('base64')}`,
},
},
)
const activeCreds = credInfo.credentials.find(c => c.state === 'active')
if (!activeCreds) {
ux.error(`Could not find any active credentials for ${name}`, {exit: 1})
}
const creds = Object.assign({}, db, {
database: credInfo.database, host: credInfo.host, port: credInfo.port,
}, {
user: activeCreds?.user, password: activeCreds?.password,
})
const connUrl = new URL(`postgres://${creds.host}/${creds.database}`)
connUrl.port = creds.port.toString()
if (creds.user && creds.password) {
connUrl.username = creds.user
connUrl.password = creds.password
}
ux.log(heredoc(`
Connection information for ${color.yellow(name)} credential.
Connection info string:
"dbname=${creds.database} host=${creds.host} port=${creds.port} user=${creds.user} password=${creds.password} sslmode=require"
Connection URL:
${connUrl}
`))
}
}