From 979520d68690f3efac7a140524cd9131ffcb479d Mon Sep 17 00:00:00 2001 From: "nabeel.hassan" Date: Thu, 8 Oct 2020 01:54:40 +0500 Subject: [PATCH 1/2] fix(errors): Update deprecated error handling #5 --- package.json | 3 ++- src/commands/secrets/set.ts | 19 ++++++++++--------- yarn.lock | 2 +- 3 files changed, 13 insertions(+), 11 deletions(-) diff --git a/package.json b/package.json index 403c4fa..792c2fb 100644 --- a/package.json +++ b/package.json @@ -10,6 +10,7 @@ "dependencies": { "@oclif/command": "^1", "@oclif/config": "^1", + "@oclif/errors": "^1", "@oclif/plugin-help": "^3", "@octokit/request": "^5.4.5", "fs-extra": "^9.0.1", @@ -74,4 +75,4 @@ "prepare": "rm -rf lib && tsc -b" }, "types": "lib/index.d.ts" -} +} \ No newline at end of file diff --git a/src/commands/secrets/set.ts b/src/commands/secrets/set.ts index 8fa87e4..a0dcb32 100644 --- a/src/commands/secrets/set.ts +++ b/src/commands/secrets/set.ts @@ -1,15 +1,16 @@ -import {Command, flags} from '@oclif/command' -import {request} from '@octokit/request' +import { Command, flags } from '@oclif/command' +import { CLIError } from '@oclif/errors' +import { request } from '@octokit/request' import cli from 'cli-ux' import fs from 'fs-extra' import sodium from 'tweetsodium' -import {configuration} from '../../utils/config' +import { configuration } from '../../utils/config' export default class SecretsSet extends Command { static description = 'Update/Create a secret' static flags = { - help: flags.help({char: 'h'}), + help: flags.help({ char: 'h' }), personalAccessToken: flags.string({ char: 't', description: 'Your GitHub Personal Access Token.', @@ -20,7 +21,7 @@ export default class SecretsSet extends Command { description: 'Organisation the repo belongs to.', required: false, }), - repo: flags.string({char: 'r', description: 'Name of the repo.', required: false}), + repo: flags.string({ char: 'r', description: 'Name of the repo.', required: false }), file: flags.string({ char: 'f', description: 'Location of a file to create a secret from', @@ -45,7 +46,7 @@ export default class SecretsSet extends Command { } async run() { - const {flags} = this.parse(SecretsSet) + const { flags } = this.parse(SecretsSet) try { const conf = await configuration(this) @@ -54,11 +55,11 @@ export default class SecretsSet extends Command { headers: { authorization: `token ${ flags.personalAccessToken ?? conf.personalAccessToken - }`, + }`, }, }) - const {data: token} = await requestWithAuth( + const { data: token } = await requestWithAuth( 'GET /repos/{owner}/{repo}/actions/secrets/public-key', { owner: flags.org ?? conf.org, @@ -111,7 +112,7 @@ export default class SecretsSet extends Command { this.log('unable to update secret') } } catch (error) { - this.error(error) + this.error(new CLIError(error)) this.exit(1) } } diff --git a/yarn.lock b/yarn.lock index 47348c6..4d3563c 100644 --- a/yarn.lock +++ b/yarn.lock @@ -181,7 +181,7 @@ qqjs "^0.3.10" tslib "^1.9.3" -"@oclif/errors@^1.2.1", "@oclif/errors@^1.2.2", "@oclif/errors@^1.3.3": +"@oclif/errors@^1", "@oclif/errors@^1.2.1", "@oclif/errors@^1.2.2", "@oclif/errors@^1.3.3": version "1.3.3" resolved "https://registry.yarnpkg.com/@oclif/errors/-/errors-1.3.3.tgz#fb597dfbc58c6b8609dc0b2fdf91a2d487818a82" integrity sha512-EJR6AIOEkt/NnARNIVAskPDVtdhtO5TTNXmhDrGqMoWVsr0R6DkkLrMyq95BmHvlVWM1nduoq4fQPuCyuF2jaA== From 9fac87683b125ce996fe84e4f30d07a482378711 Mon Sep 17 00:00:00 2001 From: "nabeel.hassan" Date: Sun, 11 Oct 2020 16:10:30 +0500 Subject: [PATCH 2/2] fix(error handling): handled error across all commands. --- src/commands/config/get.ts | 7 ++++--- src/commands/config/set.ts | 9 +++++---- src/commands/secrets/get.ts | 20 ++++++++++---------- src/utils/config/index.ts | 7 ++++--- 4 files changed, 23 insertions(+), 20 deletions(-) diff --git a/src/commands/config/get.ts b/src/commands/config/get.ts index 496f472..611915d 100644 --- a/src/commands/config/get.ts +++ b/src/commands/config/get.ts @@ -1,5 +1,6 @@ -import {Command} from '@oclif/command' -import {configuration} from '../../utils/config' +import { Command } from '@oclif/command' +import { CLIError } from '@oclif/errors' +import { configuration } from '../../utils/config' export default class ConfigGet extends Command { static description = 'Outputs your configuration.' @@ -8,7 +9,7 @@ export default class ConfigGet extends Command { try { this.log(JSON.stringify(await configuration(this))) } catch (error) { - this.error(error || 'A GHS CLI error has occurred.', { + this.error(new CLIError(error) || 'A GHS CLI error has occurred.', { exit: 1, }) } diff --git a/src/commands/config/set.ts b/src/commands/config/set.ts index 132181a..795a567 100644 --- a/src/commands/config/set.ts +++ b/src/commands/config/set.ts @@ -1,4 +1,5 @@ -import {Command, flags} from '@oclif/command' +import { Command, flags } from '@oclif/command' +import { CLIError } from '@oclif/errors' import cli from 'cli-ux' import fs from 'fs-extra' import emoji from 'node-emoji' @@ -26,7 +27,7 @@ export default class ConfigSet extends Command { } async run() { - const {flags} = this.parse(ConfigSet) + const { flags } = this.parse(ConfigSet) const config = path.join(this.config.configDir, 'config.json') try { @@ -37,7 +38,7 @@ export default class ConfigSet extends Command { flags.repo = await cli.prompt('What is the name of the repo?') flags.personalAccessToken = await cli.prompt( 'What is your GitHub Personal Access Token?', - {type: 'hide'} + { type: 'hide' } ) } @@ -54,7 +55,7 @@ export default class ConfigSet extends Command { ) this.exit() } catch (error) { - this.error(error || 'A GHS CLI error has occurred.', { + this.error(new CLIError(error) || 'A GHS CLI error has occurred.', { exit: 1, }) } diff --git a/src/commands/secrets/get.ts b/src/commands/secrets/get.ts index 9e18a38..a77ce05 100644 --- a/src/commands/secrets/get.ts +++ b/src/commands/secrets/get.ts @@ -1,12 +1,13 @@ -import {Command, flags} from '@oclif/command' -import {request} from '@octokit/request' -import {configuration} from '../../utils/config' +import { Command, flags } from '@oclif/command' +import { CLIError } from '@oclif/errors' +import { request } from '@octokit/request' +import { configuration } from '../../utils/config' export default class SecretsGet extends Command { static description = 'Fetch a list os set secrets (cannot read secret values)' static flags = { - help: flags.help({char: 'h'}), + help: flags.help({ char: 'h' }), personalAccessToken: flags.string({ char: 't', description: 'Your GitHub Personal Access Token.', @@ -17,20 +18,19 @@ export default class SecretsGet extends Command { description: 'Organisation the repo belongs to.', required: false, }), - repo: flags.string({char: 'r', description: 'Name of the repo.', required: false}), + repo: flags.string({ char: 'r', description: 'Name of the repo.', required: false }), } async run() { - const {flags} = this.parse(SecretsGet) + const { flags } = this.parse(SecretsGet) try { const conf = await configuration(this) const requestWithAuth = request.defaults({ headers: { - authorization: `token ${ - flags.personalAccessToken ?? conf.personalAccessToken - }`, + authorization: `token ${flags.personalAccessToken ?? conf.personalAccessToken + }`, }, }) const result = await requestWithAuth( @@ -43,7 +43,7 @@ export default class SecretsGet extends Command { this.log(result.data) } catch (error) { - this.error(error) + this.error(new CLIError(error)) this.exit(1) } } diff --git a/src/utils/config/index.ts b/src/utils/config/index.ts index 7811d97..0843085 100644 --- a/src/utils/config/index.ts +++ b/src/utils/config/index.ts @@ -1,7 +1,8 @@ import chalk from 'chalk' import path from 'path' import fs from 'fs-extra' -import Command from '@oclif/command' +import { Command } from '@oclif/command' +import { CLIError } from '@oclif/errors' interface Config { org: string @@ -21,7 +22,7 @@ export async function configuration(ctx: Command) { }) } - const {org, repo, personalAccessToken}: Config = await fs.readJson(config) + const { org, repo, personalAccessToken }: Config = await fs.readJson(config) if (!org || !repo || !personalAccessToken) { ctx.warn( @@ -39,7 +40,7 @@ export async function configuration(ctx: Command) { personalAccessToken, } } catch (error) { - ctx.error(error || 'A GHS CLI error has occurred.', { + ctx.error(new CLIError(error) || 'A GHS CLI error has occurred.', { exit: 1, }) }