Skip to content

Commit ddd999b

Browse files
committed
Cleanup scan, merge report stuff, hide report command
1 parent 51de259 commit ddd999b

23 files changed

+459
-282
lines changed

src/commands/audit-log/get-audit-log.ts

Lines changed: 2 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { SocketSdkReturnType } from '@socketsecurity/sdk'
77
import constants from '../../constants'
88
import { handleApiCall, handleUnsuccessfulApiResponse } from '../../utils/api'
99
import { AuthError } from '../../utils/errors'
10+
import { mdTable } from '../../utils/markdown'
1011
import { getDefaultToken, setupSdk } from '../../utils/sdk'
1112

1213
import type { Choice } from '@socketsecurity/registry/lib/prompts'
@@ -111,7 +112,7 @@ async function outputAsMarkdown(
111112
perPage: number
112113
): Promise<void> {
113114
try {
114-
const table = mdTable(auditLogs, [
115+
const table = mdTable<any>(auditLogs, [
115116
'event_id',
116117
'created_at',
117118
'type',
@@ -144,46 +145,6 @@ ${table}
144145
}
145146
}
146147

147-
function mdTable<
148-
T extends SocketSdkReturnType<'getAuditLogEvents'>['data']['results']
149-
>(
150-
logs: T,
151-
// This is saying "an array of strings and the strings are a valid key of elements of T"
152-
// In turn, T is defined above as the audit log event type from our OpenAPI docs.
153-
cols: Array<string & keyof T[number]>
154-
): string {
155-
// Max col width required to fit all data in that column
156-
const cws = cols.map(col => col.length)
157-
158-
for (const log of logs) {
159-
for (let i = 0; i < cols.length; ++i) {
160-
// @ts-ignore
161-
const val: unknown = log[cols[i] ?? ''] ?? ''
162-
cws[i] = Math.max(cws[i] ?? 0, String(val).length)
163-
}
164-
}
165-
166-
let div = '|'
167-
for (const cw of cws) div += ' ' + '-'.repeat(cw) + ' |'
168-
169-
let header = '|'
170-
for (let i = 0; i < cols.length; ++i)
171-
header += ' ' + String(cols[i]).padEnd(cws[i] ?? 0, ' ') + ' |'
172-
173-
let body = ''
174-
for (const log of logs) {
175-
body += '|'
176-
for (let i = 0; i < cols.length; ++i) {
177-
// @ts-ignore
178-
const val: unknown = log[cols[i] ?? ''] ?? ''
179-
body += ' ' + String(val).padEnd(cws[i] ?? 0, ' ') + ' |'
180-
}
181-
body += '\n'
182-
}
183-
184-
return [div, header, div, body.trim(), div].filter(s => !!s.trim()).join('\n')
185-
}
186-
187148
async function outputAsPrint(
188149
auditLogs: SocketSdkReturnType<'getAuditLogEvents'>['data']['results'],
189150
orgSlug: string,

src/commands/raw-npm/cmd-raw-npm.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { logger } from '@socketsecurity/registry/lib/logger'
33
import { runRawNpm } from './run-raw-npm'
44
import constants from '../../constants'
55
import { meowOrExit } from '../../utils/meow-with-subcommands'
6-
import { getFlagListOutput } from '../../utils/output-formatting'
76

87
import type { CliCommandConfig } from '../../utils/meow-with-subcommands'
98

@@ -14,13 +13,10 @@ const config: CliCommandConfig = {
1413
description: `Temporarily disable the Socket ${NPM} wrapper`,
1514
hidden: false,
1615
flags: {},
17-
help: (command, config) => `
16+
help: command => `
1817
Usage
1918
$ ${command} <command>
2019
21-
Options
22-
${getFlagListOutput(config.flags, 6)}
23-
2420
Examples
2521
$ ${command} install
2622
`

src/commands/raw-npx/cmd-raw-npx.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { logger } from '@socketsecurity/registry/lib/logger'
33
import { runRawNpx } from './run-raw-npx'
44
import constants from '../../constants'
55
import { meowOrExit } from '../../utils/meow-with-subcommands'
6-
import { getFlagListOutput } from '../../utils/output-formatting'
76

87
import type { CliCommandConfig } from '../../utils/meow-with-subcommands'
98

@@ -14,13 +13,10 @@ const config: CliCommandConfig = {
1413
description: `Temporarily disable the Socket ${NPX} wrapper`,
1514
hidden: false,
1615
flags: {},
17-
help: (command, config) => `
16+
help: command => `
1817
Usage
1918
$ ${command} <command>
2019
21-
Options
22-
${getFlagListOutput(config.flags, 6)}
23-
2420
Examples
2521
$ ${command} install
2622
`

src/commands/report/cmd-report-create.ts

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,14 @@ import constants from '../../constants'
1010
import { commonFlags, outputFlags, validationFlags } from '../../flags'
1111
import { ColorOrMarkdown } from '../../utils/color-or-markdown'
1212
import { meowOrExit } from '../../utils/meow-with-subcommands'
13-
import { getFlagListOutput } from '../../utils/output-formatting'
1413

1514
import type { CliCommandConfig } from '../../utils/meow-with-subcommands'
1615

1716
const { DRY_RUN_BAIL_TEXT } = constants
1817

1918
const config: CliCommandConfig = {
2019
commandName: 'create',
21-
description: 'Create a project report',
20+
description: '[Deprecated] Create a project report',
2221
hidden: false,
2322
flags: {
2423
...commonFlags,
@@ -36,27 +35,9 @@ const config: CliCommandConfig = {
3635
description: 'Will wait for and return the created report'
3736
}
3837
},
39-
help: (command, config) => `
40-
Usage
41-
$ ${command} <paths-to-package-folders-and-files>
42-
43-
Uploads the specified "package.json" and lock files for JavaScript, Python, and Go dependency manifests.
44-
If any folder is specified, the ones found in there recursively are uploaded.
45-
46-
Supports globbing such as "**/package.json", "**/requirements.txt", "**/pyproject.toml", and "**/go.mod".
47-
48-
Ignores any file specified in your project's ".gitignore", your project's
49-
"socket.yml" file's "projectIgnorePaths" and also has a sensible set of
50-
default ignores from the "ignore-by-default" module.
51-
52-
Options
53-
${getFlagListOutput(config.flags, 6)}
54-
55-
Examples
56-
$ ${command} .
57-
$ ${command} '**/package.json'
58-
$ ${command} /path/to/a/package.json /path/to/another/package.json
59-
$ ${command} . --view --json
38+
help: () => `
39+
This command is deprecated in favor of \`socket scan create\`.
40+
It will be removed in the next major release of the CLI.
6041
`
6142
}
6243

src/commands/report/cmd-report-view.ts

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,30 +7,23 @@ import { viewReport } from './view-report'
77
import constants from '../../constants'
88
import { commonFlags, outputFlags, validationFlags } from '../../flags'
99
import { meowOrExit } from '../../utils/meow-with-subcommands'
10-
import { getFlagListOutput } from '../../utils/output-formatting'
1110

1211
import type { CliCommandConfig } from '../../utils/meow-with-subcommands'
1312

1413
const { DRY_RUN_BAIL_TEXT } = constants
1514

1615
const config: CliCommandConfig = {
1716
commandName: 'view',
18-
description: 'View a project report',
17+
description: '[Deprecated] View a project report',
1918
hidden: false,
2019
flags: {
2120
...commonFlags,
2221
...outputFlags,
2322
...validationFlags
2423
},
25-
help: (command, config) => `
26-
Usage
27-
$ ${command} <report-identifier>
28-
29-
Options
30-
${getFlagListOutput(config.flags, 6)}
31-
32-
Examples
33-
$ ${command} QXU8PmK7LfH608RAwfIKdbcHgwEd_ZeWJ9QEGv05FJUQ
24+
help: () => `
25+
This command is deprecated in favor of \`socket scan view\`.
26+
It will be removed in the next major release of the CLI.
3427
`
3528
}
3629

src/commands/report/cmd-report.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const description = '[Deprecated] Project report related commands'
88

99
export const cmdReport: CliSubcommand = {
1010
description,
11+
hidden: true, // Deprecated in favor of `scan`
1112
async run(argv, importMeta, { parentName }) {
1213
await meowWithSubcommands(
1314
{

src/commands/report/create-report.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { pluralize } from '@socketsecurity/registry/lib/words'
33

44
import constants from '../../constants'
55
import { handleApiCall, handleUnsuccessfulApiResponse } from '../../utils/api'
6-
import { getPackageFiles } from '../../utils/path-resolve'
6+
import { getPackageFilesFullScans } from '../../utils/path-resolve'
77
import { setupSdk } from '../../utils/sdk'
88

99
import type { SocketYml } from '@socketsecurity/config'
@@ -40,13 +40,13 @@ export async function createReport(
4040
cause
4141
})
4242
})
43-
const packagePaths = await getPackageFiles(
43+
const packagePaths = await getPackageFilesFullScans(
4444
cwd,
4545
inputPaths,
46-
socketConfig,
47-
supportedFiles
46+
supportedFiles,
47+
socketConfig
4848
)
49-
const { length: packagePathsCount } = packagePaths
49+
const packagePathsCount = packagePaths.length
5050
if (packagePathsCount && isDebug()) {
5151
for (const pkgPath of packagePaths) {
5252
debugLog(`Uploading: ${pkgPath}`)

src/commands/scan/cmd-scan-create.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,30 @@ const config: CliCommandConfig = {
8888
default: false,
8989
description:
9090
'Set the visibility (true/false) of the scan in your dashboard'
91+
},
92+
view: {
93+
type: 'boolean',
94+
shortFlag: 'v',
95+
default: true,
96+
description:
97+
'Will wait for and return the created report. Use --no-view to disable.'
9198
}
9299
},
100+
// TODO: your project's "socket.yml" file's "projectIgnorePaths"
93101
help: (command, config) => `
94102
Usage
95103
$ ${command} [...options] <org> <TARGET> [TARGET...]
96104
97-
Where TARGET is a FILE or DIR that _must_ be inside the CWD.
105+
Uploads the specified "package.json" and lock files for JavaScript, Python,
106+
Go, Scala, Gradle, and Kotlin dependency manifests.
107+
If any folder is specified, the ones found in there recursively are uploaded.
108+
109+
Supports globbing such as "**/package.json", "**/requirements.txt", etc.
110+
111+
Ignores any file specified in your project's ".gitignore" and also has a
112+
sensible set of default ignores from the "ignore-by-default" module.
113+
114+
TARGET should be a FILE or DIR that _must_ be inside the CWD.
98115
99116
When a FILE is given only that FILE is targeted. Otherwise any eligible
100117
files in the given DIR will be considered.
@@ -134,7 +151,7 @@ async function run(
134151

135152
let { branch: branchName, repo: repoName } = cli.flags
136153

137-
const apiToken = getDefaultToken()
154+
const apiToken = getDefaultToken() // This checks if we _can_ suggest anything
138155

139156
if (!apiToken && (!orgSlug || !repoName || !branchName || !targets.length)) {
140157
// Without api token we cannot recover because we can't request more info

src/commands/scan/cmd-scan-del.ts

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,8 @@ import { logger } from '@socketsecurity/registry/lib/logger'
66
import { deleteOrgFullScan } from './delete-full-scan'
77
import constants from '../../constants'
88
import { commonFlags, outputFlags } from '../../flags'
9-
import { AuthError } from '../../utils/errors'
109
import { meowOrExit } from '../../utils/meow-with-subcommands'
1110
import { getFlagListOutput } from '../../utils/output-formatting'
12-
import { getDefaultToken } from '../../utils/sdk'
1311

1412
import type { CliCommandConfig } from '../../utils/meow-with-subcommands'
1513

@@ -75,12 +73,5 @@ async function run(
7573
return
7674
}
7775

78-
const apiToken = getDefaultToken()
79-
if (!apiToken) {
80-
throw new AuthError(
81-
'User must be authenticated to run this command. To log in, run the command `socket login` and enter your API key.'
82-
)
83-
}
84-
85-
await deleteOrgFullScan(orgSlug, fullScanId, apiToken)
76+
await deleteOrgFullScan(orgSlug, fullScanId)
8677
}

src/commands/scan/cmd-scan-list.ts

Lines changed: 12 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,8 @@ import { logger } from '@socketsecurity/registry/lib/logger'
66
import { listFullScans } from './list-full-scans'
77
import constants from '../../constants'
88
import { commonFlags, outputFlags } from '../../flags'
9-
import { AuthError } from '../../utils/errors'
109
import { meowOrExit } from '../../utils/meow-with-subcommands'
1110
import { getFlagListOutput } from '../../utils/output-formatting'
12-
import { getDefaultToken } from '../../utils/sdk'
1311

1412
import type {
1513
CliCommandConfig,
@@ -113,37 +111,17 @@ async function run(
113111
return
114112
}
115113

116-
const apiToken = getDefaultToken()
117-
if (!apiToken) {
118-
throw new AuthError(
119-
'User must be authenticated to run this command. To log in, run the command `socket login` and enter your API key.'
120-
)
121-
}
122-
123-
await listFullScans(
114+
await listFullScans({
115+
direction: String(cli.flags['direction'] || ''),
116+
from_time: String(cli.flags['fromTime'] || ''),
124117
orgSlug,
125-
// TODO: refine this object to what we need
126-
{
127-
outputJson: cli.flags['json'],
128-
outputMarkdown: cli.flags['markdown'],
129-
orgSlug,
130-
sort: cli.flags['sort'],
131-
direction: cli.flags['direction'],
132-
per_page: cli.flags['perPage'],
133-
page: cli.flags['page'],
134-
from_time: cli.flags['fromTime'],
135-
until_time: cli.flags['untilTime']
136-
} as {
137-
outputJson: boolean
138-
outputMarkdown: boolean
139-
orgSlug: string
140-
sort: string
141-
direction: string
142-
per_page: number
143-
page: number
144-
from_time: string
145-
until_time: string
146-
},
147-
apiToken
148-
)
118+
outputKind: cli.flags['json']
119+
? 'json'
120+
: cli.flags['markdown']
121+
? 'markdown'
122+
: 'print',
123+
page: Number(cli.flags['page'] || 1),
124+
per_page: Number(cli.flags['perPage'] || 30),
125+
sort: String(cli.flags['sort'] || '')
126+
})
149127
}

0 commit comments

Comments
 (0)