Skip to content

Commit 6bbc8f5

Browse files
authored
Apply handle pattern to dependencies (#374)
1 parent 069fcc2 commit 6bbc8f5

File tree

5 files changed

+141
-79
lines changed

5 files changed

+141
-79
lines changed

src/commands/dependencies/cmd-dependencies.ts

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { logger } from '@socketsecurity/registry/lib/logger'
22

3-
import { findDependencies } from './find-dependencies'
3+
import { handleDependencies } from './handle-dependencies'
44
import constants from '../../constants'
55
import { commonFlags, outputFlags } from '../../flags'
66
import { meowOrExit } from '../../utils/meow-with-subcommands'
@@ -61,15 +61,16 @@ async function run(
6161
parentName
6262
})
6363

64+
const { json, limit, markdown, offset } = cli.flags
65+
6466
if (cli.flags['dryRun']) {
6567
logger.log(DRY_RUN_BAIL_TEXT)
6668
return
6769
}
6870

69-
// TODO: markdown flag is ignored
70-
await findDependencies({
71-
limit: Number(cli.flags['limit'] || 0) || 0,
72-
offset: Number(cli.flags['offset'] || 0) || 0,
73-
outputJson: Boolean(cli.flags['json'])
71+
await handleDependencies({
72+
limit: Number(limit || 0) || 0,
73+
offset: Number(offset || 0) || 0,
74+
outputKind: json ? 'json' : markdown ? 'markdown' : 'text'
7475
})
7576
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import constants from '../../constants'
2+
import { handleApiCall, handleUnsuccessfulApiResponse } from '../../utils/api'
3+
import { AuthError } from '../../utils/errors'
4+
import { getDefaultToken, setupSdk } from '../../utils/sdk'
5+
6+
import type { SocketSdkReturnType } from '@socketsecurity/sdk'
7+
8+
export async function fetchDependencies({
9+
limit,
10+
offset
11+
}: {
12+
limit: number
13+
offset: number
14+
}): Promise<SocketSdkReturnType<'searchDependencies'>['data'] | undefined> {
15+
const apiToken = getDefaultToken()
16+
if (!apiToken) {
17+
throw new AuthError(
18+
'User must be authenticated to run this command. To log in, run the command `socket login` and enter your API key.'
19+
)
20+
}
21+
22+
return await fetchDependenciesWithToken(apiToken, {
23+
limit,
24+
offset
25+
})
26+
}
27+
28+
async function fetchDependenciesWithToken(
29+
apiToken: string,
30+
{
31+
limit,
32+
offset
33+
}: {
34+
limit: number
35+
offset: number
36+
}
37+
): Promise<SocketSdkReturnType<'searchDependencies'>['data'] | undefined> {
38+
// Lazily access constants.spinner.
39+
const { spinner } = constants
40+
41+
spinner.start('Fetching organization dependencies...')
42+
43+
const socketSdk = await setupSdk(apiToken)
44+
45+
const result = await handleApiCall(
46+
socketSdk.searchDependencies({ limit, offset }),
47+
'Searching dependencies'
48+
)
49+
50+
spinner?.successAndStop('Received organization dependencies response.')
51+
52+
if (!result.success) {
53+
handleUnsuccessfulApiResponse('searchDependencies', result)
54+
return
55+
}
56+
57+
return result.data
58+
}

src/commands/dependencies/find-dependencies.ts

-73
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { fetchDependencies } from './fetch-dependencies'
2+
import { outputDependencies } from './output-dependencies'
3+
4+
export async function handleDependencies({
5+
limit,
6+
offset,
7+
outputKind
8+
}: {
9+
limit: number
10+
offset: number
11+
outputKind: 'json' | 'markdown' | 'text'
12+
}): Promise<void> {
13+
const data = await fetchDependencies({ limit, offset })
14+
if (!data) return
15+
16+
await outputDependencies(data, { limit, offset, outputKind })
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// @ts-ignore
2+
import chalkTable from 'chalk-table'
3+
import colors from 'yoctocolors-cjs'
4+
5+
import { logger } from '@socketsecurity/registry/lib/logger'
6+
7+
import type { SocketSdkReturnType } from '@socketsecurity/sdk'
8+
9+
export async function outputDependencies(
10+
data: SocketSdkReturnType<'searchDependencies'>['data'],
11+
{
12+
limit,
13+
offset,
14+
outputKind
15+
}: {
16+
limit: number
17+
offset: number
18+
outputKind: 'json' | 'markdown' | 'text'
19+
}
20+
): Promise<void> {
21+
if (outputKind === 'json') {
22+
let json
23+
try {
24+
json = JSON.stringify(data, null, 2)
25+
} catch (e) {
26+
process.exitCode = 1
27+
logger.fail(
28+
'There was a problem converting the data to JSON, please try without the `--json` flag'
29+
)
30+
return
31+
}
32+
33+
logger.log(json)
34+
return
35+
}
36+
37+
logger.log(
38+
'Request details: Offset:',
39+
offset,
40+
', limit:',
41+
limit,
42+
', is there more data after this?',
43+
data.end ? 'no' : 'yes'
44+
)
45+
46+
const options = {
47+
columns: [
48+
{ field: 'namespace', name: colors.cyan('Namespace') },
49+
{ field: 'name', name: colors.cyan('Name') },
50+
{ field: 'version', name: colors.cyan('Version') },
51+
{ field: 'repository', name: colors.cyan('Repository') },
52+
{ field: 'branch', name: colors.cyan('Branch') },
53+
{ field: 'type', name: colors.cyan('Type') },
54+
{ field: 'direct', name: colors.cyan('Direct') }
55+
]
56+
}
57+
58+
logger.log(chalkTable(options, data.rows))
59+
}

0 commit comments

Comments
 (0)