@socketsecurity/cli 0.9.3 → 0.10.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,30 @@
1
+ import { create } from './create.js'
2
+ import { del } from './delete.js'
3
+ import { list } from './list.js'
4
+ import { update } from './update.js'
5
+ import { view } from './view.js'
6
+ import { meowWithSubcommands } from '../../utils/meow-with-subcommands.js'
7
+
8
+ const description = 'Repositories related commands'
9
+
10
+ /** @type {import('../../utils/meow-with-subcommands.js').CliSubcommand} */
11
+ export const repo = {
12
+ description,
13
+ run: async (argv, importMeta, { parentName }) => {
14
+ await meowWithSubcommands(
15
+ {
16
+ create,
17
+ view,
18
+ list,
19
+ del,
20
+ update
21
+ },
22
+ {
23
+ argv,
24
+ description,
25
+ importMeta,
26
+ name: parentName + ' repo',
27
+ }
28
+ )
29
+ }
30
+ }
@@ -0,0 +1,170 @@
1
+ /* eslint-disable no-console */
2
+
3
+ import chalk from 'chalk'
4
+ // @ts-ignore
5
+ import chalkTable from 'chalk-table'
6
+ import meow from 'meow'
7
+ import ora from 'ora'
8
+
9
+ import { outputFlags } from '../../flags/index.js'
10
+ import { handleApiCall, handleUnsuccessfulApiResponse } from '../../utils/api-helpers.js'
11
+ import { prepareFlags } from '../../utils/flags.js'
12
+ import { printFlagList } from '../../utils/formatting.js'
13
+ import { getDefaultKey, setupSdk } from '../../utils/sdk.js'
14
+
15
+ /** @type {import('../../utils/meow-with-subcommands.js').CliSubcommand} */
16
+ export const list = {
17
+ description: 'List repositories in an organization',
18
+ async run (argv, importMeta, { parentName }) {
19
+ const name = parentName + ' list'
20
+
21
+ const input = setupCommand(name, list.description, argv, importMeta)
22
+ if (input) {
23
+ const spinnerText = 'Listing repositories... \n'
24
+ const spinner = ora(spinnerText).start()
25
+ await listOrgRepos(input.orgSlug, input, spinner)
26
+ }
27
+ }
28
+ }
29
+
30
+ const listRepoFlags = prepareFlags({
31
+ sort: {
32
+ type: 'string',
33
+ shortFlag: 's',
34
+ default: 'created_at',
35
+ description: 'Sorting option',
36
+ },
37
+ direction: {
38
+ type: 'string',
39
+ default: 'desc',
40
+ description: 'Direction option',
41
+ },
42
+ perPage: {
43
+ type: 'number',
44
+ shortFlag: 'pp',
45
+ default: 30,
46
+ description: 'Number of results per page'
47
+ },
48
+ page: {
49
+ type: 'number',
50
+ shortFlag: 'p',
51
+ default: 1,
52
+ description: 'Page number'
53
+ },
54
+ })
55
+
56
+ // Internal functions
57
+
58
+ /**
59
+ * @typedef CommandContext
60
+ * @property {boolean} outputJson
61
+ * @property {boolean} outputMarkdown
62
+ * @property {string} orgSlug
63
+ * @property {string} sort
64
+ * @property {string} direction
65
+ * @property {number} per_page
66
+ * @property {number} page
67
+ */
68
+
69
+ /**
70
+ * @param {string} name
71
+ * @param {string} description
72
+ * @param {readonly string[]} argv
73
+ * @param {ImportMeta} importMeta
74
+ * @returns {void|CommandContext}
75
+ */
76
+ function setupCommand (name, description, argv, importMeta) {
77
+ const flags = {
78
+ ...outputFlags,
79
+ ...listRepoFlags
80
+ }
81
+
82
+ const cli = meow(`
83
+ Usage
84
+ $ ${name} <org slug>
85
+
86
+ Options
87
+ ${printFlagList(flags, 6)}
88
+
89
+ Examples
90
+ $ ${name} FakeOrg
91
+ `, {
92
+ argv,
93
+ description,
94
+ importMeta,
95
+ flags
96
+ })
97
+
98
+ const {
99
+ json: outputJson,
100
+ markdown: outputMarkdown,
101
+ perPage,
102
+ sort,
103
+ direction,
104
+ page
105
+ } = cli.flags
106
+
107
+ if (!cli.input[0]) {
108
+ console.error(`${chalk.bgRed('Input error')}: Please provide an organization slug \n`)
109
+ cli.showHelp()
110
+ return
111
+ }
112
+
113
+ const [orgSlug = ''] = cli.input
114
+
115
+ return {
116
+ outputJson,
117
+ outputMarkdown,
118
+ orgSlug,
119
+ sort,
120
+ direction,
121
+ page,
122
+ per_page: perPage
123
+ }
124
+ }
125
+
126
+ /**
127
+ * @typedef RepositoryData
128
+ * @property {import('@socketsecurity/sdk').SocketSdkReturnType<'getOrgRepoList'>["data"]} data
129
+ */
130
+
131
+ /**
132
+ * @param {string} orgSlug
133
+ * @param {CommandContext} input
134
+ * @param {import('ora').Ora} spinner
135
+ * @returns {Promise<void|RepositoryData>}
136
+ */
137
+ async function listOrgRepos (orgSlug, input, spinner) {
138
+ const socketSdk = await setupSdk(getDefaultKey())
139
+ const result = await handleApiCall(socketSdk.getOrgRepoList(orgSlug, input), 'listing repositories')
140
+
141
+ if (!result.success) {
142
+ return handleUnsuccessfulApiResponse('getOrgRepoList', result, spinner)
143
+ }
144
+
145
+ spinner.stop()
146
+
147
+ const options = {
148
+ columns: [
149
+ { field: 'id', name: chalk.magenta('ID') },
150
+ { field: 'name', name: chalk.magenta('Name') },
151
+ { field: 'visibility', name: chalk.magenta('Visibility') },
152
+ { field: 'default_branch', name: chalk.magenta('Default branch') },
153
+ { field: 'archived', name: chalk.magenta('Archived') }
154
+ ]
155
+ }
156
+
157
+ const formattedResults = result.data.results.map(d => {
158
+ return {
159
+ ...d
160
+ }
161
+ })
162
+
163
+ const table = chalkTable(options, formattedResults)
164
+
165
+ console.log(table, '\n')
166
+
167
+ return {
168
+ data: result.data
169
+ }
170
+ }
@@ -0,0 +1,166 @@
1
+ /* eslint-disable no-console */
2
+
3
+ import chalk from 'chalk'
4
+ import meow from 'meow'
5
+ import ora from 'ora'
6
+
7
+ import { outputFlags } from '../../flags/index.js'
8
+ import { handleApiCall, handleUnsuccessfulApiResponse } from '../../utils/api-helpers.js'
9
+ import { prepareFlags } from '../../utils/flags.js'
10
+ import { printFlagList } from '../../utils/formatting.js'
11
+ import { getDefaultKey, setupSdk } from '../../utils/sdk.js'
12
+
13
+ /** @type {import('../../utils/meow-with-subcommands.js').CliSubcommand} */
14
+ export const update = {
15
+ description: 'Update a repository in an organization',
16
+ async run (argv, importMeta, { parentName }) {
17
+ const name = parentName + ' update'
18
+
19
+ const input = setupCommand(name, update.description, argv, importMeta)
20
+ if (input) {
21
+ const spinnerText = 'Updating repository... \n'
22
+ const spinner = ora(spinnerText).start()
23
+ await updateRepository(input.orgSlug, input, spinner)
24
+ }
25
+ }
26
+ }
27
+
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
+
61
+ // Internal functions
62
+
63
+ /**
64
+ * @typedef CommandContext
65
+ * @property {boolean} outputJson
66
+ * @property {boolean} outputMarkdown
67
+ * @property {string} orgSlug
68
+ * @property {string} name
69
+ * @property {string} description
70
+ * @property {string} homepage
71
+ * @property {string} default_branch
72
+ * @property {string} visibility
73
+ */
74
+
75
+ /**
76
+ * @param {string} name
77
+ * @param {string} description
78
+ * @param {readonly string[]} argv
79
+ * @param {ImportMeta} importMeta
80
+ * @returns {void|CommandContext}
81
+ */
82
+ function setupCommand (name, description, argv, importMeta) {
83
+ const flags = {
84
+ ...outputFlags,
85
+ ...repositoryUpdateFlags
86
+ }
87
+
88
+ const cli = meow(`
89
+ Usage
90
+ $ ${name} <org slug>
91
+
92
+ Options
93
+ ${printFlagList(flags, 6)}
94
+
95
+ Examples
96
+ $ ${name} FakeOrg
97
+ `, {
98
+ argv,
99
+ description,
100
+ importMeta,
101
+ flags
102
+ })
103
+
104
+ const {
105
+ json: outputJson,
106
+ markdown: outputMarkdown,
107
+ repoName,
108
+ repoDescription,
109
+ homepage,
110
+ defaultBranch,
111
+ visibility
112
+ } = cli.flags
113
+
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
120
+ }
121
+
122
+ if (!repoName) {
123
+ console.error(`${chalk.bgRed('Input error')}: Repository name is required. \n`)
124
+ cli.showHelp()
125
+ return
126
+ }
127
+
128
+ return {
129
+ outputJson,
130
+ outputMarkdown,
131
+ orgSlug,
132
+ name: repoName,
133
+ description: repoDescription,
134
+ homepage,
135
+ default_branch: defaultBranch,
136
+ visibility
137
+ }
138
+ }
139
+
140
+ /**
141
+ * @typedef RepositoryData
142
+ * @property {import('@socketsecurity/sdk').SocketSdkReturnType<'updateOrgRepo'>["data"]} data
143
+ */
144
+
145
+ /**
146
+ * @param {string} orgSlug
147
+ * @param {CommandContext} input
148
+ * @param {import('ora').Ora} spinner
149
+ * @returns {Promise<void|RepositoryData>}
150
+ */
151
+ async function updateRepository (orgSlug, input, spinner) {
152
+ const socketSdk = await setupSdk(getDefaultKey())
153
+ const result = await handleApiCall(socketSdk.updateOrgRepo(orgSlug, input.name, input), 'updating repository')
154
+
155
+ if (!result.success) {
156
+ return handleUnsuccessfulApiResponse('updateOrgRepo', result, spinner)
157
+ }
158
+
159
+ spinner.stop()
160
+
161
+ console.log('\n✅ Repository updated successfully \n')
162
+
163
+ return {
164
+ data: result.data
165
+ }
166
+ }
@@ -0,0 +1,128 @@
1
+ /* eslint-disable no-console */
2
+
3
+ import chalk from 'chalk'
4
+ // @ts-ignore
5
+ import chalkTable from 'chalk-table'
6
+ import meow from 'meow'
7
+ import ora from 'ora'
8
+
9
+ import { outputFlags } from '../../flags/index.js'
10
+ import { handleApiCall, handleUnsuccessfulApiResponse } from '../../utils/api-helpers.js'
11
+ import { printFlagList } from '../../utils/formatting.js'
12
+ import { getDefaultKey, setupSdk } from '../../utils/sdk.js'
13
+
14
+ /** @type {import('../../utils/meow-with-subcommands.js').CliSubcommand} */
15
+ export const view = {
16
+ description: 'View repositories in an organization',
17
+ async run (argv, importMeta, { parentName }) {
18
+ const name = parentName + ' view'
19
+
20
+ const input = setupCommand(name, view.description, argv, importMeta)
21
+ if (input) {
22
+ const spinnerText = 'Fetching repository... \n'
23
+ const spinner = ora(spinnerText).start()
24
+ await viewRepository(input.orgSlug, input.repositoryName, spinner)
25
+ }
26
+ }
27
+ }
28
+
29
+ // Internal functions
30
+
31
+ /**
32
+ * @typedef CommandContext
33
+ * @property {boolean} outputJson
34
+ * @property {boolean} outputMarkdown
35
+ * @property {string} orgSlug
36
+ * @property {string} repositoryName
37
+ */
38
+
39
+ /**
40
+ * @param {string} name
41
+ * @param {string} description
42
+ * @param {readonly string[]} argv
43
+ * @param {ImportMeta} importMeta
44
+ * @returns {void|CommandContext}
45
+ */
46
+ function setupCommand (name, description, argv, importMeta) {
47
+ const flags = {
48
+ ...outputFlags
49
+ }
50
+
51
+ const cli = meow(`
52
+ Usage
53
+ $ ${name} <org slug>
54
+
55
+ Options
56
+ ${printFlagList(flags, 6)}
57
+
58
+ Examples
59
+ $ ${name} FakeOrg
60
+ `, {
61
+ argv,
62
+ description,
63
+ importMeta,
64
+ flags
65
+ })
66
+
67
+ const {
68
+ json: outputJson,
69
+ markdown: outputMarkdown
70
+ } = cli.flags
71
+
72
+ if (!cli.input[0]) {
73
+ console.error(`${chalk.bgRed('Input error')}: Please provide an organization slug and repository name \n`)
74
+ cli.showHelp()
75
+ return
76
+ }
77
+
78
+ const [orgSlug = '', repositoryName = ''] = cli.input
79
+
80
+ return {
81
+ outputJson,
82
+ outputMarkdown,
83
+ orgSlug,
84
+ repositoryName
85
+ }
86
+ }
87
+
88
+ /**
89
+ * @typedef RepositoryData
90
+ * @property {import('@socketsecurity/sdk').SocketSdkReturnType<'getOrgRepo'>["data"]} data
91
+ */
92
+
93
+ /**
94
+ * @param {string} orgSlug
95
+ * @param {string} repoName
96
+ * @param {import('ora').Ora} spinner
97
+ * @returns {Promise<void|RepositoryData>}
98
+ */
99
+ async function viewRepository (orgSlug, repoName, spinner) {
100
+ const socketSdk = await setupSdk(getDefaultKey())
101
+ const result = await handleApiCall(socketSdk.getOrgRepo(orgSlug, repoName), 'fetching repository')
102
+
103
+ if (!result.success) {
104
+ return handleUnsuccessfulApiResponse('getOrgRepo', result, spinner)
105
+ }
106
+
107
+ spinner.stop()
108
+
109
+ const options = {
110
+ columns: [
111
+ { field: 'id', name: chalk.magenta('ID') },
112
+ { field: 'name', name: chalk.magenta('Name') },
113
+ { field: 'visibility', name: chalk.magenta('Visibility') },
114
+ { field: 'default_branch', name: chalk.magenta('Default branch') },
115
+ { field: 'homepage', name: chalk.magenta('Homepage') },
116
+ { field: 'archived', name: chalk.magenta('Archived') },
117
+ { field: 'created_at', name: chalk.magenta('Created at') }
118
+ ]
119
+ }
120
+
121
+ const table = chalkTable(options, [result.data])
122
+
123
+ console.log(table, '\n')
124
+
125
+ return {
126
+ data: result.data
127
+ }
128
+ }