Skip to content

Commit 6b7d6ec

Browse files
committed
refactor
1 parent cd3092d commit 6b7d6ec

File tree

2 files changed

+32
-35
lines changed

2 files changed

+32
-35
lines changed

lib/commands/audit-log/index.js

Lines changed: 29 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1+
// @ts-nocheck
12
/* eslint-disable no-console */
3+
import { Separator } from '@inquirer/select'
24
import chalk from 'chalk'
3-
// @ts-ignore
4-
import Table from 'cli-table3'
5+
import inquirer from 'inquirer'
56
import meow from 'meow'
67
import ora from 'ora'
78

89
import { outputFlags } from '../../flags/index.js'
910
import { handleApiCall, handleUnsuccessfulApiResponse } from '../../utils/api-helpers.js'
10-
import { InputError } from '../../utils/errors.js'
1111
import { prepareFlags } from '../../utils/flags.js'
1212
import { printFlagList } from '../../utils/formatting.js'
1313
import { FREE_API_KEY, getDefaultKey, setupSdk } from '../../utils/sdk.js'
1414

15-
/** @type {import('../../utils/meow-with-subcommands').CliSubcommand} */
15+
/** @type {import('../../utils/meow-with-subcommands.js').CliSubcommand} */
1616
export const auditlog = {
1717
description: 'Look up the audit log for an organization',
1818
async run (argv, importMeta, { parentName }) {
@@ -50,13 +50,13 @@ const auditLogFlags = prepareFlags({
5050
// Internal functions
5151

5252
/**
53-
* @typedef CommandContext
53+
* @typedef CommandInput
5454
* @property {boolean} outputJson
5555
* @property {boolean} outputMarkdown
5656
* @property {string} orgSlug
5757
* @property {string} type
5858
* @property {number} page
59-
* @property {number} perPage
59+
* @property {number} per_page
6060
*/
6161

6262
/**
@@ -94,27 +94,22 @@ function setupCommand (name, description, argv, importMeta) {
9494
type,
9595
page,
9696
perPage
97-
9897
} = cli.flags
9998

10099
if (cli.input.length < 1) {
101-
throw new InputError('Please provide an organization slug')
102-
}
103-
104-
const [orgSlug = ''] = cli.input
105-
106-
if (!orgSlug) {
100+
console.error(`${chalk.bgRed('Input error')}: Please provide an organization slug \n`)
107101
cli.showHelp()
108102
return
109103
}
104+
const [orgSlug = ''] = cli.input
110105

111106
return {
112107
outputJson,
113108
outputMarkdown,
114109
orgSlug,
115110
type,
116111
page,
117-
perPage
112+
per_page: perPage
118113
}
119114
}
120115

@@ -125,13 +120,12 @@ function setupCommand (name, description, argv, importMeta) {
125120

126121
/**
127122
* @param {string} orgSlug
128-
* @param {CommandContext} input
123+
* @param {CommandInput} input
129124
* @param {import('ora').Ora} spinner
130125
* @returns {Promise<void|AuditLogData>}
131126
*/
132127
async function fetchOrgAuditLog (orgSlug, input, spinner) {
133128
const socketSdk = await setupSdk(getDefaultKey() || FREE_API_KEY)
134-
console.log(input)
135129
// @ts-ignore
136130
const result = await handleApiCall(socketSdk.getAuditLogEvents(orgSlug, input), 'looking up package')
137131

@@ -141,29 +135,29 @@ async function fetchOrgAuditLog (orgSlug, input, spinner) {
141135
}
142136
spinner.stop()
143137

144-
console.log(`\n Audit log for: ${orgSlug} \n`)
138+
const /** @type {({name: string} | Separator)[]} */ data = []
139+
const /** @type {{[key: string]: string}} */ logDetails = {}
145140

146-
const table = new Table({
147-
chars: { 'top': '═', 'top-mid': '╤', 'top-left': '╔', 'top-right': '╗', 'bottom': '═', 'bottom-mid': '╧', 'bottom-left': '╚', 'bottom-right': '╝', 'left': '║', 'left-mid': '╟', 'mid': '─', 'mid-mid': '┼', 'right': '║', 'right-mid': '╢', 'middle': '│' },
148-
colWidths: [11, 20],
149-
style: { head: [], border: [] },
150-
wordWrap: true
151-
})
141+
result.data.results.map(d => {
142+
data.push({
143+
name: `${d.created_at && new Date(d.created_at).toLocaleDateString('en-us', { year: 'numeric', month: 'numeric', day: 'numeric' })} - ${d.user_email} - ${d.type} - ${d.ip_address} - ${d.user_agent}`
144+
}, new Separator())
152145

153-
table.push([chalk.magenta('Date'), chalk.magenta('User'), chalk.magenta('Type'), chalk.magenta('IP address'), chalk.magenta('User agent')])
154-
155-
result.data.results.map((/** @type {{ created_at: string | number | Date; user_email: any; type: any; ip_address: any; user_agent: string; }} */ d) => {
156-
const data = [
157-
d.created_at ? new Date(d.created_at).toLocaleDateString('en-us', { year: 'numeric', month: 'numeric', day: 'numeric' }) : '',
158-
d.user_email,
159-
d.type,
160-
d.ip_address,
161-
d.user_agent.split(';').join('\n')
162-
]
163-
return table.push(data)
146+
logDetails[`${d.created_at && new Date(d.created_at).toLocaleDateString('en-us', { year: 'numeric', month: 'numeric', day: 'numeric' })} - ${d.user_email} - ${d.type} - ${d.ip_address} - ${d.user_agent}`] = JSON.stringify(d.payload)
147+
return data
164148
})
165149

166-
console.log(table.toString())
150+
inquirer
151+
.prompt(
152+
{
153+
type: 'list',
154+
name: 'log',
155+
message: `\n Audit log for: ${orgSlug} \n`,
156+
choices: data,
157+
pageSize: 30
158+
}
159+
)
160+
.then((/** @type {{log: string}} */ answers) => console.log(logDetails[answers.log]))
167161

168162
return {
169163
data: result.data

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
"@tsconfig/node20": "^20.1.4",
4949
"@types/chai": "^4.3.16",
5050
"@types/chai-as-promised": "^7.1.8",
51+
"@types/inquirer": "^9.0.7",
5152
"@types/micromatch": "^4.0.7",
5253
"@types/mocha": "^10.0.6",
5354
"@types/mock-fs": "^4.13.4",
@@ -84,6 +85,7 @@
8485
"dependencies": {
8586
"@apideck/better-ajv-errors": "^0.3.6",
8687
"@cyclonedx/cdxgen": "^10.5.2",
88+
"@inquirer/select": "^2.3.5",
8789
"@socketsecurity/config": "^2.1.3",
8890
"@socketsecurity/sdk": "^1.0.1",
8991
"chalk": "^5.3.0",
@@ -93,6 +95,7 @@
9395
"hpagent": "^1.2.0",
9496
"ignore": "^5.3.1",
9597
"ignore-by-default": "^2.1.0",
98+
"inquirer": "^9.2.23",
9699
"is-interactive": "^2.0.0",
97100
"is-unicode-supported": "^2.0.0",
98101
"meow": "^13.2.0",

0 commit comments

Comments
 (0)