Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
charliegerard committed Jun 15, 2024
1 parent a430fc8 commit 2aa7a8b
Show file tree
Hide file tree
Showing 7 changed files with 615 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/commands/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
164 changes: 164 additions & 0 deletions lib/commands/repos/create.js
Original file line number Diff line number Diff line change
@@ -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} <org slug>
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<void|FullScansData>}
*/
async function createRepo (orgSlug, input, spinner) {
// const socketSdk = await setupSdk(getDefaultKey())
console.log(input)

// return {
// // data: result.data
// }
}
105 changes: 105 additions & 0 deletions lib/commands/repos/delete.js
Original file line number Diff line number Diff line change
@@ -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} <org slug>
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<void|RepositoryData>}
*/
async function deleteRepository (orgSlug, input, spinner) {
// const socketSdk = await setupSdk(getDefaultKey())
console.log(input)

// return {
// // data: result.data
// }
}
31 changes: 31 additions & 0 deletions lib/commands/repos/index.js
Original file line number Diff line number Diff line change
@@ -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',
}
)
}
}
Loading

0 comments on commit 2aa7a8b

Please sign in to comment.