diff --git a/src/cli.ts b/src/cli.ts index a64409ebc..a9f150cef 100755 --- a/src/cli.ts +++ b/src/cli.ts @@ -5,7 +5,6 @@ import { pathToFileURL } from 'node:url' import { messageWithCauses, stackWithCauses } from 'pony-cause' import updateNotifier from 'tiny-updater' -import colors from 'yoctocolors-cjs' import { logger } from '@socketsecurity/registry/lib/logger' @@ -35,6 +34,7 @@ import { cmdThreatFeed } from './commands/threat-feed/cmd-threat-feed' import { cmdWrapper } from './commands/wrapper/cmd-wrapper' import constants from './constants' import { AuthError, InputError, captureException } from './utils/errors' +import { failMsgWithBadge } from './utils/fail-msg-with-badge' import { meowWithSubcommands } from './utils/meow-with-subcommands' const { SOCKET_CLI_BIN_NAME } = constants @@ -107,9 +107,7 @@ void (async () => { } else { errorTitle = 'Unexpected error with no details' } - logger.fail( - `${colors.bgRed(colors.white(`${errorTitle}:`))} ${errorMessage}` - ) + logger.fail(failMsgWithBadge(errorTitle, errorMessage)) if (errorBody) { logger.error(`\n${errorBody}`) } diff --git a/src/commands/analytics/cmd-analytics.test.ts b/src/commands/analytics/cmd-analytics.test.ts index d484e1c4f..c5f0c20db 100644 --- a/src/commands/analytics/cmd-analytics.test.ts +++ b/src/commands/analytics/cmd-analytics.test.ts @@ -70,7 +70,7 @@ describe('socket analytics', async () => { |__ | . | _| '_| -_| _| | Node: , API token set: |_____|___|___|_,_|___|_|.dev | Command: \`socket analytics\`, cwd: - \\x1b[31m\\xd7\\x1b[39m \\x1b[41m\\x1b[1m\\x1b[37m Input error: \\x1b[39m\\x1b[22m\\x1b[49m \\x1b[1mPlease review the input requirements and try again\\x1b[22m: + \\x1b[31m\\xd7\\x1b[39m \\x1b[41m\\x1b[1m\\x1b[37m Input error: \\x1b[39m\\x1b[22m\\x1b[49m \\x1b[1mPlease review the input requirements and try again\\x1b[22m - Scope must be "repo" or "org" (\\x1b[32mok\\x1b[39m) diff --git a/src/commands/analytics/display-analytics.ts b/src/commands/analytics/display-analytics.ts index e17581aaa..9c83d3b05 100644 --- a/src/commands/analytics/display-analytics.ts +++ b/src/commands/analytics/display-analytics.ts @@ -10,9 +10,7 @@ import { logger } from '@socketsecurity/registry/lib/logger' import { fetchOrgAnalyticsData } from './fetch-org-analytics' import { fetchRepoAnalyticsData } from './fetch-repo-analytics' import constants from '../../constants' -import { AuthError } from '../../utils/errors' import { mdTableStringNumber } from '../../utils/markdown' -import { getDefaultToken } from '../../utils/sdk' import type { SocketSdkReturnType } from '@socketsecurity/sdk' import type { Widgets } from 'blessed' // Note: Widgets does not seem to actually work as code :'( @@ -76,38 +74,6 @@ export async function displayAnalytics({ repo: string outputKind: 'json' | 'markdown' | 'print' filePath: 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 token.' - ) - } - - await outputAnalyticsWithToken({ - apiToken, - filePath, - outputKind, - repo, - scope, - time - }) -} - -async function outputAnalyticsWithToken({ - apiToken, - filePath, - outputKind, - repo, - scope, - time -}: { - apiToken: string - scope: string - time: number - repo: string - outputKind: 'json' | 'markdown' | 'print' - filePath: string }): Promise { // Lazily access constants.spinner. const { spinner } = constants @@ -119,9 +85,9 @@ async function outputAnalyticsWithToken({ | SocketSdkReturnType<'getOrgAnalytics'>['data'] | SocketSdkReturnType<'getRepoAnalytics'>['data'] if (scope === 'org') { - data = await fetchOrgAnalyticsData(time, spinner, apiToken) + data = await fetchOrgAnalyticsData(time, spinner) } else if (repo) { - data = await fetchRepoAnalyticsData(repo, time, spinner, apiToken) + data = await fetchRepoAnalyticsData(repo, time, spinner) } // A message should already have been printed if we have no data here diff --git a/src/commands/analytics/fetch-org-analytics.ts b/src/commands/analytics/fetch-org-analytics.ts index 6724d96f0..9b012958a 100644 --- a/src/commands/analytics/fetch-org-analytics.ts +++ b/src/commands/analytics/fetch-org-analytics.ts @@ -8,10 +8,9 @@ import type { SocketSdkReturnType } from '@socketsecurity/sdk' export async function fetchOrgAnalyticsData( time: number, - spinner: Spinner, - apiToken: string + spinner: Spinner ): Promise['data'] | undefined> { - const sockSdk = await setupSdk(apiToken) + const sockSdk = await setupSdk() const result = await handleApiCall( sockSdk.getOrgAnalytics(time.toString()), 'fetching analytics data' diff --git a/src/commands/analytics/fetch-repo-analytics.ts b/src/commands/analytics/fetch-repo-analytics.ts index e2b4a168c..6482befc3 100644 --- a/src/commands/analytics/fetch-repo-analytics.ts +++ b/src/commands/analytics/fetch-repo-analytics.ts @@ -9,10 +9,9 @@ import type { SocketSdkReturnType } from '@socketsecurity/sdk' export async function fetchRepoAnalyticsData( repo: string, time: number, - spinner: Spinner, - apiToken: string + spinner: Spinner ): Promise['data'] | undefined> { - const sockSdk = await setupSdk(apiToken) + const sockSdk = await setupSdk() const result = await handleApiCall( sockSdk.getRepoAnalytics(repo, time.toString()), 'fetching analytics data' diff --git a/src/commands/audit-log/cmd-audit-log.test.ts b/src/commands/audit-log/cmd-audit-log.test.ts index 2db830b70..ec4d05a82 100644 --- a/src/commands/audit-log/cmd-audit-log.test.ts +++ b/src/commands/audit-log/cmd-audit-log.test.ts @@ -67,7 +67,7 @@ describe('socket audit-log', async () => { |__ | . | _| '_| -_| _| | Node: , API token set: |_____|___|___|_,_|___|_|.dev | Command: \`socket audit-log\`, cwd: - \\x1b[31m\\xd7\\x1b[39m \\x1b[41m\\x1b[1m\\x1b[37m Input error: \\x1b[39m\\x1b[22m\\x1b[49m \\x1b[1mPlease review the input requirements and try again\\x1b[22m: + \\x1b[31m\\xd7\\x1b[39m \\x1b[41m\\x1b[1m\\x1b[37m Input error: \\x1b[39m\\x1b[22m\\x1b[49m \\x1b[1mPlease review the input requirements and try again\\x1b[22m - Org name should be the first arg (\\x1b[31mmissing\\x1b[39m) diff --git a/src/commands/cdxgen/cmd-cdxgen.ts b/src/commands/cdxgen/cmd-cdxgen.ts index 97eaa6ef3..34ceb0dd8 100644 --- a/src/commands/cdxgen/cmd-cdxgen.ts +++ b/src/commands/cdxgen/cmd-cdxgen.ts @@ -147,16 +147,6 @@ async function run( importMeta, parentName }) - // if (cli.input.length) - // logger.fail( - // stripIndents` - // ${colors.bgRed(colors.white('Input error'))}: Please provide the required fields: - // - // - Unexpected arguments - // `) - // config.help(parentName, config) - // return - // } // TODO: Convert to meow. const yargv = { diff --git a/src/commands/config/cmd-config-get.test.ts b/src/commands/config/cmd-config-get.test.ts index 07ee896bf..f5e41adaf 100644 --- a/src/commands/config/cmd-config-get.test.ts +++ b/src/commands/config/cmd-config-get.test.ts @@ -69,7 +69,7 @@ describe('socket config get', async () => { |__ | . | _| '_| -_| _| | Node: , API token set: |_____|___|___|_,_|___|_|.dev | Command: \`socket config get\`, cwd: - \\x1b[31m\\xd7\\x1b[39m \\x1b[41m\\x1b[1m\\x1b[37m Input error: \\x1b[39m\\x1b[22m\\x1b[49m \\x1b[1mPlease review the input requirements and try again\\x1b[22m: + \\x1b[31m\\xd7\\x1b[39m \\x1b[41m\\x1b[1m\\x1b[37m Input error: \\x1b[39m\\x1b[22m\\x1b[49m \\x1b[1mPlease review the input requirements and try again\\x1b[22m - Config key should be the first arg (\\x1b[31mmissing\\x1b[39m)" `) diff --git a/src/commands/config/cmd-config-set.test.ts b/src/commands/config/cmd-config-set.test.ts index cc4f4a534..c247f797a 100644 --- a/src/commands/config/cmd-config-set.test.ts +++ b/src/commands/config/cmd-config-set.test.ts @@ -74,7 +74,7 @@ describe('socket config get', async () => { |__ | . | _| '_| -_| _| | Node: , API token set: |_____|___|___|_,_|___|_|.dev | Command: \`socket config set\`, cwd: - \\x1b[31m\\xd7\\x1b[39m \\x1b[41m\\x1b[1m\\x1b[37m Input error: \\x1b[39m\\x1b[22m\\x1b[49m \\x1b[1mPlease review the input requirements and try again\\x1b[22m: + \\x1b[31m\\xd7\\x1b[39m \\x1b[41m\\x1b[1m\\x1b[37m Input error: \\x1b[39m\\x1b[22m\\x1b[49m \\x1b[1mPlease review the input requirements and try again\\x1b[22m - Config key should be the first arg (\\x1b[31mmissing\\x1b[39m) diff --git a/src/commands/config/cmd-config-unset.test.ts b/src/commands/config/cmd-config-unset.test.ts index bdf21c250..d9f72c4d2 100644 --- a/src/commands/config/cmd-config-unset.test.ts +++ b/src/commands/config/cmd-config-unset.test.ts @@ -69,7 +69,7 @@ describe('socket config unset', async () => { |__ | . | _| '_| -_| _| | Node: , API token set: |_____|___|___|_,_|___|_|.dev | Command: \`socket config unset\`, cwd: - \\x1b[31m\\xd7\\x1b[39m \\x1b[41m\\x1b[1m\\x1b[37m Input error: \\x1b[39m\\x1b[22m\\x1b[49m \\x1b[1mPlease review the input requirements and try again\\x1b[22m: + \\x1b[31m\\xd7\\x1b[39m \\x1b[41m\\x1b[1m\\x1b[37m Input error: \\x1b[39m\\x1b[22m\\x1b[49m \\x1b[1mPlease review the input requirements and try again\\x1b[22m - Config key should be the first arg (\\x1b[31mmissing\\x1b[39m)" `) diff --git a/src/commands/diff-scan/cmd-diff-scan-get.test.ts b/src/commands/diff-scan/cmd-diff-scan-get.test.ts index c8704ca3f..b7e8b8e45 100644 --- a/src/commands/diff-scan/cmd-diff-scan-get.test.ts +++ b/src/commands/diff-scan/cmd-diff-scan-get.test.ts @@ -68,7 +68,7 @@ describe('socket diff-scan get', async () => { |__ | . | _| '_| -_| _| | Node: , API token set: |_____|___|___|_,_|___|_|.dev | Command: \`socket diff-scan get\`, cwd: - \\x1b[31m\\xd7\\x1b[39m \\x1b[41m\\x1b[1m\\x1b[37m Input error: \\x1b[39m\\x1b[22m\\x1b[49m \\x1b[1mPlease review the input requirements and try again\\x1b[22m: + \\x1b[31m\\xd7\\x1b[39m \\x1b[41m\\x1b[1m\\x1b[37m Input error: \\x1b[39m\\x1b[22m\\x1b[49m \\x1b[1mPlease review the input requirements and try again\\x1b[22m - Specify a before and after scan ID (\\x1b[31mmissing before and after\\x1b[39m) The args are expecting a full \`aaa0aa0a-aaaa-0000-0a0a-0000000a00a0\` scan ID. diff --git a/src/commands/diff-scan/fetch-diff-scan.ts b/src/commands/diff-scan/fetch-diff-scan.ts index 220fdc4d9..aae6ec028 100644 --- a/src/commands/diff-scan/fetch-diff-scan.ts +++ b/src/commands/diff-scan/fetch-diff-scan.ts @@ -1,7 +1,8 @@ -import colors from 'yoctocolors-cjs' +import { logger } from '@socketsecurity/registry/lib/logger' import constants from '../../constants' import { handleApiCall, handleApiError, queryApi } from '../../utils/api' +import { failMsgWithBadge } from '../../utils/fail-msg-with-badge' import { getDefaultToken } from '../../utils/sdk' import type { SocketSdkReturnType } from '@socketsecurity/sdk' @@ -31,9 +32,7 @@ export async function fetchDiffScan({ if (!response.ok) { const err = await handleApiError(response.status) - spinner.errorAndStop( - `${colors.bgRed(colors.white(response.statusText))}: ${err}` - ) + logger.fail(failMsgWithBadge(response.statusText, err)) return } diff --git a/src/commands/info/cmd-info.test.ts b/src/commands/info/cmd-info.test.ts index 000ab7ef4..2efceacd5 100644 --- a/src/commands/info/cmd-info.test.ts +++ b/src/commands/info/cmd-info.test.ts @@ -62,7 +62,7 @@ describe('socket info', async () => { |__ | . | _| '_| -_| _| | Node: , API token set: |_____|___|___|_,_|___|_|.dev | Command: \`socket info\`, cwd: - \\x1b[31m\\xd7\\x1b[39m \\x1b[41m\\x1b[1m\\x1b[37m Input error: \\x1b[39m\\x1b[22m\\x1b[49m \\x1b[1mPlease review the input requirements and try again\\x1b[22m: + \\x1b[31m\\xd7\\x1b[39m \\x1b[41m\\x1b[1m\\x1b[37m Input error: \\x1b[39m\\x1b[22m\\x1b[49m \\x1b[1mPlease review the input requirements and try again\\x1b[22m - Expecting a package name (\\x1b[31mmissing\\x1b[39m) diff --git a/src/commands/login/attempt-login.ts b/src/commands/login/attempt-login.ts index 4e3230563..156ddb694 100644 --- a/src/commands/login/attempt-login.ts +++ b/src/commands/login/attempt-login.ts @@ -5,8 +5,8 @@ import { confirm, password, select } from '@socketsecurity/registry/lib/prompts' import { applyLogin } from './apply-login' import constants from '../../constants' +import { handleUnsuccessfulApiResponse } from '../../utils/api' import { getConfigValue } from '../../utils/config' -import { AuthError } from '../../utils/errors' import { setupSdk } from '../../utils/sdk' import type { Choice, Separator } from '@socketsecurity/registry/lib/prompts' @@ -32,22 +32,24 @@ export async function attemptLogin( // Lazily access constants.spinner. const { spinner } = constants + const sdk = await setupSdk(apiToken, apiBaseUrl, apiProxy) + spinner.start('Verifying API key...') - let orgs: SocketSdkReturnType<'getOrganizations'>['data'] - try { - const sdk = await setupSdk(apiToken, apiBaseUrl, apiProxy) - const result = await sdk.getOrganizations() - if (!result.success) { - throw new AuthError() - } - orgs = result.data - spinner.success('API key verified') - } catch { - spinner.errorAndStop('Invalid API key') + const result = await sdk.getOrganizations() + + spinner.successAndStop('Received response') + + if (!result.success) { + logger.fail('Authentication failed...') + handleUnsuccessfulApiResponse('getOrganizations', result) return } + logger.success('API key verified') + + const orgs: SocketSdkReturnType<'getOrganizations'>['data'] = result.data + const enforcedChoices: OrgChoices = Object.values(orgs.organizations) .filter(org => org?.plan === 'enterprise') .map(org => ({ diff --git a/src/commands/manifest/cmd-manifest-gradle.test.ts b/src/commands/manifest/cmd-manifest-gradle.test.ts index a34ff26a5..f7d8e0778 100644 --- a/src/commands/manifest/cmd-manifest-gradle.test.ts +++ b/src/commands/manifest/cmd-manifest-gradle.test.ts @@ -88,7 +88,7 @@ describe('socket manifest gradle', async () => { |__ | . | _| '_| -_| _| | Node: , API token set: |_____|___|___|_,_|___|_|.dev | Command: \`socket manifest gradle\`, cwd: - \\x1b[31m\\xd7\\x1b[39m \\x1b[41m\\x1b[1m\\x1b[37m Input error: \\x1b[39m\\x1b[22m\\x1b[49m \\x1b[1mPlease review the input requirements and try again\\x1b[22m: + \\x1b[31m\\xd7\\x1b[39m \\x1b[41m\\x1b[1m\\x1b[37m Input error: \\x1b[39m\\x1b[22m\\x1b[49m \\x1b[1mPlease review the input requirements and try again\\x1b[22m - The DIR arg is required (\\x1b[31mmissing\\x1b[39m) diff --git a/src/commands/manifest/cmd-manifest-kotlin.test.ts b/src/commands/manifest/cmd-manifest-kotlin.test.ts index 3873b3247..96b34f134 100644 --- a/src/commands/manifest/cmd-manifest-kotlin.test.ts +++ b/src/commands/manifest/cmd-manifest-kotlin.test.ts @@ -88,7 +88,7 @@ describe('socket manifest kotlin', async () => { |__ | . | _| '_| -_| _| | Node: , API token set: |_____|___|___|_,_|___|_|.dev | Command: \`socket manifest kotlin\`, cwd: - \\x1b[31m\\xd7\\x1b[39m \\x1b[41m\\x1b[1m\\x1b[37m Input error: \\x1b[39m\\x1b[22m\\x1b[49m \\x1b[1mPlease review the input requirements and try again\\x1b[22m: + \\x1b[31m\\xd7\\x1b[39m \\x1b[41m\\x1b[1m\\x1b[37m Input error: \\x1b[39m\\x1b[22m\\x1b[49m \\x1b[1mPlease review the input requirements and try again\\x1b[22m - The DIR arg is required (\\x1b[31mmissing\\x1b[39m) diff --git a/src/commands/manifest/cmd-manifest-scala.test.ts b/src/commands/manifest/cmd-manifest-scala.test.ts index 977401669..630ba5516 100644 --- a/src/commands/manifest/cmd-manifest-scala.test.ts +++ b/src/commands/manifest/cmd-manifest-scala.test.ts @@ -92,7 +92,7 @@ describe('socket manifest scala', async () => { |__ | . | _| '_| -_| _| | Node: , API token set: |_____|___|___|_,_|___|_|.dev | Command: \`socket manifest scala\`, cwd: - \\x1b[31m\\xd7\\x1b[39m \\x1b[41m\\x1b[1m\\x1b[37m Input error: \\x1b[39m\\x1b[22m\\x1b[49m \\x1b[1mPlease review the input requirements and try again\\x1b[22m: + \\x1b[31m\\xd7\\x1b[39m \\x1b[41m\\x1b[1m\\x1b[37m Input error: \\x1b[39m\\x1b[22m\\x1b[49m \\x1b[1mPlease review the input requirements and try again\\x1b[22m - The DIR arg is required (\\x1b[31mmissing\\x1b[39m) diff --git a/src/commands/organization/cmd-organization-policy-license.test.ts b/src/commands/organization/cmd-organization-policy-license.test.ts index 3a7bc0c27..5f7413913 100644 --- a/src/commands/organization/cmd-organization-policy-license.test.ts +++ b/src/commands/organization/cmd-organization-policy-license.test.ts @@ -65,7 +65,7 @@ describe('socket organization policy license', async () => { |__ | . | _| '_| -_| _| | Node: , API token set: |_____|___|___|_,_|___|_|.dev | Command: \`socket organization policy license\`, cwd: - \\x1b[31m\\xd7\\x1b[39m \\x1b[41m\\x1b[1m\\x1b[37m Input error: \\x1b[39m\\x1b[22m\\x1b[49m \\x1b[1mPlease review the input requirements and try again\\x1b[22m: + \\x1b[31m\\xd7\\x1b[39m \\x1b[41m\\x1b[1m\\x1b[37m Input error: \\x1b[39m\\x1b[22m\\x1b[49m \\x1b[1mPlease review the input requirements and try again\\x1b[22m - Org name as the first argument (\\x1b[31mmissing\\x1b[39m) diff --git a/src/commands/organization/cmd-organization-policy-security.test.ts b/src/commands/organization/cmd-organization-policy-security.test.ts index a9e2f8f1e..e83897bf9 100644 --- a/src/commands/organization/cmd-organization-policy-security.test.ts +++ b/src/commands/organization/cmd-organization-policy-security.test.ts @@ -65,7 +65,7 @@ describe('socket organization policy security', async () => { |__ | . | _| '_| -_| _| | Node: , API token set: |_____|___|___|_,_|___|_|.dev | Command: \`socket organization policy security\`, cwd: - \\x1b[31m\\xd7\\x1b[39m \\x1b[41m\\x1b[1m\\x1b[37m Input error: \\x1b[39m\\x1b[22m\\x1b[49m \\x1b[1mPlease review the input requirements and try again\\x1b[22m: + \\x1b[31m\\xd7\\x1b[39m \\x1b[41m\\x1b[1m\\x1b[37m Input error: \\x1b[39m\\x1b[22m\\x1b[49m \\x1b[1mPlease review the input requirements and try again\\x1b[22m - Org name as the first argument (\\x1b[31mmissing\\x1b[39m) diff --git a/src/commands/organization/fetch-license-policy.ts b/src/commands/organization/fetch-license-policy.ts index 02db8c301..1bb26525c 100644 --- a/src/commands/organization/fetch-license-policy.ts +++ b/src/commands/organization/fetch-license-policy.ts @@ -1,28 +1,13 @@ import constants from '../../constants' import { handleApiCall, handleUnsuccessfulApiResponse } from '../../utils/api' -import { AuthError } from '../../utils/errors' -import { getDefaultToken, setupSdk } from '../../utils/sdk' +import { setupSdk } from '../../utils/sdk' import type { SocketSdkReturnType } from '@socketsecurity/sdk' export async function fetchLicensePolicy( orgSlug: string ): Promise['data'] | undefined> { - 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.' - ) - } - - return await fetchLicensePolicyWithToken(apiToken, orgSlug) -} - -async function fetchLicensePolicyWithToken( - apiToken: string, - orgSlug: string -): Promise['data'] | undefined> { - const sockSdk = await setupSdk(apiToken) + const sockSdk = await setupSdk() // Lazily access constants.spinner. const { spinner } = constants diff --git a/src/commands/package/cmd-package-score.test.ts b/src/commands/package/cmd-package-score.test.ts index ecac28387..e0040dff9 100644 --- a/src/commands/package/cmd-package-score.test.ts +++ b/src/commands/package/cmd-package-score.test.ts @@ -84,7 +84,7 @@ describe('socket package score', async () => { |__ | . | _| '_| -_| _| | Node: , API token set: |_____|___|___|_,_|___|_|.dev | Command: \`socket package score\`, cwd: - \\x1b[31m\\xd7\\x1b[39m \\x1b[41m\\x1b[1m\\x1b[37m Input error: \\x1b[39m\\x1b[22m\\x1b[49m \\x1b[1mPlease review the input requirements and try again\\x1b[22m: + \\x1b[31m\\xd7\\x1b[39m \\x1b[41m\\x1b[1m\\x1b[37m Input error: \\x1b[39m\\x1b[22m\\x1b[49m \\x1b[1mPlease review the input requirements and try again\\x1b[22m - First parameter must be an ecosystem or the whole purl (\\x1b[31mbad\\x1b[39m) diff --git a/src/commands/package/cmd-package-shallow.test.ts b/src/commands/package/cmd-package-shallow.test.ts index c8c07a9eb..1f403e7d6 100644 --- a/src/commands/package/cmd-package-shallow.test.ts +++ b/src/commands/package/cmd-package-shallow.test.ts @@ -83,7 +83,7 @@ describe('socket package shallow', async () => { |__ | . | _| '_| -_| _| | Node: , API token set: |_____|___|___|_,_|___|_|.dev | Command: \`socket package shallow\`, cwd: - \\x1b[31m\\xd7\\x1b[39m \\x1b[41m\\x1b[1m\\x1b[37m Input error: \\x1b[39m\\x1b[22m\\x1b[49m \\x1b[1mPlease review the input requirements and try again\\x1b[22m: + \\x1b[31m\\xd7\\x1b[39m \\x1b[41m\\x1b[1m\\x1b[37m Input error: \\x1b[39m\\x1b[22m\\x1b[49m \\x1b[1mPlease review the input requirements and try again\\x1b[22m - First parameter should be an ecosystem or all args must be purls (\\x1b[31mbad\\x1b[39m) diff --git a/src/commands/package/fetch-purl-deep-score.ts b/src/commands/package/fetch-purl-deep-score.ts index 30b81dbf0..b636e6a2b 100644 --- a/src/commands/package/fetch-purl-deep-score.ts +++ b/src/commands/package/fetch-purl-deep-score.ts @@ -1,10 +1,9 @@ -import colors from 'yoctocolors-cjs' - import { logger } from '@socketsecurity/registry/lib/logger' import constants from '../../constants' import { handleApiCall, handleApiError, queryApi } from '../../utils/api' import { AuthError } from '../../utils/errors' +import { failMsgWithBadge } from '../../utils/fail-msg-with-badge' import { getDefaultToken } from '../../utils/sdk' export async function fetchPurlDeepScore(purl: string) { @@ -42,9 +41,7 @@ export async function fetchPurlDeepScore(purl: string) { if (!result.ok) { const err = await handleApiError(result.status) - logger.fail( - `${colors.bgRed(colors.bold(colors.white(' ' + result.statusText + ' ')))}: ${err}` - ) + logger.fail(failMsgWithBadge(result.statusText, err)) process.exitCode = 1 return } diff --git a/src/commands/package/fetch-purls-shallow-score.ts b/src/commands/package/fetch-purls-shallow-score.ts index bbff1caa1..9369fd752 100644 --- a/src/commands/package/fetch-purls-shallow-score.ts +++ b/src/commands/package/fetch-purls-shallow-score.ts @@ -11,7 +11,7 @@ import type { export async function fetchPurlsShallowScore( purls: string[] -): Promise> { +): Promise | undefined> { logger.error( `Requesting shallow score data for ${purls.length} package urls (purl): ${purls.join(', ')}` ) @@ -40,8 +40,10 @@ export async function fetchPurlsShallowScore( spinner.successAndStop('Request completed') - if (result.success) { - return result + if (!result.success) { + handleUnsuccessfulApiResponse('batchPackageFetch', result) + return } - handleUnsuccessfulApiResponse('batchPackageFetch', result) + + return result } diff --git a/src/commands/package/handle-purls-shallow-score.ts b/src/commands/package/handle-purls-shallow-score.ts index 1b05500d2..f78afdce7 100644 --- a/src/commands/package/handle-purls-shallow-score.ts +++ b/src/commands/package/handle-purls-shallow-score.ts @@ -11,11 +11,13 @@ export async function handlePurlsShallowScore({ purls: string[] }) { const packageData = await fetchPurlsShallowScore(purls) - if (packageData) { - outputPurlsShallowScore( - purls, - packageData.data as Array, - outputKind - ) + if (!packageData) { + return } + + outputPurlsShallowScore( + purls, + packageData.data as Array, + outputKind + ) } diff --git a/src/commands/repos/cmd-repos-create.test.ts b/src/commands/repos/cmd-repos-create.test.ts index d2cee262a..f7e5222bd 100644 --- a/src/commands/repos/cmd-repos-create.test.ts +++ b/src/commands/repos/cmd-repos-create.test.ts @@ -64,7 +64,7 @@ describe('socket repos create', async () => { |__ | . | _| '_| -_| _| | Node: , API token set: |_____|___|___|_,_|___|_|.dev | Command: \`socket repos create\`, cwd: - \\x1b[31m\\xd7\\x1b[39m \\x1b[41m\\x1b[1m\\x1b[37m Input error: \\x1b[39m\\x1b[22m\\x1b[49m \\x1b[1mPlease review the input requirements and try again\\x1b[22m: + \\x1b[31m\\xd7\\x1b[39m \\x1b[41m\\x1b[1m\\x1b[37m Input error: \\x1b[39m\\x1b[22m\\x1b[49m \\x1b[1mPlease review the input requirements and try again\\x1b[22m - Org name as the first argument (\\x1b[31mmissing\\x1b[39m) diff --git a/src/commands/repos/cmd-repos-del.test.ts b/src/commands/repos/cmd-repos-del.test.ts index 65d0941b8..eb96a7841 100644 --- a/src/commands/repos/cmd-repos-del.test.ts +++ b/src/commands/repos/cmd-repos-del.test.ts @@ -59,7 +59,7 @@ describe('socket repos del', async () => { |__ | . | _| '_| -_| _| | Node: , API token set: |_____|___|___|_,_|___|_|.dev | Command: \`socket repos del\`, cwd: - \\x1b[31m\\xd7\\x1b[39m \\x1b[41m\\x1b[1m\\x1b[37m Input error: \\x1b[39m\\x1b[22m\\x1b[49m \\x1b[1mPlease review the input requirements and try again\\x1b[22m: + \\x1b[31m\\xd7\\x1b[39m \\x1b[41m\\x1b[1m\\x1b[37m Input error: \\x1b[39m\\x1b[22m\\x1b[49m \\x1b[1mPlease review the input requirements and try again\\x1b[22m - Org name as the first argument (\\x1b[31mmissing\\x1b[39m) diff --git a/src/commands/repos/cmd-repos-list.test.ts b/src/commands/repos/cmd-repos-list.test.ts index ce2813a0f..7db3e5814 100644 --- a/src/commands/repos/cmd-repos-list.test.ts +++ b/src/commands/repos/cmd-repos-list.test.ts @@ -65,7 +65,7 @@ describe('socket repos list', async () => { |__ | . | _| '_| -_| _| | Node: , API token set: |_____|___|___|_,_|___|_|.dev | Command: \`socket repos list\`, cwd: - \\x1b[31m\\xd7\\x1b[39m \\x1b[41m\\x1b[1m\\x1b[37m Input error: \\x1b[39m\\x1b[22m\\x1b[49m \\x1b[1mPlease review the input requirements and try again\\x1b[22m: + \\x1b[31m\\xd7\\x1b[39m \\x1b[41m\\x1b[1m\\x1b[37m Input error: \\x1b[39m\\x1b[22m\\x1b[49m \\x1b[1mPlease review the input requirements and try again\\x1b[22m - Org name as the first argument (\\x1b[31mmissing\\x1b[39m) diff --git a/src/commands/repos/cmd-repos-update.test.ts b/src/commands/repos/cmd-repos-update.test.ts index 73aa83d6b..b93288a12 100644 --- a/src/commands/repos/cmd-repos-update.test.ts +++ b/src/commands/repos/cmd-repos-update.test.ts @@ -64,7 +64,7 @@ describe('socket repos update', async () => { |__ | . | _| '_| -_| _| | Node: , API token set: |_____|___|___|_,_|___|_|.dev | Command: \`socket repos update\`, cwd: - \\x1b[31m\\xd7\\x1b[39m \\x1b[41m\\x1b[1m\\x1b[37m Input error: \\x1b[39m\\x1b[22m\\x1b[49m \\x1b[1mPlease review the input requirements and try again\\x1b[22m: + \\x1b[31m\\xd7\\x1b[39m \\x1b[41m\\x1b[1m\\x1b[37m Input error: \\x1b[39m\\x1b[22m\\x1b[49m \\x1b[1mPlease review the input requirements and try again\\x1b[22m - Org name as the first argument (\\x1b[31mmissing\\x1b[39m) diff --git a/src/commands/repos/cmd-repos-view.test.ts b/src/commands/repos/cmd-repos-view.test.ts index 268ad6755..eae88f53e 100644 --- a/src/commands/repos/cmd-repos-view.test.ts +++ b/src/commands/repos/cmd-repos-view.test.ts @@ -62,7 +62,7 @@ describe('socket repos view', async () => { |__ | . | _| '_| -_| _| | Node: , API token set: |_____|___|___|_,_|___|_|.dev | Command: \`socket repos view\`, cwd: - \\x1b[31m\\xd7\\x1b[39m \\x1b[41m\\x1b[1m\\x1b[37m Input error: \\x1b[39m\\x1b[22m\\x1b[49m \\x1b[1mPlease review the input requirements and try again\\x1b[22m: + \\x1b[31m\\xd7\\x1b[39m \\x1b[41m\\x1b[1m\\x1b[37m Input error: \\x1b[39m\\x1b[22m\\x1b[49m \\x1b[1mPlease review the input requirements and try again\\x1b[22m - Org name as the first argument (\\x1b[31mmissing\\x1b[39m) diff --git a/src/commands/scan/cmd-scan-del.test.ts b/src/commands/scan/cmd-scan-del.test.ts index 85509ec9a..44ccf7352 100644 --- a/src/commands/scan/cmd-scan-del.test.ts +++ b/src/commands/scan/cmd-scan-del.test.ts @@ -61,7 +61,7 @@ describe('socket scan del', async () => { |__ | . | _| '_| -_| _| | Node: , API token set: |_____|___|___|_,_|___|_|.dev | Command: \`socket scan del\`, cwd: - \\x1b[31m\\xd7\\x1b[39m \\x1b[41m\\x1b[1m\\x1b[37m Input error: \\x1b[39m\\x1b[22m\\x1b[49m \\x1b[1mPlease review the input requirements and try again\\x1b[22m: + \\x1b[31m\\xd7\\x1b[39m \\x1b[41m\\x1b[1m\\x1b[37m Input error: \\x1b[39m\\x1b[22m\\x1b[49m \\x1b[1mPlease review the input requirements and try again\\x1b[22m - Org name as the first argument (\\x1b[31mmissing\\x1b[39m) diff --git a/src/commands/scan/cmd-scan-list.test.ts b/src/commands/scan/cmd-scan-list.test.ts index ae1686f4b..25a4246d6 100644 --- a/src/commands/scan/cmd-scan-list.test.ts +++ b/src/commands/scan/cmd-scan-list.test.ts @@ -67,7 +67,7 @@ describe('socket scan list', async () => { |__ | . | _| '_| -_| _| | Node: , API token set: |_____|___|___|_,_|___|_|.dev | Command: \`socket scan list\`, cwd: - \\x1b[31m\\xd7\\x1b[39m \\x1b[41m\\x1b[1m\\x1b[37m Input error: \\x1b[39m\\x1b[22m\\x1b[49m \\x1b[1mPlease review the input requirements and try again\\x1b[22m: + \\x1b[31m\\xd7\\x1b[39m \\x1b[41m\\x1b[1m\\x1b[37m Input error: \\x1b[39m\\x1b[22m\\x1b[49m \\x1b[1mPlease review the input requirements and try again\\x1b[22m - Org name as the first argument (\\x1b[31mmissing\\x1b[39m) diff --git a/src/commands/scan/cmd-scan-metadata.test.ts b/src/commands/scan/cmd-scan-metadata.test.ts index 0dbf74cc0..89122f249 100644 --- a/src/commands/scan/cmd-scan-metadata.test.ts +++ b/src/commands/scan/cmd-scan-metadata.test.ts @@ -61,7 +61,7 @@ describe('socket scan metadata', async () => { |__ | . | _| '_| -_| _| | Node: , API token set: |_____|___|___|_,_|___|_|.dev | Command: \`socket scan metadata\`, cwd: - \\x1b[31m\\xd7\\x1b[39m \\x1b[41m\\x1b[1m\\x1b[37m Input error: \\x1b[39m\\x1b[22m\\x1b[49m \\x1b[1mPlease review the input requirements and try again\\x1b[22m: + \\x1b[31m\\xd7\\x1b[39m \\x1b[41m\\x1b[1m\\x1b[37m Input error: \\x1b[39m\\x1b[22m\\x1b[49m \\x1b[1mPlease review the input requirements and try again\\x1b[22m - Org name as the first argument (\\x1b[31mmissing\\x1b[39m) diff --git a/src/commands/scan/cmd-scan-report.test.ts b/src/commands/scan/cmd-scan-report.test.ts index 52b57184d..9359882ef 100644 --- a/src/commands/scan/cmd-scan-report.test.ts +++ b/src/commands/scan/cmd-scan-report.test.ts @@ -81,7 +81,7 @@ describe('socket scan report', async () => { |__ | . | _| '_| -_| _| | Node: , API token set: |_____|___|___|_,_|___|_|.dev | Command: \`socket scan report\`, cwd: - \\x1b[31m\\xd7\\x1b[39m \\x1b[41m\\x1b[1m\\x1b[37m Input error: \\x1b[39m\\x1b[22m\\x1b[49m \\x1b[1mPlease review the input requirements and try again\\x1b[22m: + \\x1b[31m\\xd7\\x1b[39m \\x1b[41m\\x1b[1m\\x1b[37m Input error: \\x1b[39m\\x1b[22m\\x1b[49m \\x1b[1mPlease review the input requirements and try again\\x1b[22m - Org name as the first argument (\\x1b[31mmissing\\x1b[39m) diff --git a/src/commands/scan/cmd-scan-view.test.ts b/src/commands/scan/cmd-scan-view.test.ts index 73627ad09..268901122 100644 --- a/src/commands/scan/cmd-scan-view.test.ts +++ b/src/commands/scan/cmd-scan-view.test.ts @@ -63,7 +63,7 @@ describe('socket scan view', async () => { |__ | . | _| '_| -_| _| | Node: , API token set: |_____|___|___|_,_|___|_|.dev | Command: \`socket scan view\`, cwd: - \\x1b[31m\\xd7\\x1b[39m \\x1b[41m\\x1b[1m\\x1b[37m Input error: \\x1b[39m\\x1b[22m\\x1b[49m \\x1b[1mPlease review the input requirements and try again\\x1b[22m: + \\x1b[31m\\xd7\\x1b[39m \\x1b[41m\\x1b[1m\\x1b[37m Input error: \\x1b[39m\\x1b[22m\\x1b[49m \\x1b[1mPlease review the input requirements and try again\\x1b[22m - Org name as the first argument (\\x1b[31mmissing\\x1b[39m) diff --git a/src/commands/scan/fetch-report-data.ts b/src/commands/scan/fetch-report-data.ts index ce703bfce..64c50fb00 100644 --- a/src/commands/scan/fetch-report-data.ts +++ b/src/commands/scan/fetch-report-data.ts @@ -1,10 +1,9 @@ -import colors from 'yoctocolors-cjs' - import { logger } from '@socketsecurity/registry/lib/logger' import constants from '../../constants' import { handleApiCall, handleApiError, queryApi } from '../../utils/api' import { AuthError } from '../../utils/errors' +import { failMsgWithBadge } from '../../utils/fail-msg-with-badge' import { getDefaultToken, setupSdk } from '../../utils/sdk' import type { @@ -37,6 +36,15 @@ export async function fetchReportData( securityPolicy: undefined } > { + 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.' + ) + } + + const sockSdk = await setupSdk(apiToken) + let haveScan = false // let haveLicensePolicy = false let haveSecurityPolicy = false @@ -79,17 +87,8 @@ export async function fetchReportData( } } - 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.' - ) - } - updateProgress() - const sockSdk = await setupSdk(apiToken) - // @ts-ignore const [ scan, @@ -113,7 +112,7 @@ export async function fetchReportData( if (!response.ok) { const err = await handleApiError(response.status) logger.fail( - `${colors.bgRed(colors.white(response.statusText))}: Fetch error: ${err}` + failMsgWithBadge(response.statusText, `Fetch error: ${err}`) ) return undefined } diff --git a/src/commands/scan/fetch-scan.ts b/src/commands/scan/fetch-scan.ts index feb91b363..38ed3918c 100644 --- a/src/commands/scan/fetch-scan.ts +++ b/src/commands/scan/fetch-scan.ts @@ -1,10 +1,9 @@ -import colors from 'yoctocolors-cjs' - import { logger } from '@socketsecurity/registry/lib/logger' import constants from '../../constants' import { handleApiError, queryApi } from '../../utils/api' import { AuthError } from '../../utils/errors' +import { failMsgWithBadge } from '../../utils/fail-msg-with-badge' import { getDefaultToken } from '../../utils/sdk' import type { components } from '@socketsecurity/sdk/types/api' @@ -23,20 +22,18 @@ export async function fetchScan( // Lazily access constants.spinner. const { spinner } = constants - spinner.start('Fetching full-scan...') + spinner.start('Fetching scan data...') const response = await queryApi( `orgs/${orgSlug}/full-scans/${encodeURIComponent(scanId)}`, apiToken ) - spinner.stop('Fetch complete.') + spinner.successAndStop('Received response while fetching scan data.') if (!response.ok) { const err = await handleApiError(response.status) - logger.fail( - `${colors.bgRed(colors.white(response.statusText))}: Fetch error: ${err}` - ) + logger.fail(failMsgWithBadge(response.statusText, `Fetch error: ${err}`)) return } diff --git a/src/commands/scan/handle-create-new-scan.ts b/src/commands/scan/handle-create-new-scan.ts index c4ba354d3..ec7c0b252 100644 --- a/src/commands/scan/handle-create-new-scan.ts +++ b/src/commands/scan/handle-create-new-scan.ts @@ -1,16 +1,10 @@ -import process from 'node:process' - -import { stripIndents } from 'common-tags' -import colors from 'yoctocolors-cjs' - import { logger } from '@socketsecurity/registry/lib/logger' import { fetchCreateOrgFullScan } from './fetch-create-org-full-scan' import { fetchSupportedScanFileNames } from './fetch-supported-scan-file-names' import { outputCreateNewScan } from './output-create-new-scan' -import { AuthError } from '../../utils/errors' +import { handleBadInput } from '../../utils/handle-bad-input' import { getPackageFilesForScan } from '../../utils/path-resolve' -import { getDefaultToken } from '../../utils/sdk' export async function handleCreateNewScan({ branchName, @@ -35,15 +29,6 @@ export async function handleCreateNewScan({ targets: string[] tmp: boolean }): Promise { - const apiToken = getDefaultToken() - - // Note: you need an apiToken to request supportedScanFileNames from the API - if (!apiToken) { - throw new AuthError( - 'User must be authenticated to create and submit new scans. To log in, run the command `socket login` and enter your API key.' - ) - } - const supportedFileNames = await fetchSupportedScanFileNames() if (!supportedFileNames) { return @@ -56,16 +41,13 @@ export async function handleCreateNewScan({ // socketConfig ) - if (!packagePaths.length) { - // Use exit status of 2 to indicate incorrect usage, generally invalid - // options or missing arguments. - // https://www.gnu.org/software/bash/manual/html_node/Exit-Status.html - process.exitCode = 2 - logger.fail(stripIndents` - ${colors.bgRed(colors.white('Input error'))}: The TARGET did not contain any matching / supported files for a scan - `) - return - } + handleBadInput({ + nook: true, + test: packagePaths.length > 0, + pass: 'ok', + fail: 'found none', + message: 'TARGET must contain matching / supported file types for a scan' + }) if (readOnly) { logger.log('[ReadOnly] Bailing now') diff --git a/src/commands/scan/streamScan.ts b/src/commands/scan/streamScan.ts index 589c48bcb..6e2eeb513 100644 --- a/src/commands/scan/streamScan.ts +++ b/src/commands/scan/streamScan.ts @@ -1,7 +1,6 @@ import constants from '../../constants' import { handleApiCall, handleUnsuccessfulApiResponse } from '../../utils/api' -import { AuthError } from '../../utils/errors' -import { getDefaultToken, setupSdk } from '../../utils/sdk' +import { setupSdk } from '../../utils/sdk' import type { SocketSdkResultType } from '@socketsecurity/sdk' @@ -13,14 +12,7 @@ export async function streamScan( // Lazily access constants.spinner. const { spinner } = constants - 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.' - ) - } - - const sockSdk = await setupSdk(apiToken) + const sockSdk = await setupSdk() spinner.start('Fetching scan...') diff --git a/src/commands/threat-feed/cmd-threat-feed.test.ts b/src/commands/threat-feed/cmd-threat-feed.test.ts index 636fb4273..13b209076 100644 --- a/src/commands/threat-feed/cmd-threat-feed.test.ts +++ b/src/commands/threat-feed/cmd-threat-feed.test.ts @@ -93,7 +93,7 @@ describe('socket threat-feed', async () => { |__ | . | _| '_| -_| _| | Node: , API token set: |_____|___|___|_,_|___|_|.dev | Command: \`socket threat-feed\`, cwd: - \\x1b[31m\\xd7\\x1b[39m \\x1b[41m\\x1b[1m\\x1b[37m Input error: \\x1b[39m\\x1b[22m\\x1b[49m \\x1b[1mPlease review the input requirements and try again\\x1b[22m: + \\x1b[31m\\xd7\\x1b[39m \\x1b[41m\\x1b[1m\\x1b[37m Input error: \\x1b[39m\\x1b[22m\\x1b[49m \\x1b[1mPlease review the input requirements and try again\\x1b[22m - Org name as the first argument (\\x1b[31mmissing\\x1b[39m) diff --git a/src/commands/threat-feed/fetch-threat-feed.ts b/src/commands/threat-feed/fetch-threat-feed.ts index 883fa338d..74825318e 100644 --- a/src/commands/threat-feed/fetch-threat-feed.ts +++ b/src/commands/threat-feed/fetch-threat-feed.ts @@ -40,7 +40,7 @@ export async function fetchThreatFeed({ const response = await queryApi(`threat-feed?${queryParams}`, apiToken) - spinner.successAndStop('Threat feed data fetched') + spinner.successAndStop('Received response while fetching Threat Feed data.') const data = await response.json() diff --git a/src/commands/threat-feed/handle-threat-feed.ts b/src/commands/threat-feed/handle-threat-feed.ts index e5813c115..9cb8aa6d4 100644 --- a/src/commands/threat-feed/handle-threat-feed.ts +++ b/src/commands/threat-feed/handle-threat-feed.ts @@ -1,5 +1,8 @@ +import { logger } from '@socketsecurity/registry/lib/logger' + import { fetchThreatFeed } from './fetch-threat-feed' import { outputThreatFeed } from './output-threat-feed' +import { failMsgWithBadge } from '../../utils/fail-msg-with-badge' import type { ThreadFeedResponse } from './types' @@ -30,7 +33,7 @@ export async function handleThreatFeed({ } if ('error' in data && data.error) { - console.log(data.error.message) + logger.fail(failMsgWithBadge('Server Error', data.error.message)) return } diff --git a/src/commands/wrapper/cmd-wrapper.test.ts b/src/commands/wrapper/cmd-wrapper.test.ts index 5c67ae971..5f4603a3b 100644 --- a/src/commands/wrapper/cmd-wrapper.test.ts +++ b/src/commands/wrapper/cmd-wrapper.test.ts @@ -62,7 +62,7 @@ describe('socket wrapper', async () => { |__ | . | _| '_| -_| _| | Node: , API token set: |_____|___|___|_,_|___|_|.dev | Command: \`socket wrapper\`, cwd: - \\x1b[31m\\xd7\\x1b[39m \\x1b[41m\\x1b[1m\\x1b[37m Input error: \\x1b[39m\\x1b[22m\\x1b[49m \\x1b[1mPlease review the input requirements and try again\\x1b[22m: + \\x1b[31m\\xd7\\x1b[39m \\x1b[41m\\x1b[1m\\x1b[37m Input error: \\x1b[39m\\x1b[22m\\x1b[49m \\x1b[1mPlease review the input requirements and try again\\x1b[22m - Must use --enabled or --disable (\\x1b[31mmissing\\x1b[39m)" `) diff --git a/src/utils/api.ts b/src/utils/api.ts index 30f21b149..91c8168db 100644 --- a/src/utils/api.ts +++ b/src/utils/api.ts @@ -1,13 +1,12 @@ import process from 'node:process' -import colors from 'yoctocolors-cjs' - import { logger } from '@socketsecurity/registry/lib/logger' import { isNonEmptyString } from '@socketsecurity/registry/lib/strings' import { getConfigValue } from './config' import { AuthError } from './errors' import constants from '../constants' +import { failMsgWithBadge } from './fail-msg-with-badge' import type { SocketSdkErrorType, @@ -28,9 +27,7 @@ export function handleUnsuccessfulApiResponse( throw new AuthError(message) } - logger.fail( - `${colors.bgRed(colors.white('API returned an error:'))} ${message}` - ) + logger.fail(failMsgWithBadge('API returned an error:', message)) // eslint-disable-next-line n/no-process-exit process.exit(1) } @@ -52,9 +49,9 @@ export async function handleApiError(code: number) { if (code === 400) { return 'One of the options passed might be incorrect.' } else if (code === 403) { - return 'You might be trying to access an organization that is not linked to the API key you are logged in with.' + return 'Your API token may not have the required permissions for this command or you might be trying to access (data from) an organization that is not linked to the API key you are logged in with.' } else if (code === 404) { - return 'The requested Socket API endpoint was not found (404). This could be a temporary problem caused by an incident or a bug in the CLI. If the problem persists please let us know.' + return 'The requested Socket API endpoint was not found (404) or there was no result for the requested parameters. This could be a temporary problem caused by an incident or a bug in the CLI. If the problem persists please let us know.' } else { return `Server responded with status code ${code}` } diff --git a/src/utils/fail-msg-with-badge.ts b/src/utils/fail-msg-with-badge.ts new file mode 100644 index 000000000..f6ef18b12 --- /dev/null +++ b/src/utils/fail-msg-with-badge.ts @@ -0,0 +1,5 @@ +import colors from 'yoctocolors-cjs' + +export function failMsgWithBadge(badge: string, msg: string): string { + return `${colors.bgRed(colors.bold(colors.white(` ${badge}: `)))} ${colors.bold(msg)}` +} diff --git a/src/utils/handle-bad-input.ts b/src/utils/handle-bad-input.ts index 93163d641..19f9d9627 100644 --- a/src/utils/handle-bad-input.ts +++ b/src/utils/handle-bad-input.ts @@ -2,6 +2,8 @@ import colors from 'yoctocolors-cjs' import { logger } from '@socketsecurity/registry/lib/logger' +import { failMsgWithBadge } from './fail-msg-with-badge' + export function handleBadInput( ...arr: Array<{ message: string @@ -16,7 +18,10 @@ export function handleBadInput( } const msg = [ - `${colors.bgRed(colors.bold(colors.white(' Input error: ')))} ${colors.bold('Please review the input requirements and try again')}:`, + failMsgWithBadge( + 'Input error', + 'Please review the input requirements and try again' + ), '' ] for (const data of arr) {