Skip to content

Apply handle pattern to organization #377

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

Merged
merged 2 commits into from
Mar 20, 2025
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
4 changes: 2 additions & 2 deletions src/commands/organization/cmd-organization-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import colors from 'yoctocolors-cjs'

import { logger } from '@socketsecurity/registry/lib/logger'

import { getOrganization } from './get-organization'
import { handleOrganizationList } from './handle-organization-list'
import constants from '../../constants'
import { commonFlags, outputFlags } from '../../flags'
import { meowOrExit } from '../../utils/meow-with-subcommands'
Expand Down Expand Up @@ -68,5 +68,5 @@ ${colors.bgRed(colors.white('Input error'))}: Please provide the required fields
return
}

await getOrganization(json ? 'json' : markdown ? 'markdown' : 'text')
await handleOrganizationList(json ? 'json' : markdown ? 'markdown' : 'text')
}
4 changes: 2 additions & 2 deletions src/commands/organization/cmd-organization-policy-security.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import colors from 'yoctocolors-cjs'

import { logger } from '@socketsecurity/registry/lib/logger'

import { getSecurityPolicy } from './get-security-policy'
import { handleSecurityPolicy } from './handle-security-policy'
import constants from '../../constants'
import { commonFlags, outputFlags } from '../../flags'
import { meowOrExit } from '../../utils/meow-with-subcommands'
Expand Down Expand Up @@ -80,7 +80,7 @@ ${colors.bgRed(colors.white('Input error'))}: Please provide the required fields
return
}

await getSecurityPolicy(
await handleSecurityPolicy(
orgSlug,
json ? 'json' : markdown ? 'markdown' : 'text'
)
Expand Down
4 changes: 2 additions & 2 deletions src/commands/organization/cmd-organization-quota.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import colors from 'yoctocolors-cjs'

import { logger } from '@socketsecurity/registry/lib/logger'

import { getQuota } from './get-quota'
import { handleQuota } from './handle-quota'
import constants from '../../constants'
import { commonFlags, outputFlags } from '../../flags'
import { meowOrExit } from '../../utils/meow-with-subcommands'
Expand Down Expand Up @@ -68,5 +68,5 @@ ${colors.bgRed(colors.white('Input error'))}: Please provide the required fields
return
}

await getQuota(json ? 'json' : markdown ? 'markdown' : 'text')
await handleQuota(json ? 'json' : markdown ? 'markdown' : 'text')
}
44 changes: 44 additions & 0 deletions src/commands/organization/fetch-organization-list.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import constants from '../../constants'
import { handleApiCall, handleUnsuccessfulApiResponse } from '../../utils/api'
import { AuthError } from '../../utils/errors'
import { getDefaultToken, setupSdk } from '../../utils/sdk'

import type { SocketSdkReturnType } from '@socketsecurity/sdk'

export async function fetchOrganization(): Promise<
SocketSdkReturnType<'getOrganizations'>['data'] | undefined
> {
const apiToken = getDefaultToken()
if (!apiToken) {
throw new AuthError(
'User must be authenticated to run this command. To log in, run the command `socket login` and enter your API key.'
)
}

return await fetchOrganizationWithToken(apiToken)
}

async function fetchOrganizationWithToken(
apiToken: string
): Promise<SocketSdkReturnType<'getOrganizations'>['data'] | undefined> {
const socketSdk = await setupSdk(apiToken)

// Lazily access constants.spinner.
const { spinner } = constants

spinner.start('Fetching organization list...')

const result = await handleApiCall(
socketSdk.getOrganizations(),
'looking up organizations'
)

spinner.successAndStop('Received organization list response.')

if (!result.success) {
handleUnsuccessfulApiResponse('getOrganizations', result)
return
}

return result.data
}
Original file line number Diff line number Diff line change
@@ -1,64 +1,43 @@
import { logger } from '@socketsecurity/registry/lib/logger'

import constants from '../../constants'
import { handleApiCall, handleUnsuccessfulApiResponse } from '../../utils/api'
import { AuthError } from '../../utils/errors'
import { getDefaultToken, setupSdk } from '../../utils/sdk'

