Skip to content

Commit

Permalink
Merge pull request #106 from OldSneerJaw/oclif-upgrade
Browse files Browse the repository at this point in the history
Upgrade Heroku CLI command package
  • Loading branch information
OldSneerJaw authored Jan 26, 2023
2 parents 0a25330 + 14bdb8b commit 9a3a80b
Show file tree
Hide file tree
Showing 24 changed files with 232 additions and 170 deletions.
17 changes: 17 additions & 0 deletions bin/dev
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/usr/bin/env node

const oclif = require('@oclif/core')

const path = require('path')
const project = path.join(__dirname, '..', 'tsconfig.json')

// In dev mode -> use ts-node and dev plugins
process.env.NODE_ENV = 'development'

require('ts-node').register({project})

// In dev mode, always show stack traces
oclif.settings.debug = true;

// Start the CLI
oclif.run().then(oclif.flush).catch(oclif.Errors.handle)
3 changes: 3 additions & 0 deletions bin/dev.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@echo off

node "%~dp0\dev" %*
5 changes: 3 additions & 2 deletions bin/run
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env node

require('@oclif/command').run()
.catch(require('@oclif/errors/handle'))
const oclif = require('@oclif/core')

oclif.run().then(require('@oclif/core/flush')).catch(require('@oclif/core/handle'))
13 changes: 6 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,20 @@
"author": "Boreal Information Systems Inc.",
"bugs": "https://github.com/OldSneerJaw/borealis-pg-cli/issues",
"dependencies": {
"@heroku-cli/command": "^8.5.0",
"@oclif/command": "^1.8.21",
"@oclif/config": "^1.18.6",
"@heroku-cli/command": "^9.0.1",
"@oclif/core": "^1.26.1",
"chalk": "^2.4.2",
"cli-ux": "^5.6.7",
"dotenv": "^16.0.3",
"http-call": "^5.3.0",
"pg": "^8.7.3",
"pg": "^8.8.0",
"ssh2": "^1.11.0",
"tslib": "^2.4.1"
},
"devDependencies": {
"@heroku-cli/schema": "^1.0.25",
"@oclif/dev-cli": "^1.26.10",
"@oclif/plugin-help": "^5.2.1",
"@oclif/test": "^1.2.9",
"@oclif/test": "~2.2.21",
"@types/chai": "^4.3.4",
"@types/chai-as-promised": "^7.1.5",
"@types/chai-string": "^1.4.2",
Expand All @@ -34,7 +32,7 @@
"chai-string": "^1.5.0",
"eslint": "^7.32.0",
"eslint-config-oclif": "^4.0.0",
"eslint-config-oclif-typescript": "^1.0.2",
"eslint-config-oclif-typescript": "^1.0.3",
"globby": "^11.1.0",
"mocha": "^10.2.0",
"nock": "^13.3.0",
Expand All @@ -60,6 +58,7 @@
"oclif-plugin"
],
"license": "MIT",
"main": "lib/index.js",
"nyc": {
"all": true,
"reporter": [
Expand Down
8 changes: 4 additions & 4 deletions src/async-actions.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import color from '@heroku-cli/color'
import cli from 'cli-ux'
import {CliUx} from '@oclif/core'

/**
* Used to display a spinner while a given asynchronous action is executed
Expand All @@ -11,13 +11,13 @@ import cli from 'cli-ux'
*/
export async function applyActionSpinner<T>(message: string, action: Promise<T>): Promise<T> {
try {
cli.action.start(message)
CliUx.ux.action.start(message)
const result = await action
cli.action.stop()
CliUx.ux.action.stop()

return result
} catch (error: any) {
cli.action.stop(color.bold.redBright('!'))
CliUx.ux.action.stop(color.bold.redBright('!'))

throw error
}
Expand Down
13 changes: 2 additions & 11 deletions src/command-components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,8 @@ export const cliOptions = {
char: 'p',
default: defaultPorts.pg,
description: 'local port number for the secure tunnel to the add-on Postgres server',
parse: input => {
if (!/^-?\d+$/.test(input))
throw new Error(`Value "${input}" is not a valid integer`)

const value = Number.parseInt(input, 10)
if (value < 1 || value > 65_535) {
throw new Error(`Value ${value} is outside the range of valid port numbers`)
}

return value
},
min: 1,
max: 65_535,
}),
writeAccess: flags.boolean({
char: 'w',
Expand Down
2 changes: 1 addition & 1 deletion src/commands/borealis-pg/extensions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export default class ListPgExtensionsCommand extends Command {
}

async run() {
const {flags} = this.parse(ListPgExtensionsCommand)
const {flags} = await this.parse(ListPgExtensionsCommand)
const authorization = await createHerokuAuth(this.heroku)
const attachmentInfo =
await fetchAddonAttachmentInfo(this.heroku, flags.addon, flags.app, this.error)
Expand Down
4 changes: 2 additions & 2 deletions src/commands/borealis-pg/extensions/install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ static examples = [
}

async run() {
const {args, flags} = this.parse(InstallPgExtensionsCommand)
const {args, flags} = await this.parse(InstallPgExtensionsCommand)
const pgExtension = args[cliArgs.pgExtension.name]
const suppressConflict = flags[suppressConflictOptionName]
const authorization = await createHerokuAuth(this.heroku)
Expand Down Expand Up @@ -159,7 +159,7 @@ static examples = [
}

async catch(err: any) {
const {args} = this.parse(InstallPgExtensionsCommand)
const {args} = await this.parse(InstallPgExtensionsCommand)
const pgExtension = args[cliArgs.pgExtension.name]

/* istanbul ignore else */
Expand Down
8 changes: 4 additions & 4 deletions src/commands/borealis-pg/extensions/remove.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import color from '@heroku-cli/color'
import {Command, flags} from '@heroku-cli/command'
import cli from 'cli-ux'
import {CliUx} from '@oclif/core'
import {HTTP, HTTPError} from 'http-call'
import {applyActionSpinner} from '../../../async-actions'
import {getBorealisPgApiUrl, getBorealisPgAuthHeader} from '../../../borealis-api'
Expand Down Expand Up @@ -50,13 +50,13 @@ export default class RemovePgExtensionCommand extends Command {
}

async run() {
const {args, flags} = this.parse(RemovePgExtensionCommand)
const {args, flags} = await this.parse(RemovePgExtensionCommand)
const pgExtension = args[cliArgs.pgExtension.name]
const suppressMissing = flags[suppressMissingOptionName]

const confirmation = flags.confirm ?
flags.confirm :
(await cli.prompt('Enter the name of the extension to confirm its removal'))
(await CliUx.ux.prompt('Enter the name of the extension to confirm its removal'))

if (confirmation.trim() !== pgExtension) {
this.error(`Invalid confirmation provided. Expected ${pgExtensionColour(pgExtension)}.`)
Expand Down Expand Up @@ -90,7 +90,7 @@ export default class RemovePgExtensionCommand extends Command {
}

async catch(err: any) {
const {args} = this.parse(RemovePgExtensionCommand)
const {args} = await this.parse(RemovePgExtensionCommand)
const pgExtension = args[cliArgs.pgExtension.name]

/* istanbul ignore else */
Expand Down
2 changes: 1 addition & 1 deletion src/commands/borealis-pg/info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export default class AddonInfoCommand extends Command {
}

async run() {
const {flags} = this.parse(AddonInfoCommand)
const {flags} = await this.parse(AddonInfoCommand)
const authorization = await createHerokuAuth(this.heroku)
const attachmentInfo =
await fetchAddonAttachmentInfo(this.heroku, flags.addon, flags.app, this.error)
Expand Down
6 changes: 3 additions & 3 deletions src/commands/borealis-pg/integrations/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import color from '@heroku-cli/color'
import {Command} from '@heroku-cli/command'
import cli from 'cli-ux'
import {CliUx} from '@oclif/core'
import {HTTP, HTTPError} from 'http-call'
import {applyActionSpinner} from '../../../async-actions'
import {getBorealisPgApiUrl, getBorealisPgAuthHeader} from '../../../borealis-api'
Expand All @@ -24,7 +24,7 @@ via a secure tunnel using semi-permanent SSH server and database credentials.`
}

async run() {
const {flags} = this.parse(ListDataIntegrationsCommand)
const {flags} = await this.parse(ListDataIntegrationsCommand)
const authorization = await createHerokuAuth(this.heroku)
const attachmentInfo =
await fetchAddonAttachmentInfo(this.heroku, flags.addon, flags.app, this.error)
Expand Down Expand Up @@ -57,7 +57,7 @@ via a secure tunnel using semi-permanent SSH server and database credentials.`
})

this.log()
cli.table(normalizedIntegrations, columns, {'no-truncate': true})
CliUx.ux.table(normalizedIntegrations, columns, {'no-truncate': true})
} else {
this.warn('No data integrations found')
}
Expand Down
4 changes: 2 additions & 2 deletions src/commands/borealis-pg/integrations/register.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,15 +263,15 @@ describe('data integration registration command', () => {
'-n',
fakeIntegrationName,
])
.catch(/^Missing 1 required arg:/)
.catch(/.*Missing 1 required arg:.*/)
.it('exits with an error if there is no SSH public key argument', ctx => {
expect(ctx.stdout).to.equal('')
})

test.stdout()
.stderr()
.command(['borealis-pg:integrations:register', '-a', fakeHerokuAppName, fakeSshPublicKey])
.catch(/^Missing required flag:/)
.catch(/.*Missing required flag name.*/)
.it('exits with an error if there is no integration name option', ctx => {
expect(ctx.stdout).to.equal('')
})
Expand Down
2 changes: 1 addition & 1 deletion src/commands/borealis-pg/integrations/register.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ supports it.`
}

async run() {
const {args, flags} = this.parse(RegisterDataIntegrationsCommand)
const {args, flags} = await this.parse(RegisterDataIntegrationsCommand)
const sshPublicKey = args[sshPublicKeyCliArgName]
const integrationName = flags[dataIntegrationOptionName]
const enableWriteAccess = flags[writeAccessOptionName]
Expand Down
2 changes: 1 addition & 1 deletion src/commands/borealis-pg/integrations/remove.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ describe('data integration removal command', () => {
test.stdout()
.stderr()
.command(['borealis-pg:integrations:remove', '-a', fakeHerokuAppName])
.catch(/^Missing required flag:/)
.catch(/.*Missing required flag name.*/)
.it('exits with an error if the data integration option is missing', ctx => {
expect(ctx.stdout).to.equal('')
})
Expand Down
6 changes: 3 additions & 3 deletions src/commands/borealis-pg/integrations/remove.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import color from '@heroku-cli/color'
import {Command, flags} from '@heroku-cli/command'
import cli from 'cli-ux'
import {CliUx} from '@oclif/core'
import {HTTP, HTTPError} from 'http-call'
import {applyActionSpinner} from '../../../async-actions'
import {getBorealisPgApiUrl, getBorealisPgAuthHeader} from '../../../borealis-api'
Expand Down Expand Up @@ -43,11 +43,11 @@ export default class RemoveDataIntegrationCommand extends Command {
}

async run() {
const {flags} = this.parse(RemoveDataIntegrationCommand)
const {flags} = await this.parse(RemoveDataIntegrationCommand)
const integrationName = flags[dataIntegrationOptionName]
const confirmation = flags.confirm ?
flags.confirm :
(await cli.prompt('Enter the name of the data integration to confirm its removal'))
(await CliUx.ux.prompt('Enter the name of the data integration to confirm its removal'))

if (confirmation.trim() !== integrationName) {
this.error(
Expand Down
8 changes: 4 additions & 4 deletions src/commands/borealis-pg/psql.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -398,16 +398,16 @@ describe('interactive psql command', () => {
test.stdout()
.stderr()
.command(['borealis-pg:psql', '--app', fakeHerokuAppName, '--port', 'port-must-be-an-integer'])
.catch('Value "port-must-be-an-integer" is not a valid integer')
.catch(/.*Expected an integer but received: port-must-be-an-integer.*/)
.it('rejects a custom port that is not an integer', () => {
verify(mockTcpServerFactoryType.create(anyFunction())).never()
verify(mockSshClientFactoryType.create()).never()
})

test.stdout()
.stderr()
.command(['borealis-pg:psql', '-a', fakeHerokuAppName, '-p', '-1'])
.catch('Value -1 is outside the range of valid port numbers')
.command(['borealis-pg:psql', '-a', fakeHerokuAppName, '-p', '0'])
.catch(/.*Expected an integer greater than or equal to 1 but received: 0.*/)
.it('rejects a custom port that is less than 1', () => {
verify(mockTcpServerFactoryType.create(anyFunction())).never()
verify(mockSshClientFactoryType.create()).never()
Expand All @@ -416,7 +416,7 @@ describe('interactive psql command', () => {
test.stdout()
.stderr()
.command(['borealis-pg:psql', '-a', fakeHerokuAppName, '-p', '65536'])
.catch('Value 65536 is outside the range of valid port numbers')
.catch(/.*Expected an integer less than or equal to 65535 but received: 65536.*/)
.it('rejects a custom port that is greater than 65535', () => {
verify(mockTcpServerFactoryType.create(anyFunction())).never()
verify(mockSshClientFactoryType.create()).never()
Expand Down
2 changes: 1 addition & 1 deletion src/commands/borealis-pg/psql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ pgAdmin).`
}

async run() {
const {flags} = this.parse(PsqlCommand)
const {flags} = await this.parse(PsqlCommand)

const customBinaryPath = flags[binaryPathOptionName]
if (customBinaryPath && !fs.existsSync(customBinaryPath)) {
Expand Down
14 changes: 7 additions & 7 deletions src/commands/borealis-pg/run.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ describe('noninteractive run command', () => {
expect(ctx.stdout).to.contain(
' id value1 value2 \n' +
' ── ────── ────── \n' +
' 21 test1 null \n' +
' 21 test1 \n' +
' 33 test2 test3 \n')
expect(ctx.stdout).to.contain('(2 rows)')

Expand Down Expand Up @@ -641,7 +641,7 @@ describe('noninteractive run command', () => {
' ─── ──── \n' +
' 9 val1 \n' +
' 104 val2 \n' +
' 23 null \n' +
' 23 \n' +
' 1 one \n')
expect(ctx.stdout).to.contain('(4 rows)')

Expand Down Expand Up @@ -908,7 +908,7 @@ describe('noninteractive run command', () => {
'--shell-cmd',
fakeShellCommand,
])
.catch('Value "port-must-be-an-integer" is not a valid integer')
.catch(/.*Expected an integer but received: port-must-be-an-integer.*/)
.it('rejects a --port value that is not an integer', () => {
verify(mockTcpServerFactoryType.create(anyFunction())).never()
verify(mockSshClientFactoryType.create()).never()
Expand All @@ -918,7 +918,7 @@ describe('noninteractive run command', () => {
.stdout()
.stderr()
.command(['borealis-pg:run', '-a', fakeHerokuAppName, '-p', '-1', '-e', fakeShellCommand])
.catch('Value -1 is outside the range of valid port numbers')
.catch(/.*Expected an integer greater than or equal to 1 but received: -1.*/)
.it('rejects a --port value that is less than 1', () => {
verify(mockTcpServerFactoryType.create(anyFunction())).never()
verify(mockSshClientFactoryType.create()).never()
Expand All @@ -928,7 +928,7 @@ describe('noninteractive run command', () => {
.stdout()
.stderr()
.command(['borealis-pg:run', '-a', fakeHerokuAppName, '-p', '65536', '-e', fakeShellCommand])
.catch('Value 65536 is outside the range of valid port numbers')
.catch(/.*Expected an integer less than or equal to 65535 but received: 65536.*/)
.it('rejects a --port value that is greater than 65535', () => {
verify(mockTcpServerFactoryType.create(anyFunction())).never()
verify(mockSshClientFactoryType.create()).never()
Expand All @@ -948,7 +948,7 @@ describe('noninteractive run command', () => {
.stderr()
.command(
['borealis-pg:run', '-a', fakeHerokuAppName, '-e', fakeShellCommand, '-d', fakeDbCommand])
.catch('--shell-cmd= cannot also be provided when using --db-cmd=')
.catch(/.*--shell-cmd=my-cool-shell-command cannot also be provided when using --db-cmd.*/)
.it('exits with an error if both a shell command and a database command are provided', ctx => {
expect(ctx.stdout).to.equal('')
})
Expand All @@ -964,7 +964,7 @@ describe('noninteractive run command', () => {
'--format',
'yaml',
])
.catch('--shell-cmd= cannot also be provided when using --format=')
.catch(/.*--shell-cmd=my-cool-shell-command cannot also be provided when using --format.*/)
.it('exits with an error if the --format option is specified for a shell command', ctx => {
expect(ctx.stdout).to.equal('')
})
Expand Down
6 changes: 3 additions & 3 deletions src/commands/borealis-pg/run.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import color from '@heroku-cli/color'
import {Command, flags} from '@heroku-cli/command'
import {ConfigVars} from '@heroku-cli/schema'
import cli from 'cli-ux'
import {CliUx} from '@oclif/core'
import {readFileSync} from 'fs'
import {HTTP, HTTPError} from 'http-call'
import {QueryResult} from 'pg'
Expand Down Expand Up @@ -121,7 +121,7 @@ like pgAdmin).`
}

async run() {
const {flags} = this.parse(RunCommand)
const {flags} = await this.parse(RunCommand)
const shellCommand = flags[shellCommandOptionName]

if (
Expand Down Expand Up @@ -305,7 +305,7 @@ like pgAdmin).`
},
{})

cli.table(
CliUx.ux.table(
resultInstance.rows,
columns,
{'no-truncate': true, output: outputFormat})
Expand Down
Loading

0 comments on commit 9a3a80b

Please sign in to comment.