From 8c09feb423be4b87d81a5980e0a0dfe2fa426e19 Mon Sep 17 00:00:00 2001 From: hopeyen Date: Wed, 30 Nov 2022 13:54:51 -0600 Subject: [PATCH] indexer-cli: add delete options all and status filter --- .../src/commands/indexer/actions/delete.ts | 46 +++++++++++++++---- 1 file changed, 36 insertions(+), 10 deletions(-) diff --git a/packages/indexer-cli/src/commands/indexer/actions/delete.ts b/packages/indexer-cli/src/commands/indexer/actions/delete.ts index 4b4cf9181..84940bf0e 100644 --- a/packages/indexer-cli/src/commands/indexer/actions/delete.ts +++ b/packages/indexer-cli/src/commands/indexer/actions/delete.ts @@ -4,27 +4,29 @@ import chalk from 'chalk' import { loadValidatedConfig } from '../../../config' import { createIndexerManagementClient } from '../../../client' import { fixParameters } from '../../../command-helpers' -import { deleteActions } from '../../../actions' +import { deleteActions, fetchActions } from '../../../actions' const HELP = ` +${chalk.bold('graph indexer actions delete')} [options] all ${chalk.bold('graph indexer actions delete')} [options] [ ...] ${chalk.dim('Options:')} - -h, --help Show usage information - -o, --output table|json|yaml Choose the output format: table (default), JSON, or YAML + -h, --help Show usage information + --status queued|approved|pending|success|failed|canceled Filter by status + -o, --output table|json|yaml Choose the output format: table (default), JSON, or YAML ` module.exports = { name: 'delete', alias: [], - description: 'Delete an item in the queue', + description: 'Delete one or many actions in the queue', run: async (toolbox: GluegunToolbox) => { const { print, parameters } = toolbox const inputSpinner = toolbox.print.spin('Processing inputs') - const { h, help, o, output } = parameters.options + const { status, h, help, o, output } = parameters.options const [...actionIDs] = fixParameters(parameters, { h, help }) || [] const outputFormat = o || output || 'table' @@ -35,8 +37,6 @@ module.exports = { return } - let numericActionIDs: number[] - try { if (!['json', 'yaml', 'table'].includes(outputFormat)) { throw Error( @@ -44,11 +44,30 @@ module.exports = { ) } - if (!actionIDs || actionIDs.length === 0) { - throw Error(`Missing required argument: 'actionID'`) + if ( + status && + !['queued', 'approved', 'pending', 'success', 'failed', 'canceled'].includes( + status, + ) + ) { + throw Error( + `Invalid '--status' provided, must be one of ['queued', 'approved', 'pending', 'success', 'failed', 'canceled]`, + ) + } + + if (actionIDs[0] == 'all') { + if (status || actionIDs.length > 1) { + throw Error( + `Invalid query, cannot specify '--status' filter or multiple ids in addition to 'action = all'`, + ) + } } - numericActionIDs = actionIDs.map(action => +action) + if (!status && (!actionIDs || actionIDs.length === 0)) { + throw Error( + `Required at least one argument: actionID(s), 'all', or '--status' filter`, + ) + } inputSpinner.succeed('Processed input parameters') } catch (error) { @@ -63,6 +82,13 @@ module.exports = { const config = loadValidatedConfig() const client = await createIndexerManagementClient({ url: config.api }) + const numericActionIDs: number[] = + actionIDs[0] == 'all' + ? (await fetchActions(client, {})).map(action => action.id) + : status + ? (await fetchActions(client, { status })).map(action => action.id) + : actionIDs.map(action => +action) + const numDeleted = await deleteActions(client, numericActionIDs) actionSpinner.succeed(`${numDeleted} actions deleted`)