1
- // @ts -nocheck
2
1
/* eslint-disable no-console */
3
2
4
- // import chalk from 'chalk'
3
+ import chalk from 'chalk'
4
+ import chalkTable from 'chalk-table'
5
5
import meow from 'meow'
6
6
import ora from 'ora'
7
7
8
8
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'
11
11
import { printFlagList } from '../../utils/formatting.js'
12
- // import { getDefaultKey, setupSdk } from '../../utils/sdk.js'
12
+ import { getDefaultKey , setupSdk } from '../../utils/sdk.js'
13
13
14
- /** @type {import('../../utils/meow-with-subcommands').CliSubcommand } */
14
+ /** @type {import('../../utils/meow-with-subcommands.js ').CliSubcommand } */
15
15
export const list = {
16
16
description : 'List repositories in an organization' ,
17
17
async run ( argv , importMeta , { parentName } ) {
@@ -26,13 +26,43 @@ export const list = {
26
26
}
27
27
}
28
28
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
+
29
55
// Internal functions
30
56
31
57
/**
32
58
* @typedef CommandContext
33
59
* @property {boolean } outputJson
34
60
* @property {boolean } outputMarkdown
35
61
* @property {string } orgSlug
62
+ * @property {string } sort
63
+ * @property {string } direction
64
+ * @property {number } per_page
65
+ * @property {number } page
36
66
*/
37
67
38
68
/**
@@ -44,7 +74,8 @@ export const list = {
44
74
*/
45
75
function setupCommand ( name , description , argv , importMeta ) {
46
76
const flags = {
47
- ...outputFlags
77
+ ...outputFlags ,
78
+ ...listRepoFlags
48
79
}
49
80
50
81
const cli = meow ( `
@@ -65,28 +96,35 @@ function setupCommand (name, description, argv, importMeta) {
65
96
66
97
const {
67
98
json : outputJson ,
68
- markdown : outputMarkdown
99
+ markdown : outputMarkdown ,
100
+ perPage,
101
+ sort,
102
+ direction,
103
+ page
69
104
} = cli . flags
70
105
71
106
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
76
110
}
77
111
78
- const orgSlug = cli . input [ 0 ] || ''
112
+ const [ orgSlug = '' ] = cli . input
79
113
80
114
return {
81
115
outputJson,
82
116
outputMarkdown,
83
- orgSlug
117
+ orgSlug,
118
+ sort,
119
+ direction,
120
+ page,
121
+ per_page : perPage
84
122
}
85
123
}
86
124
87
125
/**
88
126
* @typedef RepositoryData
89
- * @property {import('@socketsecurity/sdk').SocketSdkReturnType<'getOrgFullScanList '>["data"] } data
127
+ * @property {import('@socketsecurity/sdk').SocketSdkReturnType<'getOrgRepoList '>["data"] } data
90
128
*/
91
129
92
130
/**
@@ -96,10 +134,36 @@ socket scan list FakeOrg
96
134
* @returns {Promise<void|RepositoryData> }
97
135
*/
98
136
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 ( )
101
145
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
+ }
105
169
}
0 commit comments