-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.ts
71 lines (65 loc) · 2.54 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
61
62
63
64
65
66
67
68
69
70
71
import {HTTP, HTTPError} from '@heroku/http-call'
import color from '@heroku-cli/color'
import {Command} from '@heroku-cli/command'
import {applyActionSpinner} from '../../../async-actions'
import {getBorealisPgApiUrl, getBorealisPgAuthHeader} from '../../../borealis-api'
import {
addonOptionName,
appOptionName,
cliOptions,
consoleColours,
processAddonAttachmentInfo,
} from '../../../command-components'
import {createHerokuAuth, fetchAddonAttachmentInfo, removeHerokuAuth} from '../../../heroku-api'
const pgExtensionColour = consoleColours.pgExtension
const pgExtMetadataColour = consoleColours.dataFieldValue
export default class ListPgExtensionsCommand extends Command {
static description = 'lists installed Postgres extensions for a Borealis Isolated Postgres add-on'
static flags = {
[addonOptionName]: cliOptions.addon,
[appOptionName]: cliOptions.app,
}
async run() {
const {flags} = await this.parse(ListPgExtensionsCommand)
const authorization = await createHerokuAuth(this.heroku)
const attachmentInfo =
await fetchAddonAttachmentInfo(this.heroku, flags.addon, flags.app, this.error)
const {addonName} = processAddonAttachmentInfo(attachmentInfo, this.error)
try {
const response = await applyActionSpinner(
`Fetching Postgres extension list for add-on ${color.addon(addonName)}`,
HTTP.get(
getBorealisPgApiUrl(`/heroku/resources/${addonName}/pg-extensions`),
{headers: {Authorization: getBorealisPgAuthHeader(authorization)}}),
)
const responseBody = response.body as
{extensions: Array<{name: string, schema: string, version: string}>}
if (responseBody.extensions.length > 0) {
for (const extInfo of responseBody.extensions) {
this.log(
`- ${pgExtensionColour(extInfo.name)} ` +
`(version: ${pgExtMetadataColour(extInfo.version)}, ` +
`schema: ${pgExtMetadataColour(extInfo.schema)})`)
}
} else {
this.warn('No extensions found')
}
} finally {
await removeHerokuAuth(this.heroku, authorization.id as string)
}
}
async catch(err: any) {
/* istanbul ignore else */
if (err instanceof HTTPError) {
if (err.statusCode === 404) {
this.error('Add-on is not a Borealis Isolated Postgres add-on')
} else if (err.statusCode === 422) {
this.error('Add-on is not finished provisioning')
} else {
this.error('Add-on service is temporarily unavailable. Try again later.')
}
} else {
throw err
}
}
}