|
| 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 | +} |
0 commit comments