From 2aa7a8b783ae8fbe879445638c697b51f45968d7 Mon Sep 17 00:00:00 2001 From: Charlie Gerard Date: Sat, 15 Jun 2024 14:28:18 -0700 Subject: [PATCH] wip --- lib/commands/index.js | 1 + lib/commands/repos/create.js | 164 +++++++++++++++++++++++++++++++++++ lib/commands/repos/delete.js | 105 ++++++++++++++++++++++ lib/commands/repos/index.js | 31 +++++++ lib/commands/repos/list.js | 105 ++++++++++++++++++++++ lib/commands/repos/update.js | 105 ++++++++++++++++++++++ lib/commands/repos/view.js | 104 ++++++++++++++++++++++ 7 files changed, 615 insertions(+) create mode 100644 lib/commands/repos/create.js create mode 100644 lib/commands/repos/delete.js create mode 100644 lib/commands/repos/index.js create mode 100644 lib/commands/repos/list.js create mode 100644 lib/commands/repos/update.js create mode 100644 lib/commands/repos/view.js diff --git a/lib/commands/index.js b/lib/commands/index.js index aae0d337..81a37e45 100644 --- a/lib/commands/index.js +++ b/lib/commands/index.js @@ -7,3 +7,4 @@ export * from './logout/index.js' export * from './wrapper/index.js' export * from './raw-npm/index.js' export * from './raw-npx/index.js' +export * from './repos/index.js' diff --git a/lib/commands/repos/create.js b/lib/commands/repos/create.js new file mode 100644 index 00000000..39d91721 --- /dev/null +++ b/lib/commands/repos/create.js @@ -0,0 +1,164 @@ +// @ts-nocheck +/* eslint-disable no-console */ + +// import chalk from 'chalk' +import meow from 'meow' +import ora from 'ora' + +import { outputFlags } from '../../flags/index.js' +// import { handleApiCall, handleUnsuccessfulApiResponse } from '../../utils/api-helpers.js' +import { InputError } from '../../utils/errors.js' +import { prepareFlags } from '../../utils/flags.js' +import { printFlagList } from '../../utils/formatting.js' +// import { getDefaultKey, setupSdk } from '../../utils/sdk.js' + +/** @type {import('../../utils/meow-with-subcommands').CliSubcommand} */ +export const create = { + description: 'Create a repository in an organization', + async run (argv, importMeta, { parentName }) { + const name = parentName + ' create' + + const input = setupCommand(name, create.description, argv, importMeta) + if (input) { + const spinnerText = 'Creating repository... \n' + const spinner = ora(spinnerText).start() + await createRepo(input.orgSlug, input, spinner) + } + } +} + +const listFullScanFlags = prepareFlags({ + sort: { + type: 'string', + shortFlag: 's', + default: 'created_at', + description: 'Sorting option (`name` or `created_at`) - default is `created_at`', + }, + direction: { + type: 'string', + shortFlag: 'd', + default: 'desc', + description: 'Direction option (`desc` or `asc`) - Default is `desc`', + }, + perPage: { + type: 'number', + shortFlag: 'pp', + default: 30, + description: 'Results per page - Default is 30', + }, + page: { + type: 'number', + shortFlag: 'p', + default: 1, + description: 'Page number - Default is 1', + }, + fromTime: { + type: 'string', + shortFlag: 'f', + default: '', + description: 'From time - as a unix timestamp', + }, + untilTime: { + type: 'string', + shortFlag: 'u', + default: '', + description: 'Until time - as a unix timestamp', + } +}) + +// Internal functions + +/** + * @typedef CommandContext + * @property {boolean} outputJson + * @property {boolean} outputMarkdown + * @property {string} orgSlug + * @property {string} sort + * @property {string} direction + * @property {number} perPage + * @property {number} page + * @property {string} fromTime + * @property {string} untilTime + */ + +/** + * @param {string} name + * @param {string} description + * @param {readonly string[]} argv + * @param {ImportMeta} importMeta + * @returns {void|CommandContext} + */ +function setupCommand (name, description, argv, importMeta) { + const flags = { + ...outputFlags, + ...listFullScanFlags + } + + const cli = meow(` + Usage + $ ${name} + + Options + ${printFlagList(flags, 6)} + + Examples + $ ${name} FakeOrg + `, { + argv, + description, + importMeta, + flags + }) + + const { + json: outputJson, + markdown: outputMarkdown, + sort, + direction, + perPage, + page, + fromTime, + untilTime + } = cli.flags + + if (!cli.input[0]) { + throw new InputError(`Please specify an organization slug. \n +Example: +socket scan list FakeOrg +`) + } + + const orgSlug = cli.input[0] || '' + + return { + outputJson, + outputMarkdown, + orgSlug, + sort, + direction, + perPage, + page, + fromTime, + untilTime + } +} + +/** + * @typedef FullScansData + * @property {import('@socketsecurity/sdk').SocketSdkReturnType<'getOrgFullScanList'>["data"]} data + */ + +/** + * @param {string} orgSlug + * @param {CommandContext} input + * @param {import('ora').Ora} spinner + * @returns {Promise} + */ +async function createRepo (orgSlug, input, spinner) { + // const socketSdk = await setupSdk(getDefaultKey()) + console.log(input) + +// return { +// // data: result.data +// } +} diff --git a/lib/commands/repos/delete.js b/lib/commands/repos/delete.js new file mode 100644 index 00000000..02369e2c --- /dev/null +++ b/lib/commands/repos/delete.js @@ -0,0 +1,105 @@ +// @ts-nocheck +/* eslint-disable no-console */ + +// import chalk from 'chalk' +import meow from 'meow' +import ora from 'ora' + +import { outputFlags } from '../../flags/index.js' +// import { handleApiCall, handleUnsuccessfulApiResponse } from '../../utils/api-helpers.js' +import { InputError } from '../../utils/errors.js' +import { printFlagList } from '../../utils/formatting.js' +// import { getDefaultKey, setupSdk } from '../../utils/sdk.js' + +/** @type {import('../../utils/meow-with-subcommands').CliSubcommand} */ +export const del = { + description: 'Delete a repository in an organization', + async run (argv, importMeta, { parentName }) { + const name = parentName + ' del' + + const input = setupCommand(name, del.description, argv, importMeta) + if (input) { + const spinnerText = 'Deleting repository... \n' + const spinner = ora(spinnerText).start() + await deleteRepository(input.orgSlug, input, spinner) + } + } +} + +// Internal functions + +/** + * @typedef CommandContext + * @property {boolean} outputJson + * @property {boolean} outputMarkdown + * @property {string} orgSlug + */ + +/** + * @param {string} name + * @param {string} description + * @param {readonly string[]} argv + * @param {ImportMeta} importMeta + * @returns {void|CommandContext} + */ +function setupCommand (name, description, argv, importMeta) { + const flags = { + ...outputFlags + } + + const cli = meow(` + Usage + $ ${name} + + Options + ${printFlagList(flags, 6)} + + Examples + $ ${name} FakeOrg + `, { + argv, + description, + importMeta, + flags + }) + + const { + json: outputJson, + markdown: outputMarkdown + } = cli.flags + + if (!cli.input[0]) { + throw new InputError(`Please specify an organization slug. \n +Example: +socket scan list FakeOrg +`) + } + + const orgSlug = cli.input[0] || '' + + return { + outputJson, + outputMarkdown, + orgSlug + } +} + +/** + * @typedef RepositoryData + * @property {import('@socketsecurity/sdk').SocketSdkReturnType<'getOrgFullScanList'>["data"]} data + */ + +/** + * @param {string} orgSlug + * @param {CommandContext} input + * @param {import('ora').Ora} spinner + * @returns {Promise} + */ +async function deleteRepository (orgSlug, input, spinner) { + // const socketSdk = await setupSdk(getDefaultKey()) + console.log(input) + +// return { +// // data: result.data +// } +} diff --git a/lib/commands/repos/index.js b/lib/commands/repos/index.js new file mode 100644 index 00000000..8c4c9053 --- /dev/null +++ b/lib/commands/repos/index.js @@ -0,0 +1,31 @@ +// @ts-nocheck +import { create } from './create.js' +import { del } from './delete.js' +import { list } from './list.js' +import { update } from './update.js' +import { view } from './view.js' +import { meowWithSubcommands } from '../../utils/meow-with-subcommands.js' + +const description = 'Repositories related commands' + +/** @type {import('../../utils/meow-with-subcommands').CliSubcommand} */ +export const repo = { + description, + run: async (argv, importMeta, { parentName }) => { + await meowWithSubcommands( + { + create, + view, + list, + del, + update + }, + { + argv, + description, + importMeta, + name: parentName + ' repo', + } + ) + } +} diff --git a/lib/commands/repos/list.js b/lib/commands/repos/list.js new file mode 100644 index 00000000..2fedcd70 --- /dev/null +++ b/lib/commands/repos/list.js @@ -0,0 +1,105 @@ +// @ts-nocheck +/* eslint-disable no-console */ + +// import chalk from 'chalk' +import meow from 'meow' +import ora from 'ora' + +import { outputFlags } from '../../flags/index.js' +// import { handleApiCall, handleUnsuccessfulApiResponse } from '../../utils/api-helpers.js' +import { InputError } from '../../utils/errors.js' +import { printFlagList } from '../../utils/formatting.js' +// import { getDefaultKey, setupSdk } from '../../utils/sdk.js' + +/** @type {import('../../utils/meow-with-subcommands').CliSubcommand} */ +export const list = { + description: 'List repositories in an organization', + async run (argv, importMeta, { parentName }) { + const name = parentName + ' list' + + const input = setupCommand(name, list.description, argv, importMeta) + if (input) { + const spinnerText = 'Listing repositories... \n' + const spinner = ora(spinnerText).start() + await listOrgRepos(input.orgSlug, input, spinner) + } + } +} + +// Internal functions + +/** + * @typedef CommandContext + * @property {boolean} outputJson + * @property {boolean} outputMarkdown + * @property {string} orgSlug + */ + +/** + * @param {string} name + * @param {string} description + * @param {readonly string[]} argv + * @param {ImportMeta} importMeta + * @returns {void|CommandContext} + */ +function setupCommand (name, description, argv, importMeta) { + const flags = { + ...outputFlags + } + + const cli = meow(` + Usage + $ ${name} + + Options + ${printFlagList(flags, 6)} + + Examples + $ ${name} FakeOrg + `, { + argv, + description, + importMeta, + flags + }) + + const { + json: outputJson, + markdown: outputMarkdown + } = cli.flags + + if (!cli.input[0]) { + throw new InputError(`Please specify an organization slug. \n +Example: +socket scan list FakeOrg +`) + } + + const orgSlug = cli.input[0] || '' + + return { + outputJson, + outputMarkdown, + orgSlug + } +} + +/** + * @typedef RepositoryData + * @property {import('@socketsecurity/sdk').SocketSdkReturnType<'getOrgFullScanList'>["data"]} data + */ + +/** + * @param {string} orgSlug + * @param {CommandContext} input + * @param {import('ora').Ora} spinner + * @returns {Promise} + */ +async function listOrgRepos (orgSlug, input, spinner) { + // const socketSdk = await setupSdk(getDefaultKey()) + console.log(input) + +// return { +// // data: result.data +// } +} diff --git a/lib/commands/repos/update.js b/lib/commands/repos/update.js new file mode 100644 index 00000000..2484d9cb --- /dev/null +++ b/lib/commands/repos/update.js @@ -0,0 +1,105 @@ +// @ts-nocheck +/* eslint-disable no-console */ + +// import chalk from 'chalk' +import meow from 'meow' +import ora from 'ora' + +import { outputFlags } from '../../flags/index.js' +// import { handleApiCall, handleUnsuccessfulApiResponse } from '../../utils/api-helpers.js' +import { InputError } from '../../utils/errors.js' +import { printFlagList } from '../../utils/formatting.js' +// import { getDefaultKey, setupSdk } from '../../utils/sdk.js' + +/** @type {import('../../utils/meow-with-subcommands').CliSubcommand} */ +export const update = { + description: 'Update a repository in an organization', + async run (argv, importMeta, { parentName }) { + const name = parentName + ' update' + + const input = setupCommand(name, update.description, argv, importMeta) + if (input) { + const spinnerText = 'Updating repository... \n' + const spinner = ora(spinnerText).start() + await updateRepository(input.orgSlug, input, spinner) + } + } +} + +// Internal functions + +/** + * @typedef CommandContext + * @property {boolean} outputJson + * @property {boolean} outputMarkdown + * @property {string} orgSlug + */ + +/** + * @param {string} name + * @param {string} description + * @param {readonly string[]} argv + * @param {ImportMeta} importMeta + * @returns {void|CommandContext} + */ +function setupCommand (name, description, argv, importMeta) { + const flags = { + ...outputFlags + } + + const cli = meow(` + Usage + $ ${name} + + Options + ${printFlagList(flags, 6)} + + Examples + $ ${name} FakeOrg + `, { + argv, + description, + importMeta, + flags + }) + + const { + json: outputJson, + markdown: outputMarkdown + } = cli.flags + + if (!cli.input[0]) { + throw new InputError(`Please specify an organization slug. \n +Example: +socket scan list FakeOrg +`) + } + + const orgSlug = cli.input[0] || '' + + return { + outputJson, + outputMarkdown, + orgSlug + } +} + +/** + * @typedef RepositoryData + * @property {import('@socketsecurity/sdk').SocketSdkReturnType<'getOrgFullScanList'>["data"]} data + */ + +/** + * @param {string} orgSlug + * @param {CommandContext} input + * @param {import('ora').Ora} spinner + * @returns {Promise} + */ +async function updateRepository (orgSlug, input, spinner) { + // const socketSdk = await setupSdk(getDefaultKey()) + console.log(input) + +// return { +// // data: result.data +// } +} diff --git a/lib/commands/repos/view.js b/lib/commands/repos/view.js new file mode 100644 index 00000000..e8c4fd0d --- /dev/null +++ b/lib/commands/repos/view.js @@ -0,0 +1,104 @@ +// @ts-nocheck +/* eslint-disable no-console */ + +import meow from 'meow' +import ora from 'ora' + +import { outputFlags } from '../../flags/index.js' +// import { handleApiCall, handleUnsuccessfulApiResponse } from '../../utils/api-helpers.js' +import { InputError } from '../../utils/errors.js' +import { printFlagList } from '../../utils/formatting.js' +// import { getDefaultKey, setupSdk } from '../../utils/sdk.js' + +/** @type {import('../../utils/meow-with-subcommands').CliSubcommand} */ +export const view = { + description: 'View repositories in an organization', + async run (argv, importMeta, { parentName }) { + const name = parentName + ' view' + + const input = setupCommand(name, view.description, argv, importMeta) + if (input) { + const spinnerText = 'Fetching repository... \n' + const spinner = ora(spinnerText).start() + await viewRepository(input.orgSlug, input, spinner) + } + } +} + +// Internal functions + +/** + * @typedef CommandContext + * @property {boolean} outputJson + * @property {boolean} outputMarkdown + * @property {string} orgSlug + */ + +/** + * @param {string} name + * @param {string} description + * @param {readonly string[]} argv + * @param {ImportMeta} importMeta + * @returns {void|CommandContext} + */ +function setupCommand (name, description, argv, importMeta) { + const flags = { + ...outputFlags + } + + const cli = meow(` + Usage + $ ${name} + + Options + ${printFlagList(flags, 6)} + + Examples + $ ${name} FakeOrg + `, { + argv, + description, + importMeta, + flags + }) + + const { + json: outputJson, + markdown: outputMarkdown + } = cli.flags + + if (!cli.input[0]) { + throw new InputError(`Please specify an organization slug. \n +Example: +socket scan list FakeOrg +`) + } + + const orgSlug = cli.input[0] || '' + + return { + outputJson, + outputMarkdown, + orgSlug + } +} + +/** + * @typedef RepositoryData + * @property {import('@socketsecurity/sdk').SocketSdkReturnType<'getOrgFullScanList'>["data"]} data + */ + +/** + * @param {string} orgSlug + * @param {CommandContext} input + * @param {import('ora').Ora} spinner + * @returns {Promise} + */ +async function viewRepository (orgSlug, input, spinner) { + // const socketSdk = await setupSdk(getDefaultKey()) + console.log(input) + +// return { +// // data: result.data +// } +}