1
+ // @ts -nocheck
1
2
/* eslint-disable no-console */
3
+ import { Separator } from '@inquirer/select'
2
4
import chalk from 'chalk'
3
- // @ts -ignore
4
- import Table from 'cli-table3'
5
+ import inquirer from 'inquirer'
5
6
import meow from 'meow'
6
7
import ora from 'ora'
7
8
8
9
import { outputFlags } from '../../flags/index.js'
9
10
import { handleApiCall , handleUnsuccessfulApiResponse } from '../../utils/api-helpers.js'
10
- import { InputError } from '../../utils/errors.js'
11
11
import { prepareFlags } from '../../utils/flags.js'
12
12
import { printFlagList } from '../../utils/formatting.js'
13
13
import { FREE_API_KEY , getDefaultKey , setupSdk } from '../../utils/sdk.js'
14
14
15
- /** @type {import('../../utils/meow-with-subcommands').CliSubcommand } */
15
+ /** @type {import('../../utils/meow-with-subcommands.js ').CliSubcommand } */
16
16
export const auditlog = {
17
17
description : 'Look up the audit log for an organization' ,
18
18
async run ( argv , importMeta , { parentName } ) {
@@ -50,13 +50,13 @@ const auditLogFlags = prepareFlags({
50
50
// Internal functions
51
51
52
52
/**
53
- * @typedef CommandContext
53
+ * @typedef CommandInput
54
54
* @property {boolean } outputJson
55
55
* @property {boolean } outputMarkdown
56
56
* @property {string } orgSlug
57
57
* @property {string } type
58
58
* @property {number } page
59
- * @property {number } perPage
59
+ * @property {number } per_page
60
60
*/
61
61
62
62
/**
@@ -94,27 +94,22 @@ function setupCommand (name, description, argv, importMeta) {
94
94
type,
95
95
page,
96
96
perPage
97
-
98
97
} = cli . flags
99
98
100
99
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` )
107
101
cli . showHelp ( )
108
102
return
109
103
}
104
+ const [ orgSlug = '' ] = cli . input
110
105
111
106
return {
112
107
outputJson,
113
108
outputMarkdown,
114
109
orgSlug,
115
110
type,
116
111
page,
117
- perPage
112
+ per_page : perPage
118
113
}
119
114
}
120
115
@@ -125,13 +120,12 @@ function setupCommand (name, description, argv, importMeta) {
125
120
126
121
/**
127
122
* @param {string } orgSlug
128
- * @param {CommandContext } input
123
+ * @param {CommandInput } input
129
124
* @param {import('ora').Ora } spinner
130
125
* @returns {Promise<void|AuditLogData> }
131
126
*/
132
127
async function fetchOrgAuditLog ( orgSlug , input , spinner ) {
133
128
const socketSdk = await setupSdk ( getDefaultKey ( ) || FREE_API_KEY )
134
- console . log ( input )
135
129
// @ts -ignore
136
130
const result = await handleApiCall ( socketSdk . getAuditLogEvents ( orgSlug , input ) , 'looking up package' )
137
131
@@ -141,29 +135,29 @@ async function fetchOrgAuditLog (orgSlug, input, spinner) {
141
135
}
142
136
spinner . stop ( )
143
137
144
- console . log ( `\n Audit log for: ${ orgSlug } \n` )
138
+ const /** @type {({name: string} | Separator)[] } */ data = [ ]
139
+ const /** @type {{[key: string]: string} } */ logDetails = { }
145
140
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 ( ) )
152
145
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
164
148
} )
165
149
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 ] ) )
167
161
168
162
return {
169
163
data : result . data
0 commit comments