Skip to content

Commit 4d6e8b3

Browse files
committed
improve update command
1 parent 7d35f64 commit 4d6e8b3

File tree

3 files changed

+115
-64
lines changed

3 files changed

+115
-64
lines changed

lib/commands/repos/delete.js

Lines changed: 32 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
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

8-
import { outputFlags } from '../../flags/index.js'
9-
// import { handleApiCall, handleUnsuccessfulApiResponse } from '../../utils/api-helpers.js'
10-
import { InputError } from '../../utils/errors.js'
11-
import { printFlagList } from '../../utils/formatting.js'
12-
// import { getDefaultKey, setupSdk } from '../../utils/sdk.js'
7+
import { handleApiCall, handleUnsuccessfulApiResponse } from '../../utils/api-helpers.js'
8+
import { getDefaultKey, setupSdk } from '../../utils/sdk.js'
139

1410
/** @type {import('../../utils/meow-with-subcommands.js').CliSubcommand} */
1511
export const del = {
@@ -21,7 +17,7 @@ export const del = {
2117
if (input) {
2218
const spinnerText = 'Deleting repository... \n'
2319
const spinner = ora(spinnerText).start()
24-
await deleteRepository(input.orgSlug, input, spinner)
20+
await deleteRepository(input.orgSlug, input.repoName, spinner)
2521
}
2622
}
2723
}
@@ -30,9 +26,8 @@ export const del = {
3026

3127
/**
3228
* @typedef CommandContext
33-
* @property {boolean} outputJson
34-
* @property {boolean} outputMarkdown
3529
* @property {string} orgSlug
30+
* @property {string} repoName
3631
*/
3732

3833
/**
@@ -43,63 +38,57 @@ export const del = {
4338
* @returns {void|CommandContext}
4439
*/
4540
function setupCommand (name, description, argv, importMeta) {
46-
const flags = {
47-
...outputFlags
48-
}
49-
5041
const cli = meow(`
5142
Usage
52-
$ ${name} <org slug>
53-
54-
Options
55-
${printFlagList(flags, 6)}
43+
$ ${name} <org slug> <repo slug>
5644
5745
Examples
58-
$ ${name} FakeOrg
46+
$ ${name} FakeOrg test-repo
5947
`, {
6048
argv,
6149
description,
62-
importMeta,
63-
flags
50+
importMeta
6451
})
6552

66-
const {
67-
json: outputJson,
68-
markdown: outputMarkdown
69-
} = cli.flags
53+
const [orgSlug = '', repoName = ''] = cli.input
7054

71-
if (!cli.input[0]) {
72-
throw new InputError(`Please specify an organization slug. \n
73-
Example:
74-
socket scan list FakeOrg
75-
`)
55+
if (!orgSlug || !repoName) {
56+
console.error(`${chalk.bgRed('Input error')}: Please provide an organization slug and repository slug \n`)
57+
cli.showHelp()
58+
return
7659
}
7760

78-
const orgSlug = cli.input[0] || ''
79-
8061
return {
81-
outputJson,
82-
outputMarkdown,
83-
orgSlug
62+
orgSlug,
63+
repoName
8464
}
8565
}
8666

8767
/**
8868
* @typedef RepositoryData
89-
* @property {import('@socketsecurity/sdk').SocketSdkReturnType<'getOrgFullScanList'>["data"]} data
69+
* @property {import('@socketsecurity/sdk').SocketSdkReturnType<'deleteOrgRepo'>["data"]} data
9070
*/
9171

9272
/**
9373
* @param {string} orgSlug
94-
* @param {CommandContext} input
74+
* @param {string} repoName
9575
* @param {import('ora').Ora} spinner
9676
* @returns {Promise<void|RepositoryData>}
9777
*/
98-
async function deleteRepository (orgSlug, input, spinner) {
99-
// const socketSdk = await setupSdk(getDefaultKey())
100-
console.log(input)
78+
async function deleteRepository (orgSlug, repoName, spinner) {
79+
const socketSdk = await setupSdk(getDefaultKey())
80+
// @ts-ignore
81+
const result = await handleApiCall(socketSdk.deleteOrgRepo(orgSlug, repoName), 'deleting repository')
82+
83+
if (!result.success) {
84+
return handleUnsuccessfulApiResponse('deleteOrgRepo', result, spinner)
85+
}
86+
87+
spinner.stop()
88+
89+
console.log('\n✅ Repository deleted successfully \n')
10190

102-
// return {
103-
// // data: result.data
104-
// }
91+
return {
92+
data: result.data
93+
}
10594
}

lib/commands/repos/update.js

Lines changed: 82 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +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'
9+
import { prepareFlags } from '../../utils/flags.js'
1110
import { printFlagList } from '../../utils/formatting.js'
12-
// import { getDefaultKey, setupSdk } from '../../utils/sdk.js'
11+
import { getDefaultKey, setupSdk } from '../../utils/sdk.js'
1312

1413
/** @type {import('../../utils/meow-with-subcommands.js').CliSubcommand} */
1514
export const update = {
@@ -26,13 +25,51 @@ export const update = {
2625
}
2726
}
2827

28+
const repositoryUpdateFlags = prepareFlags({
29+
repoName: {
30+
type: 'string',
31+
shortFlag: 'n',
32+
default: '',
33+
description: 'Repository name',
34+
},
35+
repoDescription: {
36+
type: 'string',
37+
shortFlag: 'd',
38+
default: '',
39+
description: 'Repository description',
40+
},
41+
homepage: {
42+
type: 'string',
43+
shortFlag: 'h',
44+
default: '',
45+
description: 'Repository url',
46+
},
47+
defaultBranch: {
48+
type: 'string',
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)',
58+
}
59+
})
60+
2961
// Internal functions
3062

3163
/**
3264
* @typedef CommandContext
3365
* @property {boolean} outputJson
3466
* @property {boolean} outputMarkdown
3567
* @property {string} orgSlug
68+
* @property {string} name
69+
* @property {string} description
70+
* @property {string} homepage
71+
* @property {string} default_branch
72+
* @property {string} visibility
3673
*/
3774

3875
/**
@@ -44,7 +81,8 @@ export const update = {
4481
*/
4582
function setupCommand (name, description, argv, importMeta) {
4683
const flags = {
47-
...outputFlags
84+
...outputFlags,
85+
...repositoryUpdateFlags
4886
}
4987

5088
const cli = meow(`
@@ -65,28 +103,43 @@ function setupCommand (name, description, argv, importMeta) {
65103

66104
const {
67105
json: outputJson,
68-
markdown: outputMarkdown
106+
markdown: outputMarkdown,
107+
repoName,
108+
repoDescription,
109+
homepage,
110+
defaultBranch,
111+
visibility
69112
} = cli.flags
70113

71-
if (!cli.input[0]) {
72-
throw new InputError(`Please specify an organization slug. \n
73-
Example:
74-
socket scan list FakeOrg
75-
`)
114+
const [orgSlug = ''] = cli.input
115+
116+
if (!orgSlug) {
117+
console.error(`${chalk.bgRed('Input error')}: Please provide an organization slug and repository name \n`)
118+
cli.showHelp()
119+
return
76120
}
77121

78-
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+
}
79127

80128
return {
81129
outputJson,
82130
outputMarkdown,
83-
orgSlug
131+
orgSlug,
132+
name: repoName,
133+
description: repoDescription,
134+
homepage,
135+
default_branch: defaultBranch,
136+
visibility
84137
}
85138
}
86139

87140
/**
88141
* @typedef RepositoryData
89-
* @property {import('@socketsecurity/sdk').SocketSdkReturnType<'getOrgFullScanList'>["data"]} data
142+
* @property {import('@socketsecurity/sdk').SocketSdkReturnType<'updateOrgRepo'>["data"]} data
90143
*/
91144

92145
/**
@@ -96,10 +149,19 @@ socket scan list FakeOrg
96149
* @returns {Promise<void|RepositoryData>}
97150
*/
98151
async function updateRepository (orgSlug, input, spinner) {
99-
// const socketSdk = await setupSdk(getDefaultKey())
100-
console.log(input)
152+
const socketSdk = await setupSdk(getDefaultKey())
153+
// @ts-ignore
154+
const result = await handleApiCall(socketSdk.updateOrgRepo(orgSlug, input.name, input), 'listing repositories')
155+
156+
if (!result.success) {
157+
return handleUnsuccessfulApiResponse('updateOrgRepo', result, spinner)
158+
}
159+
160+
spinner.stop()
161+
162+
console.log('\n✅ Repository updated successfully \n')
101163

102-
// return {
103-
// // data: result.data
104-
// }
164+
return {
165+
data: result.data
166+
}
105167
}

lib/commands/repos/view.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ function setupCommand (name, description, argv, importMeta) {
7070
} = cli.flags
7171

7272
if (!cli.input[0]) {
73-
console.error(`${chalk.bgRed('Input error')}: Please provide an organization slug \n`)
73+
console.error(`${chalk.bgRed('Input error')}: Please provide an organization slug and repository name \n`)
7474
cli.showHelp()
7575
return
7676
}

0 commit comments

Comments
 (0)