Skip to content

Commit 3dd40f5

Browse files
authored
Apply handle pattern to organization (#377)
1 parent fd47773 commit 3dd40f5

13 files changed

+237
-156
lines changed

src/commands/organization/cmd-organization-list.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import colors from 'yoctocolors-cjs'
33

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

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

71-
await getOrganization(json ? 'json' : markdown ? 'markdown' : 'text')
71+
await handleOrganizationList(json ? 'json' : markdown ? 'markdown' : 'text')
7272
}

src/commands/organization/cmd-organization-policy-security.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import colors from 'yoctocolors-cjs'
33

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

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

83-
await getSecurityPolicy(
83+
await handleSecurityPolicy(
8484
orgSlug,
8585
json ? 'json' : markdown ? 'markdown' : 'text'
8686
)

src/commands/organization/cmd-organization-quota.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import colors from 'yoctocolors-cjs'
33

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

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

71-
await getQuota(json ? 'json' : markdown ? 'markdown' : 'text')
71+
await handleQuota(json ? 'json' : markdown ? 'markdown' : 'text')
7272
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import constants from '../../constants'
2+
import { handleApiCall, handleUnsuccessfulApiResponse } from '../../utils/api'
3+
import { AuthError } from '../../utils/errors'
4+
import { getDefaultToken, setupSdk } from '../../utils/sdk'
5+
6+
import type { SocketSdkReturnType } from '@socketsecurity/sdk'
7+
8+
export async function fetchOrganization(): Promise<
9+
SocketSdkReturnType<'getOrganizations'>['data'] | undefined
10+
> {
11+
const apiToken = getDefaultToken()
12+
if (!apiToken) {
13+
throw new AuthError(
14+
'User must be authenticated to run this command. To log in, run the command `socket login` and enter your API key.'
15+
)
16+
}
17+
18+
return await fetchOrganizationWithToken(apiToken)
19+
}
20+
21+
async function fetchOrganizationWithToken(
22+
apiToken: string
23+
): Promise<SocketSdkReturnType<'getOrganizations'>['data'] | undefined> {
24+
const socketSdk = await setupSdk(apiToken)
25+
26+
// Lazily access constants.spinner.
27+
const { spinner } = constants
28+
29+
spinner.start('Fetching organization list...')
30+
31+
const result = await handleApiCall(
32+
socketSdk.getOrganizations(),
33+
'looking up organizations'
34+
)
35+
36+
spinner.successAndStop('Received organization list response.')
37+
38+
if (!result.success) {
39+
handleUnsuccessfulApiResponse('getOrganizations', result)
40+
return
41+
}
42+
43+
return result.data
44+
}
Lines changed: 14 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,43 @@
1-
import { logger } from '@socketsecurity/registry/lib/logger'
2-
31
import constants from '../../constants'
42
import { handleApiCall, handleUnsuccessfulApiResponse } from '../../utils/api'
53
import { AuthError } from '../../utils/errors'
64
import { getDefaultToken, setupSdk } from '../../utils/sdk'
75

8-
export async function getQuota(
9-
format: 'text' | 'json' | 'markdown' = 'text'
10-
): Promise<void> {
6+
import type { SocketSdkReturnType } from '@socketsecurity/sdk'
7+
8+
export async function fetchQuota(): Promise<
9+
SocketSdkReturnType<'getQuota'>['data'] | undefined
10+
> {
1111
const apiToken = getDefaultToken()
1212
if (!apiToken) {
1313
throw new AuthError(
1414
'User must be authenticated to run this command. To log in, run the command `socket login` and enter your API key.'
1515
)
1616
}
17-
await getQuotaWithToken(apiToken, format)
17+
return await fetchQuotaWithToken(apiToken)
1818
}
1919

20-
async function getQuotaWithToken(
21-
apiToken: string,
22-
format: 'text' | 'json' | 'markdown' = 'text'
23-
) {
20+
async function fetchQuotaWithToken(
21+
apiToken: string
22+
): Promise<SocketSdkReturnType<'getQuota'>['data'] | undefined> {
2423
// Lazily access constants.spinner.
2524
const { spinner } = constants
2625

26+
const socketSdk = await setupSdk(apiToken)
27+
2728
spinner.start('Fetching organization quota...')
2829

29-
const socketSdk = await setupSdk(apiToken)
3030
const result = await handleApiCall(
3131
socketSdk.getQuota(),
3232
'looking up organization quota'
3333
)
3434

35+
spinner?.successAndStop('Recieved organization quota response.')
36+
3537
if (!result.success) {
3638
handleUnsuccessfulApiResponse('getQuota', result)
3739
return
3840
}
3941

40-
spinner.stop()
41-
42-
switch (format) {
43-
case 'json': {
44-
logger.log(
45-
JSON.stringify(
46-
{
47-
quota: result.data.quota
48-
},
49-
null,
50-
2
51-
)
52-
)
53-
return
54-
}
55-
case 'markdown': {
56-
logger.log('# Quota\n')
57-
logger.log(`Quota left on the current API token: ${result.data.quota}\n`)
58-
return
59-
}
60-
default: {
61-
logger.log(`Quota left on the current API token: ${result.data.quota}\n`)
62-
}
63-
}
42+
return result.data
6443
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import constants from '../../constants'
2+
import { handleApiCall, handleUnsuccessfulApiResponse } from '../../utils/api'
3+
import { AuthError } from '../../utils/errors'
4+
import { getDefaultToken, setupSdk } from '../../utils/sdk'
5+
6+
import type { SocketSdkReturnType } from '@socketsecurity/sdk'
7+
8+
export async function fetchSecurityPolicy(
9+
orgSlug: string
10+
): Promise<SocketSdkReturnType<'getOrgSecurityPolicy'>['data'] | undefined> {
11+
const apiToken = getDefaultToken()
12+
if (!apiToken) {
13+
throw new AuthError(
14+
'User must be authenticated to run this command. To log in, run the command `socket login` and enter your API key.'
15+
)
16+
}
17+
18+
return await fetchSecurityPolicyWithToken(apiToken, orgSlug)
19+
}
20+
21+
async function fetchSecurityPolicyWithToken(
22+
apiToken: string,
23+
orgSlug: string
24+
): Promise<SocketSdkReturnType<'getOrgSecurityPolicy'>['data'] | undefined> {
25+
// Lazily access constants.spinner.
26+
const { spinner } = constants
27+
28+
const socketSdk = await setupSdk(apiToken)
29+
30+
spinner.start('Fetching organization quota...')
31+
32+
const result = await handleApiCall(
33+
socketSdk.getOrgSecurityPolicy(orgSlug),
34+
'looking up organization quota'
35+
)
36+
37+
spinner?.successAndStop('Received organization quota response.')
38+
39+
if (!result.success) {
40+
handleUnsuccessfulApiResponse('getOrgSecurityPolicy', result)
41+
return
42+
}
43+
44+
return result.data
45+
}

src/commands/organization/get-security-policy.ts

Lines changed: 0 additions & 72 deletions
This file was deleted.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { fetchOrganization } from './fetch-organization-list'
2+
import { outputOrganizationList } from './output-organization-list'
3+
4+
export async function handleOrganizationList(
5+
outputKind: 'text' | 'json' | 'markdown' = 'text'
6+
): Promise<void> {
7+
const data = await fetchOrganization()
8+
if (!data) return
9+
10+
await outputOrganizationList(data, outputKind)
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { fetchQuota } from './fetch-quota'
2+
import { outputQuota } from './output-quota'
3+
4+
export async function handleQuota(
5+
outputKind: 'text' | 'json' | 'markdown' = 'text'
6+
): Promise<void> {
7+
const data = await fetchQuota()
8+
if (!data) return
9+
10+
await outputQuota(data, outputKind)
11+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { fetchSecurityPolicy } from './fetch-security-policy'
2+
import { getSecurityPolicy } from './output-security-policy'
3+
4+
export async function handleSecurityPolicy(
5+
orgSlug: string,
6+
outputKind: 'text' | 'json' | 'markdown'
7+
): Promise<void> {
8+
const data = await fetchSecurityPolicy(orgSlug)
9+
if (!data) return
10+
11+
await getSecurityPolicy(data, outputKind)
12+
}

0 commit comments

Comments
 (0)