Skip to content

Commit 0484122

Browse files
committed
wip
1 parent 8de0359 commit 0484122

File tree

5 files changed

+153
-110
lines changed

5 files changed

+153
-110
lines changed

lib/commands/fullscans/create.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,13 @@ async function setupCommand (name, description, argv, importMeta) {
120120

121121
const cli = meow(`
122122
Usage
123-
$ ${name}
123+
$ ${name} [...options]
124124
125125
Options
126126
${printFlagList(flags, 6)}
127+
128+
Examples
129+
$ ${name} --org=FakeOrg --repo=test-repo --branch=main --tmp=true ./package.json
127130
`, {
128131
argv,
129132
description,

lib/commands/fullscans/list.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { getDefaultKey, setupSdk } from '../../utils/sdk.js'
1515

1616
/** @type {import('../../utils/meow-with-subcommands').CliSubcommand} */
1717
export const list = {
18-
description: 'List scans',
18+
description: 'List scans for an organization',
1919
async run (argv, importMeta, { parentName }) {
2020
const name = parentName + ' list'
2121

@@ -33,25 +33,25 @@ const listFullScanFlags = prepareFlags({
3333
type: 'string',
3434
shortFlag: 's',
3535
default: 'created_at',
36-
description: 'Sorting option - name or created_at',
36+
description: 'Sorting option (`name` or `created_at`) - default is `created_at`',
3737
},
3838
direction: {
3939
type: 'string',
4040
shortFlag: 'd',
4141
default: 'desc',
42-
description: 'Direction option - desc or asc',
42+
description: 'Direction option (`desc` or `asc`) - Default is `desc`',
4343
},
4444
perPage: {
4545
type: 'number',
4646
shortFlag: 'pp',
4747
default: 30,
48-
description: 'Results per page',
48+
description: 'Results per page - Default is 30',
4949
},
5050
page: {
5151
type: 'number',
5252
shortFlag: 'p',
5353
default: 1,
54-
description: 'Page number',
54+
description: 'Page number - Default is 1',
5555
},
5656
fromTime: {
5757
type: 'string',
@@ -123,7 +123,10 @@ function setupCommand (name, description, argv, importMeta) {
123123
} = cli.flags
124124

125125
if (!cli.input[0]) {
126-
throw new InputError('Please specify an organization slug.')
126+
throw new InputError(`Please specify an organization slug. \n
127+
Example:
128+
socket scan list FakeOrg
129+
`)
127130
}
128131

129132
const orgSlug = cli.input[0] || ''

lib/commands/fullscans/metadata.js

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
/* eslint-disable no-console */
2+
3+
import chalk from 'chalk'
4+
// @ts-ignore
5+
import chalkTable from 'chalk-table'
6+
import meow from 'meow'
7+
import ora from 'ora'
8+
9+
import { outputFlags } from '../../flags/index.js'
10+
import { handleApiCall, handleUnsuccessfulApiResponse } from '../../utils/api-helpers.js'
11+
import { InputError } from '../../utils/errors.js'
12+
import { printFlagList } from '../../utils/formatting.js'
13+
import { getDefaultKey, setupSdk } from '../../utils/sdk.js'
14+
15+
/** @type {import('../../utils/meow-with-subcommands').CliSubcommand} */
16+
export const metadata = {
17+
description: 'Get a scan\'s metadata',
18+
async run (argv, importMeta, { parentName }) {
19+
const name = parentName + ' metadata'
20+
21+
const input = setupCommand(name, metadata.description, argv, importMeta)
22+
if (input) {
23+
const spinnerText = 'Getting scan\'s metadata... \n'
24+
const spinner = ora(spinnerText).start()
25+
await getOrgScanMetadata(input.orgSlug, input.scanID, spinner)
26+
}
27+
}
28+
}
29+
30+
// Internal functions
31+
32+
/**
33+
* @typedef CommandContext
34+
* @property {boolean} outputJson
35+
* @property {boolean} outputMarkdown
36+
* @property {string} orgSlug
37+
* @property {string} scanID
38+
*/
39+
40+
/**
41+
* @param {string} name
42+
* @param {string} description
43+
* @param {readonly string[]} argv
44+
* @param {ImportMeta} importMeta
45+
* @returns {void|CommandContext}
46+
*/
47+
function setupCommand (name, description, argv, importMeta) {
48+
const flags = {
49+
...outputFlags,
50+
}
51+
52+
const cli = meow(`
53+
Usage
54+
$ ${name} <org slug> <scan id>
55+
56+
Options
57+
${printFlagList(flags, 6)}
58+
59+
Examples
60+
$ ${name} FakeOrg 000aaaa1-0000-0a0a-00a0-00a0000000a0
61+
`, {
62+
argv,
63+
description,
64+
importMeta,
65+
flags
66+
})
67+
68+
const {
69+
json: outputJson,
70+
markdown: outputMarkdown,
71+
} = cli.flags
72+
73+
if (cli.input.length < 2) {
74+
throw new InputError(`Please specify an organization slug and a scan id. \n
75+
Example:
76+
socket scan metadata FakeOrg 000aaaa1-0000-0a0a-00a0-00a0000000a0
77+
`)
78+
}
79+
80+
const [orgSlug = '', scanID = ''] = cli.input
81+
82+
return {
83+
outputJson,
84+
outputMarkdown,
85+
orgSlug,
86+
scanID
87+
}
88+
}
89+
90+
/**
91+
* @typedef FullScansData
92+
* @property {import('@socketsecurity/sdk').SocketSdkReturnType<'getOrgFullScanMetadata'>["data"]} data
93+
*/
94+
95+
/**
96+
* @param {string} orgSlug
97+
* @param {string} scanId
98+
* @param {import('ora').Ora} spinner
99+
* @returns {Promise<void|FullScansData>}
100+
*/
101+
async function getOrgScanMetadata (orgSlug, scanId, spinner) {
102+
const socketSdk = await setupSdk(getDefaultKey())
103+
const result = await handleApiCall(socketSdk.getOrgFullScanMetadata(orgSlug, scanId), 'Listing scans')
104+
105+
if (!result.success) {
106+
return handleUnsuccessfulApiResponse('getOrgFullScanMetadata', result, spinner)
107+
}
108+
spinner.stop()
109+
110+
console.log(`\n Listing scans for: ${orgSlug} \n`)
111+
112+
const options = {
113+
columns: [
114+
{ field: 'id', name: chalk.magenta('ID') },
115+
{ field: 'report_url', name: chalk.magenta('Scan URL') },
116+
{ field: 'branch', name: chalk.magenta('Branch') },
117+
{ field: 'created_at', name: chalk.magenta('Created at') }
118+
]
119+
}
120+
121+
// @ts-ignore
122+
const formattedResults = result.data.results.map(d => {
123+
return {
124+
id: d.id,
125+
report_url: chalk.underline(`${d.html_report_url}`),
126+
created_at: d.created_at ? new Date(d.created_at).toLocaleDateString('en-us', { year: 'numeric', month: 'short', day: 'numeric' }) : '',
127+
branch: d.branch
128+
}
129+
})
130+
131+
const table = chalkTable(options, formattedResults)
132+
133+
console.log(table, '\n')
134+
135+
return {
136+
data: result.data
137+
}
138+
}

lib/commands/fullscans/stream.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { getDefaultKey, setupSdk } from '../../utils/sdk.js'
1111

1212
/** @type {import('../../utils/meow-with-subcommands').CliSubcommand} */
1313
export const stream = {
14-
description: 'Stream a scan',
14+
description: 'Stream the output of a scan',
1515
async run (argv, importMeta, { parentName }) {
1616
const name = parentName + ' stream'
1717

@@ -49,7 +49,7 @@ function setupCommand (name, description, argv, importMeta) {
4949

5050
const cli = meow(`
5151
Usage
52-
$ ${name} <org slug> <scan ID> <file>
52+
$ ${name} <org slug> <scan ID> <path to output file>
5353
5454
Options
5555
${printFlagList(flags, 6)}

lib/utils/chalk-table.js

Lines changed: 0 additions & 101 deletions
This file was deleted.

0 commit comments

Comments
 (0)