Skip to content

Commit 2e27283

Browse files
committed
improve list command
1 parent 0d9d155 commit 2e27283

File tree

2 files changed

+85
-20
lines changed

2 files changed

+85
-20
lines changed

lib/commands/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ export * from './report/index.js'
1010
export * from './wrapper/index.js'
1111
export * from './scan/index.js'
1212
export * from './audit-log/index.js'
13+
export * from './repos/index.js'

lib/commands/repos/list.js

Lines changed: 84 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
// @ts-nocheck
21
/* eslint-disable no-console */
32

4-
// import chalk from 'chalk'
3+
import chalk from 'chalk'
4+
import chalkTable from 'chalk-table'
55
import meow from 'meow'
66
import ora from 'ora'
77

88
import { outputFlags } from '../../flags/index.js'
9-
// import { handleApiCall, handleUnsuccessfulApiResponse } from '../../utils/api-helpers.js'
10-
import { InputError } from '../../utils/errors.js'
9+
import { handleApiCall, handleUnsuccessfulApiResponse } from '../../utils/api-helpers.js'
10+
import { prepareFlags } from '../../utils/flags.js'
1111
import { printFlagList } from '../../utils/formatting.js'
12-
// import { getDefaultKey, setupSdk } from '../../utils/sdk.js'
12+
import { getDefaultKey, setupSdk } from '../../utils/sdk.js'
1313

14-
/** @type {import('../../utils/meow-with-subcommands').CliSubcommand} */
14+
/** @type {import('../../utils/meow-with-subcommands.js').CliSubcommand} */
1515
export const list = {
1616
description: 'List repositories in an organization',
1717
async run (argv, importMeta, { parentName }) {
@@ -26,13 +26,43 @@ export const list = {
2626
}
2727
}
2828

29+
const listRepoFlags = prepareFlags({
30+
sort: {
31+
type: 'string',
32+
shortFlag: 's',
33+
default: 'created_at',
34+
description: 'Sorting option',
35+
},
36+
direction: {
37+
type: 'string',
38+
default: 'desc',
39+
description: 'Direction option',
40+
},
41+
perPage: {
42+
type: 'number',
43+
shortFlag: 'pp',
44+
default: 30,
45+
description: 'Number of results per page'
46+
},
47+
page: {
48+
type: 'number',
49+
shortFlag: 'p',
50+
default: 1,
51+
description: 'Page number'
52+
},
53+
})
54+
2955
// Internal functions
3056

3157
/**
3258
* @typedef CommandContext
3359
* @property {boolean} outputJson
3460
* @property {boolean} outputMarkdown
3561
* @property {string} orgSlug
62+
* @property {string} sort
63+
* @property {string} direction
64+
* @property {number} per_page
65+
* @property {number} page
3666
*/
3767

3868
/**
@@ -44,7 +74,8 @@ export const list = {
4474
*/
4575
function setupCommand (name, description, argv, importMeta) {
4676
const flags = {
47-
...outputFlags
77+
...outputFlags,
78+
...listRepoFlags
4879
}
4980

5081
const cli = meow(`
@@ -65,28 +96,35 @@ function setupCommand (name, description, argv, importMeta) {
6596

6697
const {
6798
json: outputJson,
68-
markdown: outputMarkdown
99+
markdown: outputMarkdown,
100+
perPage,
101+
sort,
102+
direction,
103+
page
69104
} = cli.flags
70105

71106
if (!cli.input[0]) {
72-
throw new InputError(`Please specify an organization slug. \n
73-
Example:
74-
socket scan list FakeOrg
75-
`)
107+
console.error(`${chalk.bgRed('Input error')}: Please provide an organization slug \n`)
108+
cli.showHelp()
109+
return
76110
}
77111

78-
const orgSlug = cli.input[0] || ''
112+
const [orgSlug = ''] = cli.input
79113

80114
return {
81115
outputJson,
82116
outputMarkdown,
83-
orgSlug
117+
orgSlug,
118+
sort,
119+
direction,
120+
page,
121+
per_page: perPage
84122
}
85123
}
86124

87125
/**
88126
* @typedef RepositoryData
89-
* @property {import('@socketsecurity/sdk').SocketSdkReturnType<'getOrgFullScanList'>["data"]} data
127+
* @property {import('@socketsecurity/sdk').SocketSdkReturnType<'getOrgRepoList'>["data"]} data
90128
*/
91129

92130
/**
@@ -96,10 +134,36 @@ socket scan list FakeOrg
96134
* @returns {Promise<void|RepositoryData>}
97135
*/
98136
async function listOrgRepos (orgSlug, input, spinner) {
99-
// const socketSdk = await setupSdk(getDefaultKey())
100-
console.log(input)
137+
const socketSdk = await setupSdk(getDefaultKey())
138+
const result = await handleApiCall(socketSdk.getOrgRepoList(orgSlug, input), 'looking up package')
139+
140+
if (!result.success) {
141+
return handleUnsuccessfulApiResponse('getOrgRepoList', result, spinner)
142+
}
143+
144+
spinner.stop()
101145

102-
// return {
103-
// // data: result.data
104-
// }
146+
const options = {
147+
columns: [
148+
{ field: 'id', name: chalk.magenta('ID') },
149+
{ field: 'name', name: chalk.magenta('Name') },
150+
{ field: 'visibility', name: chalk.magenta('Visibility') },
151+
{ field: 'default_branch', name: chalk.magenta('Default branch') },
152+
{ field: 'archived', name: chalk.magenta('Archived') }
153+
]
154+
}
155+
156+
const formattedResults = result.data.results.map(d => {
157+
return {
158+
...d
159+
}
160+
})
161+
162+
const table = chalkTable(options, formattedResults)
163+
164+
console.log(table, '\n')
165+
166+
return {
167+
data: result.data
168+
}
105169
}

0 commit comments

Comments
 (0)