|
| 1 | +import meow from 'meow' |
| 2 | +import ora from 'ora' |
| 3 | + |
| 4 | +import { outputFlags } from '../flags' |
| 5 | +import { printFlagList } from '../utils/formatting' |
| 6 | +import { getDefaultKey } from '../utils/sdk' |
| 7 | + |
| 8 | +import type { CliSubcommand } from '../utils/meow-with-subcommands' |
| 9 | +import type { Ora } from 'ora' |
| 10 | +import { AuthError } from '../utils/errors' |
| 11 | + |
| 12 | +export const threatFeed: CliSubcommand = { |
| 13 | + description: 'Look up the threat feed', |
| 14 | + async run(argv, importMeta, { parentName }) { |
| 15 | + const name = parentName + ' threat-feed' |
| 16 | + |
| 17 | + const input = setupCommand(name, threatFeed.description, argv, importMeta) |
| 18 | + if (input) { |
| 19 | + const apiKey = getDefaultKey() |
| 20 | + if(!apiKey){ |
| 21 | + throw new AuthError("User must be authenticated to run this command. To log in, run the command `socket login` and enter your API key.") |
| 22 | + } |
| 23 | + const spinner = ora(`Looking up the threat feed \n`).start() |
| 24 | + await fetchThreatFeed(input, spinner) |
| 25 | + } |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +// Internal functions |
| 30 | + |
| 31 | +type CommandContext = { |
| 32 | + outputJson: boolean |
| 33 | + outputMarkdown: boolean |
| 34 | +} |
| 35 | + |
| 36 | +function setupCommand( |
| 37 | + name: string, |
| 38 | + description: string, |
| 39 | + argv: readonly string[], |
| 40 | + importMeta: ImportMeta |
| 41 | +): CommandContext | undefined { |
| 42 | + const flags: { [key: string]: any } = { |
| 43 | + ...outputFlags |
| 44 | + } |
| 45 | + |
| 46 | + const cli = meow( |
| 47 | + ` |
| 48 | + Usage |
| 49 | + $ ${name} <org slug> |
| 50 | +
|
| 51 | + Options |
| 52 | + ${printFlagList(flags, 6)} |
| 53 | +
|
| 54 | + Examples |
| 55 | + $ ${name} FakeOrg |
| 56 | + `, |
| 57 | + { |
| 58 | + argv, |
| 59 | + description, |
| 60 | + importMeta, |
| 61 | + flags |
| 62 | + } |
| 63 | + ) |
| 64 | + |
| 65 | + const { |
| 66 | + json: outputJson, |
| 67 | + markdown: outputMarkdown, |
| 68 | + } = cli.flags |
| 69 | + |
| 70 | + return <CommandContext>{ |
| 71 | + outputJson, |
| 72 | + outputMarkdown |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +async function fetchThreatFeed( |
| 77 | + input: CommandContext, |
| 78 | + spinner: Ora |
| 79 | +): Promise<void> { |
| 80 | +// const socketSdk = await setupSdk(apiKey) |
| 81 | + spinner.stop() |
| 82 | + console.log(input) |
| 83 | +} |
0 commit comments