-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c4f6c8c
commit 1f44210
Showing
2 changed files
with
73 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<void|OrganizationsData>} | ||
*/ | ||
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 | ||
} | ||
} |