Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add command to list organizations #114

Merged
merged 4 commits into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/commands/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
81 changes: 81 additions & 0 deletions lib/commands/organizations/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/* eslint-disable no-console */

import chalk from 'chalk'
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.js').CliSubcommand} */
export const organizations = {
description: 'List organizations associated with the API key used',
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 apiKey = getDefaultKey()
const socketSdk = await setupSdk(apiKey)
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)
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}
ID: ${o?.id}
Plan: ${o?.plan}
`)
return o
})

return {
data: result.data
}
}
Loading