-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathoutput-patch-download-result.mts
More file actions
86 lines (74 loc) · 2.19 KB
/
output-patch-download-result.mts
File metadata and controls
86 lines (74 loc) · 2.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/**
* Output formatter for patch download results.
*
* Displays download status for patches retrieved from Socket API.
* Supports JSON and text output formats.
*
* Features:
* - Summary of downloaded patches
* - Summary of failed patches
* - Error details for failures
* - JSON output for automation
*/
import { getDefaultLogger } from '@socketsecurity/lib/logger'
import { pluralize } from '@socketsecurity/lib/words'
import { OUTPUT_JSON } from '../../constants/cli.mts'
import { failMsgWithBadge } from '../../utils/error/fail-msg-with-badge.mts'
import { serializeResultJson } from '../../utils/output/result-json.mjs'
import type { PatchDownloadResult } from './handle-patch-download.mts'
import type { CResult, OutputKind } from '../../types.mts'
const logger = getDefaultLogger()
type OutputOptions = {
outputKind: OutputKind
}
/**
* Output patch download results.
*/
export async function outputPatchDownloadResult(
result: CResult<PatchDownloadResult>,
{ outputKind }: OutputOptions,
): Promise<void> {
if (!result.ok) {
process.exitCode = result.code ?? 1
}
if (outputKind === OUTPUT_JSON) {
logger.log(serializeResultJson(result))
return
}
if (!result.ok) {
logger.fail(failMsgWithBadge(result.message, result.cause))
return
}
const { downloaded, failed } = result.data
logger.log('')
// Show downloaded patches.
if (downloaded.length) {
logger.group(
`Successfully downloaded ${downloaded.length} ${pluralize('patch', { count: downloaded.length })}:`,
)
for (const patch of downloaded) {
logger.success(`${patch.purl} (${patch.uuid})`)
}
logger.groupEnd()
}
// Show failed patches.
if (failed.length) {
logger.log('')
logger.group(
`Failed to download ${failed.length} ${pluralize('patch', { count: failed.length })}:`,
)
for (const failure of failed) {
logger.error(`${failure.uuid}: ${failure.error}`)
}
logger.groupEnd()
}
// Summary.
logger.log('')
if (failed.length) {
logger.warn(
`Patch download completed with ${failed.length} ${pluralize('failure', { count: failed.length })}`,
)
} else {
logger.success('All patches downloaded successfully!')
}
}