Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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}`)
}
Expand Down
2 changes: 1 addition & 1 deletion src/commands/analytics/cmd-analytics.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ describe('socket analytics', async () => {
|__ | . | _| '_| -_| _| | Node: <redacted>, API token set: <redacted>
|_____|___|___|_,_|___|_|.dev | Command: \`socket analytics\`, cwd: <redacted>

\\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)

Expand Down
38 changes: 2 additions & 36 deletions src/commands/analytics/display-analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 :'(
Expand Down Expand Up @@ -76,38 +74,6 @@ export async function displayAnalytics({
repo: string
outputKind: 'json' | 'markdown' | 'print'
filePath: string
}): Promise<void> {
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<void> {
// Lazily access constants.spinner.
const { spinner } = constants
Expand All @@ -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
Expand Down
5 changes: 2 additions & 3 deletions src/commands/analytics/fetch-org-analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@ import type { SocketSdkReturnType } from '@socketsecurity/sdk'

export async function fetchOrgAnalyticsData(
time: number,
spinner: Spinner,
apiToken: string
spinner: Spinner
): Promise<SocketSdkReturnType<'getOrgAnalytics'>['data'] | undefined> {
const sockSdk = await setupSdk(apiToken)
const sockSdk = await setupSdk()
const result = await handleApiCall(
sockSdk.getOrgAnalytics(time.toString()),
'fetching analytics data'
Expand Down
5 changes: 2 additions & 3 deletions src/commands/analytics/fetch-repo-analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<SocketSdkReturnType<'getRepoAnalytics'>['data'] | undefined> {
const sockSdk = await setupSdk(apiToken)
const sockSdk = await setupSdk()
const result = await handleApiCall(
sockSdk.getRepoAnalytics(repo, time.toString()),
'fetching analytics data'
Expand Down
2 changes: 1 addition & 1 deletion src/commands/audit-log/cmd-audit-log.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ describe('socket audit-log', async () => {
|__ | . | _| '_| -_| _| | Node: <redacted>, API token set: <redacted>
|_____|___|___|_,_|___|_|.dev | Command: \`socket audit-log\`, cwd: <redacted>

\\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)

Expand Down
10 changes: 0 additions & 10 deletions src/commands/cdxgen/cmd-cdxgen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
2 changes: 1 addition & 1 deletion src/commands/config/cmd-config-get.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ describe('socket config get', async () => {
|__ | . | _| '_| -_| _| | Node: <redacted>, API token set: <redacted>
|_____|___|___|_,_|___|_|.dev | Command: \`socket config get\`, cwd: <redacted>

\\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)"
`)
Expand Down
2 changes: 1 addition & 1 deletion src/commands/config/cmd-config-set.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ describe('socket config get', async () => {
|__ | . | _| '_| -_| _| | Node: <redacted>, API token set: <redacted>
|_____|___|___|_,_|___|_|.dev | Command: \`socket config set\`, cwd: <redacted>

\\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)

Expand Down
2 changes: 1 addition & 1 deletion src/commands/config/cmd-config-unset.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ describe('socket config unset', async () => {
|__ | . | _| '_| -_| _| | Node: <redacted>, API token set: <redacted>
|_____|___|___|_,_|___|_|.dev | Command: \`socket config unset\`, cwd: <redacted>

\\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)"
`)
Expand Down
2 changes: 1 addition & 1 deletion src/commands/diff-scan/cmd-diff-scan-get.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ describe('socket diff-scan get', async () => {
|__ | . | _| '_| -_| _| | Node: <redacted>, API token set: <redacted>
|_____|___|___|_,_|___|_|.dev | Command: \`socket diff-scan get\`, cwd: <redacted>

\\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.
Expand Down
7 changes: 3 additions & 4 deletions src/commands/diff-scan/fetch-diff-scan.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion src/commands/info/cmd-info.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ describe('socket info', async () => {
|__ | . | _| '_| -_| _| | Node: <redacted>, API token set: <redacted>
|_____|___|___|_,_|___|_|.dev | Command: \`socket info\`, cwd: <redacted>

\\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)

Expand Down
26 changes: 14 additions & 12 deletions src/commands/login/attempt-login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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 => ({
Expand Down
2 changes: 1 addition & 1 deletion src/commands/manifest/cmd-manifest-gradle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ describe('socket manifest gradle', async () => {
|__ | . | _| '_| -_| _| | Node: <redacted>, API token set: <redacted>
|_____|___|___|_,_|___|_|.dev | Command: \`socket manifest gradle\`, cwd: <redacted>

\\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)

Expand Down
2 changes: 1 addition & 1 deletion src/commands/manifest/cmd-manifest-kotlin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ describe('socket manifest kotlin', async () => {
|__ | . | _| '_| -_| _| | Node: <redacted>, API token set: <redacted>
|_____|___|___|_,_|___|_|.dev | Command: \`socket manifest kotlin\`, cwd: <redacted>

\\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)

Expand Down
2 changes: 1 addition & 1 deletion src/commands/manifest/cmd-manifest-scala.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ describe('socket manifest scala', async () => {
|__ | . | _| '_| -_| _| | Node: <redacted>, API token set: <redacted>
|_____|___|___|_,_|___|_|.dev | Command: \`socket manifest scala\`, cwd: <redacted>

\\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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ describe('socket organization policy license', async () => {
|__ | . | _| '_| -_| _| | Node: <redacted>, API token set: <redacted>
|_____|___|___|_,_|___|_|.dev | Command: \`socket organization policy license\`, cwd: <redacted>

\\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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ describe('socket organization policy security', async () => {
|__ | . | _| '_| -_| _| | Node: <redacted>, API token set: <redacted>
|_____|___|___|_,_|___|_|.dev | Command: \`socket organization policy security\`, cwd: <redacted>

\\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)

Expand Down
19 changes: 2 additions & 17 deletions src/commands/organization/fetch-license-policy.ts
Original file line number Diff line number Diff line change
@@ -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<SocketSdkReturnType<'getOrgLicensePolicy'>['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<SocketSdkReturnType<'getOrgLicensePolicy'>['data'] | undefined> {
const sockSdk = await setupSdk(apiToken)
const sockSdk = await setupSdk()

// Lazily access constants.spinner.
const { spinner } = constants
Expand Down
2 changes: 1 addition & 1 deletion src/commands/package/cmd-package-score.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ describe('socket package score', async () => {
|__ | . | _| '_| -_| _| | Node: <redacted>, API token set: <redacted>
|_____|___|___|_,_|___|_|.dev | Command: \`socket package score\`, cwd: <redacted>

\\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)

Expand Down
2 changes: 1 addition & 1 deletion src/commands/package/cmd-package-shallow.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ describe('socket package shallow', async () => {
|__ | . | _| '_| -_| _| | Node: <redacted>, API token set: <redacted>
|_____|___|___|_,_|___|_|.dev | Command: \`socket package shallow\`, cwd: <redacted>

\\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)

Expand Down
7 changes: 2 additions & 5 deletions src/commands/package/fetch-purl-deep-score.ts
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down Expand Up @@ -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
}
Expand Down
Loading