Skip to content

Commit 7d35f64

Browse files
committed
improve create command
1 parent 764bdfb commit 7d35f64

File tree

1 file changed

+69
-66
lines changed

1 file changed

+69
-66
lines changed

lib/commands/repos/create.js

Lines changed: 69 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
1-
// @ts-nocheck
21
/* eslint-disable no-console */
32

4-
// import chalk from 'chalk'
3+
import chalk from 'chalk'
54
import meow from 'meow'
65
import ora from 'ora'
76

87
import { outputFlags } from '../../flags/index.js'
9-
// import { handleApiCall, handleUnsuccessfulApiResponse } from '../../utils/api-helpers.js'
10-
import { InputError } from '../../utils/errors.js'
8+
import { handleApiCall, handleUnsuccessfulApiResponse } from '../../utils/api-helpers.js'
119
import { prepareFlags } from '../../utils/flags.js'
1210
import { printFlagList } from '../../utils/formatting.js'
13-
// import { getDefaultKey, setupSdk } from '../../utils/sdk.js'
11+
import { getDefaultKey, setupSdk } from '../../utils/sdk.js'
1412

1513
/** @type {import('../../utils/meow-with-subcommands.js').CliSubcommand} */
1614
export const create = {
@@ -27,42 +25,36 @@ export const create = {
2725
}
2826
}
2927

30-
const listFullScanFlags = prepareFlags({
31-
sort: {
28+
const repositoryCreationFlags = prepareFlags({
29+
repoName: {
3230
type: 'string',
33-
shortFlag: 's',
34-
default: 'created_at',
35-
description: 'Sorting option (`name` or `created_at`) - default is `created_at`',
31+
shortFlag: 'n',
32+
default: '',
33+
description: 'Repository name',
3634
},
37-
direction: {
35+
repoDescription: {
3836
type: 'string',
3937
shortFlag: 'd',
40-
default: 'desc',
41-
description: 'Direction option (`desc` or `asc`) - Default is `desc`',
42-
},
43-
perPage: {
44-
type: 'number',
45-
shortFlag: 'pp',
46-
default: 30,
47-
description: 'Results per page - Default is 30',
48-
},
49-
page: {
50-
type: 'number',
51-
shortFlag: 'p',
52-
default: 1,
53-
description: 'Page number - Default is 1',
38+
default: '',
39+
description: 'Repository description',
5440
},
55-
fromTime: {
41+
homepage: {
5642
type: 'string',
57-
shortFlag: 'f',
43+
shortFlag: 'h',
5844
default: '',
59-
description: 'From time - as a unix timestamp',
45+
description: 'Repository url',
6046
},
61-
untilTime: {
47+
defaultBranch: {
6248
type: 'string',
63-
shortFlag: 'u',
64-
default: '',
65-
description: 'Until time - as a unix timestamp',
49+
shortFlag: 'b',
50+
default: 'main',
51+
description: 'Repository default branch',
52+
},
53+
visibility: {
54+
type: 'string',
55+
shortFlag: 'v',
56+
default: 'private',
57+
description: 'Repository visibility (Default Private)',
6658
}
6759
})
6860

@@ -73,12 +65,11 @@ const listFullScanFlags = prepareFlags({
7365
* @property {boolean} outputJson
7466
* @property {boolean} outputMarkdown
7567
* @property {string} orgSlug
76-
* @property {string} sort
77-
* @property {string} direction
78-
* @property {number} perPage
79-
* @property {number} page
80-
* @property {string} fromTime
81-
* @property {string} untilTime
68+
* @property {string} name
69+
* @property {string} description
70+
* @property {string} homepage
71+
* @property {string} default_branch
72+
* @property {string} visibility
8273
*/
8374

8475
/**
@@ -91,7 +82,7 @@ const listFullScanFlags = prepareFlags({
9182
function setupCommand (name, description, argv, importMeta) {
9283
const flags = {
9384
...outputFlags,
94-
...listFullScanFlags
85+
...repositoryCreationFlags
9586
}
9687

9788
const cli = meow(`
@@ -102,7 +93,7 @@ function setupCommand (name, description, argv, importMeta) {
10293
${printFlagList(flags, 6)}
10394
10495
Examples
105-
$ ${name} FakeOrg
96+
$ ${name} FakeOrg --repoName=test-repo
10697
`, {
10798
argv,
10899
description,
@@ -113,52 +104,64 @@ function setupCommand (name, description, argv, importMeta) {
113104
const {
114105
json: outputJson,
115106
markdown: outputMarkdown,
116-
sort,
117-
direction,
118-
perPage,
119-
page,
120-
fromTime,
121-
untilTime
107+
repoName,
108+
repoDescription,
109+
homepage,
110+
defaultBranch,
111+
visibility
122112
} = cli.flags
123113

124-
if (!cli.input[0]) {
125-
throw new InputError(`Please specify an organization slug. \n
126-
Example:
127-
socket scan list FakeOrg
128-
`)
114+
const [orgSlug = ''] = cli.input
115+
116+
if (!orgSlug) {
117+
console.error(`${chalk.bgRed('Input error')}: Please provide an organization slug \n`)
118+
cli.showHelp()
119+
return
129120
}
130121

131-
const orgSlug = cli.input[0] || ''
122+
if (!repoName) {
123+
console.error(`${chalk.bgRed('Input error')}: Repository name is required. \n`)
124+
cli.showHelp()
125+
return
126+
}
132127

133128
return {
134129
outputJson,
135130
outputMarkdown,
136131
orgSlug,
137-
sort,
138-
direction,
139-
perPage,
140-
page,
141-
fromTime,
142-
untilTime
132+
name: repoName,
133+
description: repoDescription,
134+
homepage,
135+
default_branch: defaultBranch,
136+
visibility
143137
}
144138
}
145139

146140
/**
147-
* @typedef FullScansData
148-
* @property {import('@socketsecurity/sdk').SocketSdkReturnType<'getOrgFullScanList'>["data"]} data
141+
* @typedef RepositoryData
142+
* @property {import('@socketsecurity/sdk').SocketSdkReturnType<'createOrgRepo'>["data"]} data
149143
*/
150144

151145
/**
152146
* @param {string} orgSlug
153147
* @param {CommandContext} input
154148
* @param {import('ora').Ora} spinner
155-
* @returns {Promise<void|FullScansData>}
149+
* @returns {Promise<void|RepositoryData>}
156150
*/
157151
async function createRepo (orgSlug, input, spinner) {
158-
// const socketSdk = await setupSdk(getDefaultKey())
159-
console.log(input)
152+
const socketSdk = await setupSdk(getDefaultKey())
153+
// @ts-ignore
154+
const result = await handleApiCall(socketSdk.createOrgRepo(orgSlug, input), 'listing repositories')
155+
156+
if (!result.success) {
157+
return handleUnsuccessfulApiResponse('createOrgRepo', result, spinner)
158+
}
159+
160+
spinner.stop()
160161

161-
// return {
162-
// // data: result.data
163-
// }
162+
console.log('\n✅ Repository created successfully \n')
163+
164+
return {
165+
data: result.data
166+
}
164167
}

0 commit comments

Comments
 (0)