Skip to content

Commit

Permalink
Add command to list organizations
Browse files Browse the repository at this point in the history
  • Loading branch information
charliegerard committed May 29, 2024
1 parent c4f6c8c commit 1f44210
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/commands/index.js
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
72 changes: 72 additions & 0 deletions lib/commands/organizations/index.js
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()

Check failure on line 49 in lib/commands/organizations/index.js

View workflow job for this annotation

GitHub Actions / Linting / Test (20, ubuntu-latest)

Strings must use singlequote
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:`)

Check failure on line 60 in lib/commands/organizations/index.js

View workflow job for this annotation

GitHub Actions / Linting / Test (20, ubuntu-latest)

Strings must use singlequote
organizations.map(o => {

Check failure on line 61 in lib/commands/organizations/index.js

View workflow job for this annotation

GitHub Actions / Linting / Test (20, ubuntu-latest)

Array.prototype.map() expects a return value from arrow function
console.log(`
Name: ${o?.name}
ID: ${o?.id}
Plan: ${o?.plan}
`)
})

Check failure on line 68 in lib/commands/organizations/index.js

View workflow job for this annotation

GitHub Actions / Linting / Test (20, ubuntu-latest)

Trailing spaces not allowed
return {
data: result.data
}
}

0 comments on commit 1f44210

Please sign in to comment.