From eba5d206ec62da70e7a31cf6d96a6b95604b95fd Mon Sep 17 00:00:00 2001 From: Charlie Gerard Date: Tue, 18 Jun 2024 11:48:45 -0700 Subject: [PATCH] update view command --- lib/commands/repos/list.js | 2 +- lib/commands/repos/view.js | 64 +++++++++++++++++++++++++++----------- 2 files changed, 46 insertions(+), 20 deletions(-) diff --git a/lib/commands/repos/list.js b/lib/commands/repos/list.js index c42aeb36..a9d058b0 100644 --- a/lib/commands/repos/list.js +++ b/lib/commands/repos/list.js @@ -137,7 +137,7 @@ function setupCommand (name, description, argv, importMeta) { async function listOrgRepos (orgSlug, input, spinner) { const socketSdk = await setupSdk(getDefaultKey()) // @ts-ignore - const result = await handleApiCall(socketSdk.getOrgRepoList(orgSlug, input), 'looking up package') + const result = await handleApiCall(socketSdk.getOrgRepoList(orgSlug, input), 'listing repositories') if (!result.success) { return handleUnsuccessfulApiResponse('getOrgRepoList', result, spinner) diff --git a/lib/commands/repos/view.js b/lib/commands/repos/view.js index 575844a4..338cc49f 100644 --- a/lib/commands/repos/view.js +++ b/lib/commands/repos/view.js @@ -1,14 +1,15 @@ -// @ts-nocheck /* eslint-disable no-console */ +import chalk from 'chalk' +// @ts-ignore +import chalkTable from 'chalk-table' import meow from 'meow' import ora from 'ora' import { outputFlags } from '../../flags/index.js' -// import { handleApiCall, handleUnsuccessfulApiResponse } from '../../utils/api-helpers.js' -import { InputError } from '../../utils/errors.js' +import { handleApiCall, handleUnsuccessfulApiResponse } from '../../utils/api-helpers.js' import { printFlagList } from '../../utils/formatting.js' -// import { getDefaultKey, setupSdk } from '../../utils/sdk.js' +import { getDefaultKey, setupSdk } from '../../utils/sdk.js' /** @type {import('../../utils/meow-with-subcommands.js').CliSubcommand} */ export const view = { @@ -20,7 +21,7 @@ export const view = { if (input) { const spinnerText = 'Fetching repository... \n' const spinner = ora(spinnerText).start() - await viewRepository(input.orgSlug, input, spinner) + await viewRepository(input.orgSlug, input.repositoryName, spinner) } } } @@ -32,6 +33,7 @@ export const view = { * @property {boolean} outputJson * @property {boolean} outputMarkdown * @property {string} orgSlug + * @property {string} repositoryName */ /** @@ -68,37 +70,61 @@ function setupCommand (name, description, argv, importMeta) { } = cli.flags if (!cli.input[0]) { - throw new InputError(`Please specify an organization slug. \n -Example: -socket scan list FakeOrg -`) + console.error(`${chalk.bgRed('Input error')}: Please provide an organization slug \n`) + cli.showHelp() + return } - const orgSlug = cli.input[0] || '' + const [orgSlug = '', repositoryName = ''] = cli.input return { outputJson, outputMarkdown, - orgSlug + orgSlug, + repositoryName } } /** * @typedef RepositoryData - * @property {import('@socketsecurity/sdk').SocketSdkReturnType<'getOrgFullScanList'>["data"]} data + * @property {import('@socketsecurity/sdk').SocketSdkReturnType<'getOrgRepo'>["data"]} data */ /** * @param {string} orgSlug - * @param {CommandContext} input + * @param {string} repoName * @param {import('ora').Ora} spinner * @returns {Promise} */ -async function viewRepository (orgSlug, input, spinner) { - // const socketSdk = await setupSdk(getDefaultKey()) - console.log(input) +async function viewRepository (orgSlug, repoName, spinner) { + const socketSdk = await setupSdk(getDefaultKey()) + // @ts-ignore + const result = await handleApiCall(socketSdk.getOrgRepo(orgSlug, repoName), 'fetching repository') -// return { -// // data: result.data -// } + if (!result.success) { + return handleUnsuccessfulApiResponse('getOrgRepo', result, spinner) + } + + spinner.stop() + + const options = { + columns: [ + { field: 'id', name: chalk.magenta('ID') }, + { field: 'name', name: chalk.magenta('Name') }, + { field: 'visibility', name: chalk.magenta('Visibility') }, + { field: 'default_branch', name: chalk.magenta('Default branch') }, + { field: 'homepage', name: chalk.magenta('Homepage') }, + { field: 'archived', name: chalk.magenta('Archived') }, + { field: 'created_at', name: chalk.magenta('Created at') } + ] + } + + const table = chalkTable(options, [result.data]) + + console.log(table, '\n') + + return { + // @ts-ignore + data: result.data + } }