|
| 1 | +/* eslint-disable no-console */ |
| 2 | + |
| 3 | +import meow from 'meow' |
| 4 | +import ora from 'ora' |
| 5 | + |
| 6 | +import { handleApiCall, handleUnsuccessfulApiResponse } from '../../utils/api-helpers.js' |
| 7 | +import { getDefaultKey, setupSdk } from '../../utils/sdk.js' |
| 8 | + |
| 9 | +/** @type {import('../../utils/meow-with-subcommands').CliSubcommand} */ |
| 10 | +export const organizations = { |
| 11 | + description: 'List organizations', |
| 12 | + async run (argv, importMeta, { parentName }) { |
| 13 | + const name = parentName + ' organizations' |
| 14 | + |
| 15 | + setupCommand(name, organizations.description, argv, importMeta) |
| 16 | + await fetchOrganizations() |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +// Internal functions |
| 21 | + |
| 22 | +/** |
| 23 | + * @param {string} name |
| 24 | + * @param {string} description |
| 25 | + * @param {readonly string[]} argv |
| 26 | + * @param {ImportMeta} importMeta |
| 27 | + * @returns {void} |
| 28 | + */ |
| 29 | +function setupCommand (name, description, argv, importMeta) { |
| 30 | + meow(` |
| 31 | + Usage |
| 32 | + $ ${name} |
| 33 | + `, { |
| 34 | + argv, |
| 35 | + description, |
| 36 | + importMeta |
| 37 | + }) |
| 38 | +} |
| 39 | + |
| 40 | +/** |
| 41 | + * @typedef OrganizationsData |
| 42 | + * @property {import('@socketsecurity/sdk').SocketSdkReturnType<'getOrganizations'>["data"]} data |
| 43 | + */ |
| 44 | + |
| 45 | +/** |
| 46 | + * @returns {Promise<void|OrganizationsData>} |
| 47 | + */ |
| 48 | +async function fetchOrganizations () { |
| 49 | + const socketSdk = await setupSdk(getDefaultKey()) |
| 50 | + const spinner = ora('Fetching organizations...').start() |
| 51 | + |
| 52 | + const result = await handleApiCall(socketSdk.getOrganizations(), 'looking up organizations') |
| 53 | + |
| 54 | + if (result.success === false) { |
| 55 | + return handleUnsuccessfulApiResponse('getOrganizations', result, spinner) |
| 56 | + } |
| 57 | + |
| 58 | + spinner.stop() |
| 59 | + |
| 60 | + const organizations = Object.values(result.data.organizations) |
| 61 | + console.log('List of organizations:') |
| 62 | + organizations.map(o => { |
| 63 | + console.log(` |
| 64 | +Name: ${o?.name} |
| 65 | +ID: ${o?.id} |
| 66 | +Plan: ${o?.plan} |
| 67 | + `) |
| 68 | + return o |
| 69 | + }) |
| 70 | + |
| 71 | + return { |
| 72 | + data: result.data |
| 73 | + } |
| 74 | +} |
0 commit comments