Skip to content

Commit 079e6dd

Browse files
committed
Apply handle pattern to audit-logs
1 parent 581b04a commit 079e6dd

File tree

4 files changed

+134
-81
lines changed

4 files changed

+134
-81
lines changed

src/commands/audit-log/cmd-audit-log.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 { getAuditLog } from './get-audit-log'
6+
import { handleAuditLog } from './handle-audit-log'
77
import constants from '../../constants'
88
import { commonFlags, outputFlags } from '../../flags'
99
import { meowOrExit } from '../../utils/meow-with-subcommands'
@@ -96,7 +96,7 @@ async function run(
9696
return
9797
}
9898

99-
await getAuditLog({
99+
await handleAuditLog({
100100
orgSlug,
101101
outputKind: json ? 'json' : markdown ? 'markdown' : 'print',
102102
page: Number(page || 0),
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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 fetchAuditLog({
9+
logType,
10+
orgSlug,
11+
outputKind,
12+
page,
13+
perPage
14+
}: {
15+
outputKind: 'json' | 'markdown' | 'print'
16+
orgSlug: string
17+
page: number
18+
perPage: number
19+
logType: string
20+
}): Promise<SocketSdkReturnType<'getAuditLogEvents'>['data'] | void> {
21+
const apiToken = getDefaultToken()
22+
if (!apiToken) {
23+
throw new AuthError(
24+
'User must be authenticated to run this command. To log in, run the command `socket login` and enter your API key.'
25+
)
26+
}
27+
28+
return await fetchAuditLogWithToken({
29+
apiToken,
30+
logType,
31+
orgSlug,
32+
outputKind,
33+
page,
34+
perPage
35+
})
36+
}
37+
38+
export async function fetchAuditLogWithToken({
39+
apiToken,
40+
logType,
41+
orgSlug,
42+
outputKind,
43+
page,
44+
perPage
45+
}: {
46+
apiToken: string
47+
outputKind: 'json' | 'markdown' | 'print'
48+
orgSlug: string
49+
page: number
50+
perPage: number
51+
logType: string
52+
}): Promise<SocketSdkReturnType<'getAuditLogEvents'>['data'] | void> {
53+
// Lazily access constants.spinner.
54+
const { spinner } = constants
55+
56+
spinner.start(`Looking up audit log for ${orgSlug}`)
57+
58+
const socketSdk = await setupSdk(apiToken)
59+
const result = await handleApiCall(
60+
socketSdk.getAuditLogEvents(orgSlug, {
61+
// I'm not sure this is used at all.
62+
outputJson: String(outputKind === 'json'),
63+
// I'm not sure this is used at all.
64+
outputMarkdown: String(outputKind === 'markdown'),
65+
orgSlug,
66+
type: logType,
67+
page: String(page),
68+
per_page: String(perPage)
69+
}),
70+
`Looking up audit log for ${orgSlug}\n`
71+
)
72+
73+
if (!result.success) {
74+
handleUnsuccessfulApiResponse('getAuditLogEvents', result)
75+
return
76+
}
77+
78+
spinner.stop()
79+
80+
return result.data
81+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { fetchAuditLog } from './fetch-audit-log'
2+
import { outputAuditLog } from './output-audit-log'
3+
4+
export async function handleAuditLog({
5+
logType,
6+
orgSlug,
7+
outputKind,
8+
page,
9+
perPage
10+
}: {
11+
outputKind: 'json' | 'markdown' | 'print'
12+
orgSlug: string
13+
page: number
14+
perPage: number
15+
logType: string
16+
}): Promise<void> {
17+
const auditLogs = await fetchAuditLog({
18+
orgSlug,
19+
outputKind,
20+
page,
21+
perPage,
22+
logType
23+
})
24+
if (!auditLogs) return
25+
26+
await outputAuditLog(auditLogs, {
27+
logType,
28+
orgSlug,
29+
outputKind,
30+
page,
31+
perPage
32+
})
33+
}

src/commands/audit-log/get-audit-log.ts renamed to src/commands/audit-log/output-audit-log.ts

Lines changed: 18 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,7 @@ import { stripIndents } from 'common-tags'
33
import { logger } from '@socketsecurity/registry/lib/logger'
44
import { Separator, select } from '@socketsecurity/registry/lib/prompts'
55

6-
import constants from '../../constants'
7-
import { handleApiCall, handleUnsuccessfulApiResponse } from '../../utils/api'
8-
import { AuthError } from '../../utils/errors'
96
import { mdTable } from '../../utils/markdown'
10-
import { getDefaultToken, setupSdk } from '../../utils/sdk'
117

128
import type { Choice } from '@socketsecurity/registry/lib/prompts'
139
import type { SocketSdkReturnType } from '@socketsecurity/sdk'
@@ -16,41 +12,29 @@ type AuditChoice = Choice<string>
1612

1713
type AuditChoices = Array<Separator | AuditChoice>
1814

19-
export async function getAuditLog({
20-
logType,
21-
orgSlug,
22-
outputKind,
23-
page,
24-
perPage
25-
}: {
26-
outputKind: 'json' | 'markdown' | 'print'
27-
orgSlug: string
28-
page: number
29-
perPage: number
30-
logType: string
31-
}): Promise<void> {
32-
const apiToken = getDefaultToken()
33-
if (!apiToken) {
34-
throw new AuthError(
35-
'User must be authenticated to run this command. To log in, run the command `socket login` and enter your API key.'
36-
)
37-
}
38-
39-
const auditLogs = await getAuditLogWithToken({
40-
apiToken,
15+
export async function outputAuditLog(
16+
auditLogs: SocketSdkReturnType<'getAuditLogEvents'>['data'],
17+
{
18+
logType,
4119
orgSlug,
4220
outputKind,
4321
page,
44-
perPage,
45-
logType
46-
})
47-
if (!auditLogs) return
48-
49-
if (outputKind === 'json')
22+
perPage
23+
}: {
24+
outputKind: 'json' | 'markdown' | 'print'
25+
orgSlug: string
26+
page: number
27+
perPage: number
28+
logType: string
29+
}
30+
): Promise<void> {
31+
if (outputKind === 'json') {
5032
await outputAsJson(auditLogs.results, orgSlug, logType, page, perPage)
51-
else if (outputKind === 'markdown')
33+
} else if (outputKind === 'markdown') {
5234
await outputAsMarkdown(auditLogs.results, orgSlug, logType, page, perPage)
53-
else await outputAsPrint(auditLogs.results, orgSlug, logType)
35+
} else {
36+
await outputAsPrint(auditLogs.results, orgSlug, logType)
37+
}
5438
}
5539

5640
async function outputAsJson(
@@ -174,48 +158,3 @@ async function outputAsPrint(
174158
]
175159
)
176160
}
177-
178-
async function getAuditLogWithToken({
179-
apiToken,
180-
logType,
181-
orgSlug,
182-
outputKind,
183-
page,
184-
perPage
185-
}: {
186-
apiToken: string
187-
outputKind: 'json' | 'markdown' | 'print'
188-
orgSlug: string
189-
page: number
190-
perPage: number
191-
logType: string
192-
}): Promise<SocketSdkReturnType<'getAuditLogEvents'>['data'] | void> {
193-
// Lazily access constants.spinner.
194-
const { spinner } = constants
195-
196-
spinner.start(`Looking up audit log for ${orgSlug}`)
197-
198-
const socketSdk = await setupSdk(apiToken)
199-
const result = await handleApiCall(
200-
socketSdk.getAuditLogEvents(orgSlug, {
201-
// I'm not sure this is used at all.
202-
outputJson: String(outputKind === 'json'),
203-
// I'm not sure this is used at all.
204-
outputMarkdown: String(outputKind === 'markdown'),
205-
orgSlug,
206-
type: logType,
207-
page: String(page),
208-
per_page: String(perPage)
209-
}),
210-
`Looking up audit log for ${orgSlug}\n`
211-
)
212-
213-
if (!result.success) {
214-
handleUnsuccessfulApiResponse('getAuditLogEvents', result)
215-
return
216-
}
217-
218-
spinner.stop()
219-
220-
return result.data
221-
}

0 commit comments

Comments
 (0)