Skip to content

Apply handle pattern to dependencies #374

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 20, 2025
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
13 changes: 7 additions & 6 deletions src/commands/dependencies/cmd-dependencies.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { logger } from '@socketsecurity/registry/lib/logger'

import { findDependencies } from './find-dependencies'
import { handleDependencies } from './handle-dependencies'
import constants from '../../constants'
import { commonFlags, outputFlags } from '../../flags'
import { meowOrExit } from '../../utils/meow-with-subcommands'
Expand Down Expand Up @@ -61,15 +61,16 @@ async function run(
parentName
})

const { json, limit, markdown, offset } = cli.flags

if (cli.flags['dryRun']) {
logger.log(DRY_RUN_BAIL_TEXT)
return
}

// TODO: markdown flag is ignored
await findDependencies({
limit: Number(cli.flags['limit'] || 0) || 0,
offset: Number(cli.flags['offset'] || 0) || 0,
outputJson: Boolean(cli.flags['json'])
await handleDependencies({
limit: Number(limit || 0) || 0,
offset: Number(offset || 0) || 0,
outputKind: json ? 'json' : markdown ? 'markdown' : 'text'
})
}
58 changes: 58 additions & 0 deletions src/commands/dependencies/fetch-dependencies.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import constants from '../../constants'
import { handleApiCall, handleUnsuccessfulApiResponse } from '../../utils/api'
import { AuthError } from '../../utils/errors'
import { getDefaultToken, setupSdk } from '../../utils/sdk'

import type { SocketSdkReturnType } from '@socketsecurity/sdk'

export async function fetchDependencies({
limit,
offset
}: {
limit: number
offset: number
}): Promise<SocketSdkReturnType<'searchDependencies'>['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 fetchDependenciesWithToken(apiToken, {
limit,
offset
})
}

async function fetchDependenciesWithToken(
apiToken: string,
{
limit,
offset
}: {
limit: number
offset: number
}
): Promise<SocketSdkReturnType<'searchDependencies'>['data'] | undefined> {
// Lazily access constants.spinner.
const { spinner } = constants

spinner.start('Fetching organization dependencies...')

const socketSdk = await setupSdk(apiToken)

const result = await handleApiCall(
socketSdk.searchDependencies({ limit, offset }),
'Searching dependencies'
)

spinner?.successAndStop('Received organization dependencies response.')

if (!result.success) {
handleUnsuccessfulApiResponse('searchDependencies', result)
return
}

return result.data
}
73 changes: 0 additions & 73 deletions src/commands/dependencies/find-dependencies.ts

This file was deleted.

17 changes: 17 additions & 0 deletions src/commands/dependencies/handle-dependencies.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { fetchDependencies } from './fetch-dependencies'
import { outputDependencies } from './output-dependencies'

export async function handleDependencies({
limit,
offset,
outputKind
}: {
limit: number
offset: number
outputKind: 'json' | 'markdown' | 'text'
}): Promise<void> {
const data = await fetchDependencies({ limit, offset })
if (!data) return

await outputDependencies(data, { limit, offset, outputKind })
}
59 changes: 59 additions & 0 deletions src/commands/dependencies/output-dependencies.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// @ts-ignore
import chalkTable from 'chalk-table'
import colors from 'yoctocolors-cjs'

import { logger } from '@socketsecurity/registry/lib/logger'

import type { SocketSdkReturnType } from '@socketsecurity/sdk'

export async function outputDependencies(
data: SocketSdkReturnType<'searchDependencies'>['data'],
{
limit,
offset,
outputKind
}: {
limit: number
offset: number
outputKind: 'json' | 'markdown' | 'text'
}
): Promise<void> {
if (outputKind === 'json') {
let json
try {
json = JSON.stringify(data, null, 2)
} catch (e) {
process.exitCode = 1
logger.fail(
'There was a problem converting the data to JSON, please try without the `--json` flag'
)
return
}

logger.log(json)
return
}

logger.log(
'Request details: Offset:',
offset,
', limit:',
limit,
', is there more data after this?',
data.end ? 'no' : 'yes'
)

const options = {
columns: [
{ field: 'namespace', name: colors.cyan('Namespace') },
{ field: 'name', name: colors.cyan('Name') },
{ field: 'version', name: colors.cyan('Version') },
{ field: 'repository', name: colors.cyan('Repository') },
{ field: 'branch', name: colors.cyan('Branch') },
{ field: 'type', name: colors.cyan('Type') },
{ field: 'direct', name: colors.cyan('Direct') }
]
}

logger.log(chalkTable(options, data.rows))
}
Loading