diff --git a/src/commands/repos/cmd-repos-create.ts b/src/commands/repos/cmd-repos-create.ts index 38c89e34d..9755a033f 100644 --- a/src/commands/repos/cmd-repos-create.ts +++ b/src/commands/repos/cmd-repos-create.ts @@ -5,11 +5,9 @@ import { logger } from '@socketsecurity/registry/lib/logger' import { createRepo } from './create-repo' import constants from '../../constants' -import { commonFlags, outputFlags } from '../../flags' -import { AuthError } from '../../utils/errors' +import { commonFlags } from '../../flags' import { meowOrExit } from '../../utils/meow-with-subcommands' import { getFlagListOutput } from '../../utils/output-formatting' -import { getDefaultToken } from '../../utils/sdk' import type { CliCommandConfig } from '../../utils/meow-with-subcommands' @@ -21,7 +19,6 @@ const config: CliCommandConfig = { hidden: false, flags: { ...commonFlags, - ...outputFlags, repoName: { type: 'string', shortFlag: 'n', @@ -104,22 +101,12 @@ async function run( return } - const apiToken = getDefaultToken() - if (!apiToken) { - throw new AuthError( - 'User must be authenticated to run this command. To log in, run the command `socket login` and enter your API key.' - ) - } - await createRepo({ - outputJson: Boolean(cli.flags['json']), - outputMarkdown: Boolean(cli.flags['markdown']), orgSlug, repoName, description: String(cli.flags['repoDescription'] || ''), homepage: String(cli.flags['homepage'] || ''), default_branch: String(cli.flags['defaultBranch'] || ''), - visibility: String(cli.flags['visibility'] || 'private'), - apiToken + visibility: String(cli.flags['visibility'] || 'private') }) } diff --git a/src/commands/repos/cmd-repos-del.ts b/src/commands/repos/cmd-repos-del.ts index 3dda932af..289977e9c 100644 --- a/src/commands/repos/cmd-repos-del.ts +++ b/src/commands/repos/cmd-repos-del.ts @@ -6,10 +6,8 @@ import { logger } from '@socketsecurity/registry/lib/logger' import { deleteRepo } from './delete-repo' import constants from '../../constants' import { commonFlags } from '../../flags' -import { AuthError } from '../../utils/errors' import { meowOrExit } from '../../utils/meow-with-subcommands' import { getFlagListOutput } from '../../utils/output-formatting' -import { getDefaultToken } from '../../utils/sdk' import type { CliCommandConfig } from '../../utils/meow-with-subcommands' @@ -74,12 +72,5 @@ async function run( return } - const apiToken = getDefaultToken() - if (!apiToken) { - throw new AuthError( - 'User must be authenticated to run this command. To log in, run the command `socket login` and enter your API key.' - ) - } - - await deleteRepo(orgSlug, repoName, apiToken) + await deleteRepo(orgSlug, repoName) } diff --git a/src/commands/repos/cmd-repos-list.ts b/src/commands/repos/cmd-repos-list.ts index 233238c06..78a13bd06 100644 --- a/src/commands/repos/cmd-repos-list.ts +++ b/src/commands/repos/cmd-repos-list.ts @@ -6,10 +6,8 @@ import { logger } from '@socketsecurity/registry/lib/logger' import { listRepos } from './list-repos' import constants from '../../constants' import { commonFlags, outputFlags } from '../../flags' -import { AuthError } from '../../utils/errors' import { meowOrExit } from '../../utils/meow-with-subcommands' import { getFlagListOutput } from '../../utils/output-formatting' -import { getDefaultToken } from '../../utils/sdk' import type { CliCommandConfig } from '../../utils/meow-with-subcommands' @@ -96,21 +94,16 @@ async function run( return } - const apiToken = getDefaultToken() - if (!apiToken) { - throw new AuthError( - 'User must be authenticated to run this command. To log in, run the command `socket login` and enter your API key.' - ) - } - await listRepos({ - apiToken, - outputJson: Boolean(cli.flags['json']), - outputMarkdown: Boolean(cli.flags['markdown']), - orgSlug, - sort: String(cli.flags['sort'] || 'created_at'), direction: cli.flags['direction'] === 'asc' ? 'asc' : 'desc', + orgSlug, + outputKind: cli.flags['json'] + ? 'json' + : cli.flags['markdown'] + ? 'markdown' + : 'print', page: Number(cli.flags['page']) || 1, - per_page: Number(cli.flags['perPage']) || 30 + per_page: Number(cli.flags['perPage']) || 30, + sort: String(cli.flags['sort'] || 'created_at') }) } diff --git a/src/commands/repos/cmd-repos-update.ts b/src/commands/repos/cmd-repos-update.ts index c9d033627..943649fa8 100644 --- a/src/commands/repos/cmd-repos-update.ts +++ b/src/commands/repos/cmd-repos-update.ts @@ -5,11 +5,9 @@ import { logger } from '@socketsecurity/registry/lib/logger' import { updateRepo } from './update-repo' import constants from '../../constants' -import { commonFlags, outputFlags } from '../../flags' -import { AuthError } from '../../utils/errors' +import { commonFlags } from '../../flags' import { meowOrExit } from '../../utils/meow-with-subcommands' import { getFlagListOutput } from '../../utils/output-formatting' -import { getDefaultToken } from '../../utils/sdk' import type { CliCommandConfig } from '../../utils/meow-with-subcommands' @@ -21,7 +19,6 @@ const config: CliCommandConfig = { hidden: false, flags: { ...commonFlags, - ...outputFlags, repoName: { type: 'string', shortFlag: 'n', @@ -106,17 +103,7 @@ async function run( return } - const apiToken = getDefaultToken() - if (!apiToken) { - throw new AuthError( - 'User must be authenticated to run this command. To log in, run the command `socket login` and enter your API key.' - ) - } - await updateRepo({ - apiToken, - outputJson: Boolean(cli.flags['json']), - outputMarkdown: Boolean(cli.flags['markdown']), orgSlug, repoName, description: String(cli.flags['repoDescription'] || ''), diff --git a/src/commands/repos/cmd-repos-view.ts b/src/commands/repos/cmd-repos-view.ts index 821034e42..bc71b85b5 100644 --- a/src/commands/repos/cmd-repos-view.ts +++ b/src/commands/repos/cmd-repos-view.ts @@ -6,10 +6,8 @@ import { logger } from '@socketsecurity/registry/lib/logger' import { viewRepo } from './view-repo' import constants from '../../constants' import { commonFlags, outputFlags } from '../../flags' -import { AuthError } from '../../utils/errors' import { meowOrExit } from '../../utils/meow-with-subcommands' import { getFlagListOutput } from '../../utils/output-formatting' -import { getDefaultToken } from '../../utils/sdk' import type { CliCommandConfig } from '../../utils/meow-with-subcommands' @@ -21,7 +19,12 @@ const config: CliCommandConfig = { hidden: false, flags: { ...commonFlags, - ...outputFlags + ...outputFlags, + repoName: { + description: 'The repository to check', + default: '', + type: 'string' + } }, help: (command, config) => ` Usage @@ -86,12 +89,9 @@ async function run( return } - const apiToken = getDefaultToken() - if (!apiToken) { - throw new AuthError( - 'User must be authenticated to run this command. To log in, run the command `socket login` and enter your API key.' - ) - } - - await viewRepo(orgSlug, repoName, apiToken) + await viewRepo( + orgSlug, + repoName, + cli.flags['json'] ? 'json' : cli.flags['markdown'] ? 'markdown' : 'print' + ) } diff --git a/src/commands/repos/create-repo.ts b/src/commands/repos/create-repo.ts index afc003cf7..27aeaad27 100644 --- a/src/commands/repos/create-repo.ts +++ b/src/commands/repos/create-repo.ts @@ -1,21 +1,51 @@ import constants from '../../constants' import { handleApiCall, handleUnsuccessfulApiResponse } from '../../utils/api' -import { setupSdk } from '../../utils/sdk' +import { AuthError } from '../../utils/errors' +import { getDefaultToken, setupSdk } from '../../utils/sdk' export async function createRepo({ + default_branch, + description, + homepage, + orgSlug, + repoName, + visibility +}: { + orgSlug: string + repoName: string + description: string + homepage: string + default_branch: string + visibility: string +}): Promise { + const apiToken = getDefaultToken() + if (!apiToken) { + throw new AuthError( + 'User must be authenticated to run this command. To log in, run the command `socket login` and enter your API key.' + ) + } + + await createRepoWithToken({ + apiToken, + default_branch, + description, + homepage, + orgSlug, + repoName, + visibility + }) +} + +async function createRepoWithToken({ apiToken, default_branch, description, homepage, orgSlug, - outputJson, - outputMarkdown, repoName, visibility }: { apiToken: string - outputJson: boolean - outputMarkdown: boolean orgSlug: string repoName: string description: string @@ -31,9 +61,6 @@ export async function createRepo({ const socketSdk = await setupSdk(apiToken) const result = await handleApiCall( socketSdk.createOrgRepo(orgSlug, { - outputJson, - outputMarkdown, - orgSlug, name: repoName, description, homepage, @@ -43,9 +70,10 @@ export async function createRepo({ 'creating repository' ) - if (result.success) { - spinner.successAndStop('Repository created successfully') - } else { + if (!result.success) { handleUnsuccessfulApiResponse('createOrgRepo', result, spinner) + return } + + spinner.successAndStop('Repository created successfully') } diff --git a/src/commands/repos/delete-repo.ts b/src/commands/repos/delete-repo.ts index 38d60c809..d1ffc21b8 100644 --- a/src/commands/repos/delete-repo.ts +++ b/src/commands/repos/delete-repo.ts @@ -1,8 +1,23 @@ import constants from '../../constants' import { handleApiCall, handleUnsuccessfulApiResponse } from '../../utils/api' -import { setupSdk } from '../../utils/sdk' +import { AuthError } from '../../utils/errors' +import { getDefaultToken, setupSdk } from '../../utils/sdk' export async function deleteRepo( + orgSlug: string, + repoName: string +): Promise { + const apiToken = getDefaultToken() + if (!apiToken) { + throw new AuthError( + 'User must be authenticated to run this command. To log in, run the command `socket login` and enter your API key.' + ) + } + + await deleteRepoWithToken(orgSlug, repoName, apiToken) +} + +async function deleteRepoWithToken( orgSlug: string, repoName: string, apiToken: string @@ -18,9 +33,10 @@ export async function deleteRepo( 'deleting repository' ) - if (result.success) { - spinner.successAndStop('Repository deleted successfully') - } else { + if (!result.success) { handleUnsuccessfulApiResponse('deleteOrgRepo', result, spinner) + return } + + spinner.successAndStop('Repository deleted successfully') } diff --git a/src/commands/repos/list-repos.ts b/src/commands/repos/list-repos.ts index e756e4d20..e3f489b63 100644 --- a/src/commands/repos/list-repos.ts +++ b/src/commands/repos/list-repos.ts @@ -6,38 +6,67 @@ import { logger } from '@socketsecurity/registry/lib/logger' import constants from '../../constants' import { handleApiCall, handleUnsuccessfulApiResponse } from '../../utils/api' -import { setupSdk } from '../../utils/sdk' +import { AuthError } from '../../utils/errors' +import { getDefaultToken, setupSdk } from '../../utils/sdk' export async function listRepos({ - apiToken, direction, orgSlug, - outputJson, - outputMarkdown, + outputKind, page, per_page, sort }: { - outputJson: boolean - outputMarkdown: boolean + direction: string orgSlug: string + outputKind: 'json' | 'markdown' | 'print' + page: number + per_page: number sort: string +}): Promise { + const apiToken = getDefaultToken() + if (!apiToken) { + throw new AuthError( + 'User must be authenticated to run this command. To log in, run the command `socket login` and enter your API key.' + ) + } + + await listReposWithToken({ + apiToken, + direction, + orgSlug, + outputKind, + page, + per_page, + sort + }) +} + +async function listReposWithToken({ + apiToken, + direction, + orgSlug, + outputKind, + page, + per_page, + sort +}: { + apiToken: string direction: string - per_page: number + orgSlug: string + outputKind: 'json' | 'markdown' | 'print' page: number - apiToken: string + per_page: number + sort: string }): Promise { // Lazily access constants.spinner. const { spinner } = constants - spinner.start('Listing repositories...') + spinner.start('Fetching list of repositories...') const socketSdk = await setupSdk(apiToken) const result = await handleApiCall( socketSdk.getOrgRepoList(orgSlug, { - outputJson, - outputMarkdown, - orgSlug, sort, direction, per_page, @@ -51,9 +80,9 @@ export async function listRepos({ return } - spinner.stop() + spinner.stop('Fetch complete.') - if (outputJson) { + if (outputKind === 'json') { const data = result.data.results.map(o => ({ id: o.id, name: o.name, diff --git a/src/commands/repos/update-repo.ts b/src/commands/repos/update-repo.ts index 7298d262f..3e9dac9af 100644 --- a/src/commands/repos/update-repo.ts +++ b/src/commands/repos/update-repo.ts @@ -1,21 +1,51 @@ import constants from '../../constants' import { handleApiCall, handleUnsuccessfulApiResponse } from '../../utils/api' -import { setupSdk } from '../../utils/sdk' +import { AuthError } from '../../utils/errors' +import { getDefaultToken, setupSdk } from '../../utils/sdk' export async function updateRepo({ + default_branch, + description, + homepage, + orgSlug, + repoName, + visibility +}: { + orgSlug: string + repoName: string + description: string + homepage: string + default_branch: string + visibility: string +}): Promise { + const apiToken = getDefaultToken() + if (!apiToken) { + throw new AuthError( + 'User must be authenticated to run this command. To log in, run the command `socket login` and enter your API key.' + ) + } + + await updateRepoWithToken({ + apiToken, + default_branch, + description, + homepage, + orgSlug, + repoName, + visibility + }) +} + +async function updateRepoWithToken({ apiToken, default_branch, description, homepage, orgSlug, - outputJson, - outputMarkdown, repoName, visibility }: { apiToken: string - outputJson: boolean - outputMarkdown: boolean orgSlug: string repoName: string description: string @@ -31,8 +61,6 @@ export async function updateRepo({ const socketSdk = await setupSdk(apiToken) const result = await handleApiCall( socketSdk.updateOrgRepo(orgSlug, repoName, { - outputJson, - outputMarkdown, orgSlug, name: repoName, description, @@ -43,9 +71,10 @@ export async function updateRepo({ 'updating repository' ) - if (result.success) { - spinner.successAndStop('Repository updated successfully') - } else { + if (!result.success) { handleUnsuccessfulApiResponse('updateOrgRepo', result, spinner) + return } + + spinner.successAndStop('Repository updated successfully') } diff --git a/src/commands/repos/view-repo.ts b/src/commands/repos/view-repo.ts index a6be8d320..75cbb57f7 100644 --- a/src/commands/repos/view-repo.ts +++ b/src/commands/repos/view-repo.ts @@ -2,19 +2,37 @@ import chalkTable from 'chalk-table' import colors from 'yoctocolors-cjs' +import { logger } from '@socketsecurity/registry/lib/logger' + import constants from '../../constants' import { handleApiCall, handleUnsuccessfulApiResponse } from '../../utils/api' -import { setupSdk } from '../../utils/sdk' +import { AuthError } from '../../utils/errors' +import { getDefaultToken, setupSdk } from '../../utils/sdk' export async function viewRepo( orgSlug: string, repoName: string, - apiToken: string + outputKind: 'json' | 'markdown' | 'print' +): Promise { + const apiToken = getDefaultToken() + if (!apiToken) { + throw new AuthError( + 'User must be authenticated to run this command. To log in, run the command `socket login` and enter your API key.' + ) + } + await viewRepoWithToken(orgSlug, repoName, apiToken, outputKind) +} + +async function viewRepoWithToken( + orgSlug: string, + repoName: string, + apiToken: string, + outputKind: 'json' | 'markdown' | 'print' ): Promise { // Lazily access constants.spinner. const { spinner } = constants - spinner.start('Fetching repository...') + spinner.start('Fetching repository data...') const socketSdk = await setupSdk(apiToken) const result = await handleApiCall( @@ -27,6 +45,36 @@ export async function viewRepo( return } + spinner.stop('Fetched repository data.') + + if (outputKind === 'json') { + const { + archived, + created_at, + default_branch, + homepage, + id, + name, + visibility + } = result.data + logger.log( + JSON.stringify( + { + id, + name, + visibility, + default_branch, + homepage, + archived, + created_at + }, + null, + 2 + ) + ) + return + } + const options = { columns: [ { field: 'id', name: colors.magenta('ID') }, @@ -39,5 +87,5 @@ export async function viewRepo( ] } - spinner.stop(chalkTable(options, [result.data])) + logger.log(chalkTable(options, [result.data])) }