Skip to content

Commit

Permalink
indexer-cli: add delete options all and status filter
Browse files Browse the repository at this point in the history
  • Loading branch information
hopeyen committed Nov 30, 2022
1 parent 546d72c commit 8c09feb
Showing 1 changed file with 36 additions and 10 deletions.
46 changes: 36 additions & 10 deletions packages/indexer-cli/src/commands/indexer/actions/delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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] [<actionID1> ...]
${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'
Expand All @@ -35,20 +37,37 @@ module.exports = {
return
}

let numericActionIDs: number[]

try {
if (!['json', 'yaml', 'table'].includes(outputFormat)) {
throw Error(
`Invalid output format "${outputFormat}", must be one of ['json', 'yaml', 'table']`,
)
}

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) {
Expand All @@ -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`)
Expand Down

0 comments on commit 8c09feb

Please sign in to comment.