export async function getQuota(
format: 'text' | 'json' | 'markdown' = 'text'
): Promise<void> {
import type { SocketSdkReturnType } from '@socketsecurity/sdk'

export async function fetchQuota(): Promise<
SocketSdkReturnType<'getQuota'>['data'] | undefined
> {
const apiToken = getDefaultToken()
if (!apiToken) {
throw new AuthError(
'User must be authenticated to run this command. To log in, run the command `socket login` and enter your API key.'
)
}
await getQuotaWithToken(apiToken, format)
return await fetchQuotaWithToken(apiToken)
}

async function getQuotaWithToken(
apiToken: string,
format: 'text' | 'json' | 'markdown' = 'text'
) {
async function fetchQuotaWithToken(
apiToken: string
): Promise<SocketSdkReturnType<'getQuota'>['data'] | undefined> {
// Lazily access constants.spinner.
const { spinner } = constants

const socketSdk = await setupSdk(apiToken)

spinner.start('Fetching organization quota...')

const socketSdk = await setupSdk(apiToken)
const result = await handleApiCall(
socketSdk.getQuota(),
'looking up organization quota'
)

spinner?.successAndStop('Recieved organization quota response.')

if (!result.success) {
handleUnsuccessfulApiResponse('getQuota', result)
return
}

spinner.stop()

switch (format) {
case 'json': {
logger.log(
JSON.stringify(
{
quota: result.data.quota
},
null,
2
)
)
return
}
case 'markdown': {
logger.log('# Quota\n')
logger.log(`Quota left on the current API token: ${result.data.quota}\n`)
return
}
default: {
logger.log(`Quota left on the current API token: ${result.data.quota}\n`)
}
}
return result.data
}
45 changes: 45 additions & 0 deletions src/commands/organization/fetch-security-policy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import constants from '../../constants'
import { handleApiCall, handleUnsuccessfulApiResponse } from '../../utils/api'
import { AuthError } from '../../utils/errors'
import { getDefaultToken, setupSdk } from '../../utils/sdk'

import type { SocketSdkReturnType } from '@socketsecurity/sdk'

export async function fetchSecurityPolicy(
orgSlug: string
): Promise<SocketSdkReturnType<'getOrgSecurityPolicy'>['data'] | undefined> {
const apiToken = getDefaultToken()
if (!apiToken) {
throw new AuthError(
'User must be authenticated to run this command. To log in, run the command `socket login` and enter your API key.'
)
}

return await fetchSecurityPolicyWithToken(apiToken, orgSlug)
}

async function fetchSecurityPolicyWithToken(
apiToken: string,
orgSlug: string
): Promise<SocketSdkReturnType<'getOrgSecurityPolicy'>['data'] | undefined> {
// Lazily access constants.spinner.
const { spinner } = constants

const socketSdk = await setupSdk(apiToken)

spinner.start('Fetching organization quota...')

const result = await handleApiCall(
socketSdk.getOrgSecurityPolicy(orgSlug),
'looking up organization quota'
)

spinner?.successAndStop('Received organization quota response.')

if (!result.success) {
handleUnsuccessfulApiResponse('getOrgSecurityPolicy', result)
return
}

return result.data
}
72 changes: 0 additions & 72 deletions src/commands/organization/get-security-policy.ts

This file was deleted.

11 changes: 11 additions & 0 deletions src/commands/organization/handle-organization-list.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { fetchOrganization } from './fetch-organization-list'
import { outputOrganizationList } from './output-organization-list'

export async function handleOrganizationList(
outputKind: 'text' | 'json' | 'markdown' = 'text'
): Promise<void> {
const data = await fetchOrganization()
if (!data) return

await outputOrganizationList(data, outputKind)
}
11 changes: 11 additions & 0 deletions src/commands/organization/handle-quota.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { fetchQuota } from './fetch-quota'
import { outputQuota } from './output-quota'

export async function handleQuota(
outputKind: 'text' | 'json' | 'markdown' = 'text'
): Promise<void> {
const data = await fetchQuota()
if (!data) return

await outputQuota(data, outputKind)
}
12 changes: 12 additions & 0 deletions src/commands/organization/handle-security-policy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { fetchSecurityPolicy } from './fetch-security-policy'
import { getSecurityPolicy } from './output-security-policy'

export async function handleSecurityPolicy(
orgSlug: string,
outputKind: 'text' | 'json' | 'markdown'
): Promise<void> {
const data = await fetchSecurityPolicy(orgSlug)
if (!data) return

await getSecurityPolicy(data, outputKind)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,53 +2,20 @@ import colors from 'yoctocolors-cjs'

import { logger } from '@socketsecurity/registry/lib/logger'

import constants from '../../constants'
import {
getLastFiveOfApiToken,
handleApiCall,
handleUnsuccessfulApiResponse
} from '../../utils/api'
import { AuthError } from '../../utils/errors'
import { getDefaultToken, setupSdk } from '../../utils/sdk'
import { getLastFiveOfApiToken } from '../../utils/api'
import { getDefaultToken } from '../../utils/sdk'

export async function getOrganization(
format: 'text' | 'json' | 'markdown' = 'text'
import type { SocketSdkReturnType } from '@socketsecurity/sdk'

export async function outputOrganizationList(
data: SocketSdkReturnType<'getOrganizations'>['data'],
outputKind: 'text' | 'json' | 'markdown' = 'text'
): Promise<void> {
const organizations = Object.values(data.organizations)
const apiToken = getDefaultToken()
if (!apiToken) {
throw new AuthError(
'User must be authenticated to run this command. To log in, run the command `socket login` and enter your API key.'
)
}
await printOrganizationsFromToken(apiToken, format)
}

async function printOrganizationsFromToken(
apiToken: string,
format: 'text' | 'json' | 'markdown' = 'text'
) {
// Lazily access constants.spinner.
const { spinner } = constants

spinner.start('Fetching organizations...')

const socketSdk = await setupSdk(apiToken)
const result = await handleApiCall(
socketSdk.getOrganizations(),
'looking up organizations'
)

if (!result.success) {
handleUnsuccessfulApiResponse('getOrganizations', result)
return
}

spinner.stop()

const organizations = Object.values(result.data.organizations)
const lastFiveOfApiToken = getLastFiveOfApiToken(apiToken)
const lastFiveOfApiToken = getLastFiveOfApiToken(apiToken ?? '?????')

switch (format) {
switch (outputKind) {
case 'json': {
logger.log(
JSON.stringify(
Expand Down
Loading
Loading