diff --git a/lib/commands/index.js b/lib/commands/index.js index aae0d337..bdc4e5ee 100644 --- a/lib/commands/index.js +++ b/lib/commands/index.js @@ -1,4 +1,5 @@ export * from './info/index.js' +export * from './organizations/index.js' export * from './report/index.js' export * from './npm/index.js' export * from './npx/index.js' diff --git a/lib/commands/organizations/index.js b/lib/commands/organizations/index.js new file mode 100644 index 00000000..dc7522aa --- /dev/null +++ b/lib/commands/organizations/index.js @@ -0,0 +1,72 @@ +/* eslint-disable no-console */ + +import meow from 'meow' +import ora from 'ora' + +import { handleApiCall, handleUnsuccessfulApiResponse } from '../../utils/api-helpers.js' +import { FREE_API_KEY, getDefaultKey, setupSdk } from '../../utils/sdk.js' + +/** @type {import('../../utils/meow-with-subcommands').CliSubcommand} */ +export const organizations = { + description: 'List organizations', + async run (argv, importMeta, { parentName }) { + const name = parentName + ' organizations' + + setupCommand(name, organizations.description, argv, importMeta) + await fetchOrganizations() + } +} + +// Internal functions + +/** + * @param {string} name + * @param {string} description + * @param {readonly string[]} argv + * @param {ImportMeta} importMeta + * @returns {void} + */ +function setupCommand (name, description, argv, importMeta) { + meow(` + Usage + $ ${name} + `, { + argv, + description, + importMeta + }) +} + +/** + * @typedef OrganizationsData + * @property {import('@socketsecurity/sdk').SocketSdkReturnType<'getOrganizations'>["data"]} data + */ + +/** + * @returns {Promise} + */ +async function fetchOrganizations () { + const spinner = ora(`Fetching organizations...`).start() + const socketSdk = await setupSdk(getDefaultKey() || FREE_API_KEY) + const result = await handleApiCall(socketSdk.getOrganizations(), 'looking up package') + + if (result.success === false) { + return handleUnsuccessfulApiResponse('getOrganizations', result, spinner) + } + + spinner.stop() + + const organizations = Object.values(result.data.organizations) + console.log(`List of organizations:`) + organizations.map(o => { + console.log(` +Name: ${o?.name} +ID: ${o?.id} +Plan: ${o?.plan} + `) + }) + + return { + data: result.data + } +}