From e2222ace552d793a87285bb3949dafecf08c7040 Mon Sep 17 00:00:00 2001 From: Charlie Gerard Date: Thu, 30 May 2024 16:16:50 -0700 Subject: [PATCH 1/4] reset --- lib/commands/organizations/index.js | 74 +++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 lib/commands/organizations/index.js diff --git a/lib/commands/organizations/index.js b/lib/commands/organizations/index.js new file mode 100644 index 00000000..c18e8bad --- /dev/null +++ b/lib/commands/organizations/index.js @@ -0,0 +1,74 @@ +/* eslint-disable no-console */ + +import meow from 'meow' +import ora from 'ora' + +import { handleApiCall, handleUnsuccessfulApiResponse } from '../../utils/api-helpers.js' +import { 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 socketSdk = await setupSdk(getDefaultKey()) + const spinner = ora('Fetching organizations...').start() + + const result = await handleApiCall(socketSdk.getOrganizations(), 'looking up organizations') + + 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 o + }) + + return { + data: result.data + } +} From ba1a96bd5d77c97c3b15c9fe86662ee8c8866f6e Mon Sep 17 00:00:00 2001 From: Charlie Gerard Date: Thu, 30 May 2024 16:45:50 -0700 Subject: [PATCH 2/4] update --- lib/commands/organizations/index.js | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/lib/commands/organizations/index.js b/lib/commands/organizations/index.js index c18e8bad..5a475db0 100644 --- a/lib/commands/organizations/index.js +++ b/lib/commands/organizations/index.js @@ -1,5 +1,6 @@ /* eslint-disable no-console */ +import chalk from 'chalk' import meow from 'meow' import ora from 'ora' @@ -46,7 +47,8 @@ function setupCommand (name, description, argv, importMeta) { * @returns {Promise} */ async function fetchOrganizations () { - const socketSdk = await setupSdk(getDefaultKey()) + const apiKey = getDefaultKey() + const socketSdk = await setupSdk(apiKey) const spinner = ora('Fetching organizations...').start() const result = await handleApiCall(socketSdk.getOrganizations(), 'looking up organizations') @@ -58,7 +60,12 @@ async function fetchOrganizations () { spinner.stop() const organizations = Object.values(result.data.organizations) - console.log('List of organizations:') + if (apiKey) { + console.log(`List of organizations associated with your API key: ${chalk.italic(apiKey)}`) + } else { + console.log('List of organizations associated with your API key.') + } + organizations.map(o => { console.log(` Name: ${o?.name} From 281b2dc5977a96038e0130160d2d9be1cf1294a5 Mon Sep 17 00:00:00 2001 From: Charlie Gerard Date: Thu, 1 Aug 2024 10:42:11 -0700 Subject: [PATCH 3/4] fix merge conflict --- lib/commands/index.js | 1 + lib/commands/organizations/index.js | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/commands/index.js b/lib/commands/index.js index 749118a8..c60f4a0b 100644 --- a/lib/commands/index.js +++ b/lib/commands/index.js @@ -12,3 +12,4 @@ export * from './scan/index.js' export * from './audit-log/index.js' export * from './repos/index.js' export * from './dependencies/index.js' +export * from './organizations/index.js' diff --git a/lib/commands/organizations/index.js b/lib/commands/organizations/index.js index 5a475db0..6b24b2d6 100644 --- a/lib/commands/organizations/index.js +++ b/lib/commands/organizations/index.js @@ -7,7 +7,7 @@ import ora from 'ora' import { handleApiCall, handleUnsuccessfulApiResponse } from '../../utils/api-helpers.js' import { getDefaultKey, setupSdk } from '../../utils/sdk.js' -/** @type {import('../../utils/meow-with-subcommands').CliSubcommand} */ +/** @type {import('../../utils/meow-with-subcommands.js').CliSubcommand} */ export const organizations = { description: 'List organizations', async run (argv, importMeta, { parentName }) { From be8b7845d1e7da56a1d4f309121e1df27b2493cc Mon Sep 17 00:00:00 2001 From: Charlie Gerard Date: Thu, 1 Aug 2024 10:47:09 -0700 Subject: [PATCH 4/4] small description update --- lib/commands/organizations/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/commands/organizations/index.js b/lib/commands/organizations/index.js index 6b24b2d6..cdf18675 100644 --- a/lib/commands/organizations/index.js +++ b/lib/commands/organizations/index.js @@ -9,7 +9,7 @@ import { getDefaultKey, setupSdk } from '../../utils/sdk.js' /** @type {import('../../utils/meow-with-subcommands.js').CliSubcommand} */ export const organizations = { - description: 'List organizations', + description: 'List organizations associated with the API key used', async run (argv, importMeta, { parentName }) { const name = parentName + ' organizations'