Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion packages/indexer-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,14 @@
"isomorphic-fetch": "3.0.0",
"table": "6.7.5",
"yaml": "1.10.2",
"wrap-ansi": "7.0.0"
"wrap-ansi": "7.0.0",
"yargs-parser": "21.1.1"
},
"devDependencies": {
"@types/isomorphic-fetch": "0.0.36",
"@types/lodash.clonedeep": "^4.5.7",
"@types/wrap-ansi": "3.0.0",
"@types/yargs-parser": "21.0.3",
"@typescript-eslint/eslint-plugin": "6.7.0",
"@typescript-eslint/parser": "6.7.0",
"eslint": "8.49.0",
Expand Down
22 changes: 22 additions & 0 deletions packages/indexer-cli/src/command-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

import { table, getBorderCharacters } from 'table'
import wrapAnsi from 'wrap-ansi'
import yargsParse from 'yargs-parser'

export enum OutputFormat {
Table = 'table',
Expand Down Expand Up @@ -64,6 +65,27 @@ export const fixParameters = (
}
}

/**
* Re-parses process.argv with number parsing disabled to recover positional
* arguments as strings. gluegun uses yargs-parser internally and converts hex
* strings (e.g. 0x0, 0x...0010) to numbers before our commands see them,
* making the conversion irreversible. Re-parsing with 'parse-numbers: false'
* and taking the last N positionals (N = parametersArray.length) restores
* the original strings, since gluegun strips the subcommand prefix from
* parameters.array but our re-parse includes it.
*/
export function getRawPositionalArgs(parametersArray: unknown[]): string[] {
const raw = yargsParse(process.argv.slice(2), {
configuration: { 'parse-numbers': false },
})
// raw._ includes the full subcommand path (e.g. ['indexer', 'allocations', 'close', ...])
// while parameters.array has already had the command path stripped by gluegun.
// Taking the last N elements (N = parameters.array.length) removes the command path
// and gives us only the user-provided positional arguments.
const positionals = raw._ as string[]
return positionals.slice(positionals.length - parametersArray.length)
}

export const formatData = (
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
data: any,
Expand Down
3 changes: 2 additions & 1 deletion packages/indexer-cli/src/commands/indexer/actions/queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { createIndexerManagementClient } from '../../../client'
import {
requireProtocolNetworkOption,
printObjectOrArray,
getRawPositionalArgs,
} from '../../../command-helpers'
import { buildActionInput, queueActions, validateActionType } from '../../../actions'
import {
Expand Down Expand Up @@ -67,7 +68,7 @@ module.exports = {
}

const [type, targetDeployment, param1, param2, param3, param4, param5, param6] =
parameters.array || []
getRawPositionalArgs(parameters.array || [])

let actionInputParams: ActionInput
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
validatePOI,
printObjectOrArray,
extractProtocolNetworkOption,
getRawPositionalArgs,
} from '../../../command-helpers'

const HELP = `
Expand Down Expand Up @@ -58,7 +59,7 @@ module.exports = {
}

const [id, unformattedPoi, unformattedBlockNumber, unformattedPublicPOI] =
parameters.array || []
getRawPositionalArgs(parameters.array || [])

if (id === undefined) {
spinner.fail(`Missing required argument: 'id'`)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import chalk from 'chalk'
import { loadValidatedConfig } from '../../../config'
import { createIndexerManagementClient } from '../../../client'
import { submitCollectReceiptsJob } from '../../../allocations'
import { extractProtocolNetworkOption } from '../../../command-helpers'
import {
extractProtocolNetworkOption,
getRawPositionalArgs,
} from '../../../command-helpers'

const HELP = `
${chalk.bold('graph indexer allocations collect')} [options] <network> <id>
Expand Down Expand Up @@ -41,7 +44,7 @@ module.exports = {
return
}

const [id] = parameters.array || []
const [id] = getRawPositionalArgs(parameters.array || [])

if (id === undefined) {
spinner.fail(`Missing required argument: 'id'`)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { processIdentifier, SubgraphIdentifierType } from '@graphprotocol/indexe
import {
extractProtocolNetworkOption,
printObjectOrArray,
getRawPositionalArgs,
} from '../../../command-helpers'

const HELP = `
Expand Down Expand Up @@ -49,7 +50,7 @@ module.exports = {
return
}

const [deploymentID, amount, indexNode] = parameters.array || []
const [deploymentID, amount, indexNode] = getRawPositionalArgs(parameters.array || [])

try {
const protocolNetwork = extractProtocolNetworkOption(parameters.options, true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
extractProtocolNetworkOption,
printObjectOrArray,
validatePOI,
getRawPositionalArgs,
} from '../../../command-helpers'

const HELP = `
Expand Down Expand Up @@ -62,7 +63,7 @@ module.exports = {
}

const [id, unformattedPoi, unformattedBlockNumber, unformattedPublicPOI] =
parameters.array || []
getRawPositionalArgs(parameters.array || [])

if (id === undefined) {
spinner.fail(`Missing required argument: 'id'`)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
extractProtocolNetworkOption,
printObjectOrArray,
validatePOI,
getRawPositionalArgs,
} from '../../../command-helpers'

const HELP = `
Expand Down Expand Up @@ -57,8 +58,9 @@ module.exports = {
return
}

// eslint-disable-next-line prefer-const
let [id, amount, poi, unformattedBlockNumber, publicPOI] = parameters.array || []
const [id, amount, poi, unformattedBlockNumber, publicPOI] = getRawPositionalArgs(
parameters.array || [],
)

if (id === undefined) {
spinner.fail(`Missing required argument: 'id'`)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { resizeAllocation } from '../../../allocations'
import {
extractProtocolNetworkOption,
printObjectOrArray,
getRawPositionalArgs,
} from '../../../command-helpers'

const HELP = `
Expand Down Expand Up @@ -54,7 +55,7 @@ module.exports = {
return
}

const [id, amount] = parameters.array || []
const [id, amount] = getRawPositionalArgs(parameters.array || [])

if (id === undefined) {
spinner.fail(`Missing required argument: 'id'`)
Expand Down
10 changes: 8 additions & 2 deletions packages/indexer-cli/src/commands/indexer/disputes/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ import chalk from 'chalk'
import { loadValidatedConfig } from '../../../config'
import { createIndexerManagementClient } from '../../../client'
import { disputes, printDisputes } from '../../../disputes'
import { parseOutputFormat, extractProtocolNetworkOption } from '../../../command-helpers'
import {
parseOutputFormat,
extractProtocolNetworkOption,
getRawPositionalArgs,
} from '../../../command-helpers'

const HELP = `
${chalk.bold(
Expand All @@ -26,7 +30,9 @@ module.exports = {
const { print, parameters } = toolbox

const { h, help, o, output } = parameters.options
const [status, minAllocationClosedEpoch] = parameters.array || []
const [status, minAllocationClosedEpoch] = getRawPositionalArgs(
parameters.array || [],
)
const outputFormat = parseOutputFormat(print, o || output || 'table')

if (help || h) {
Expand Down
7 changes: 5 additions & 2 deletions packages/indexer-cli/src/commands/indexer/provision/add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ import chalk from 'chalk'

import { loadValidatedConfig } from '../../../config'
import { createIndexerManagementClient } from '../../../client'
import { extractProtocolNetworkOption } from '../../../command-helpers'
import {
extractProtocolNetworkOption,
getRawPositionalArgs,
} from '../../../command-helpers'
import gql from 'graphql-tag'
import { IndexerProvision, printIndexerProvisions } from '../../../provisions'

Expand Down Expand Up @@ -35,7 +38,7 @@ module.exports = {
return
}

const [amount] = parameters.array || []
const [amount] = getRawPositionalArgs(parameters.array || [])

try {
if (!amount) {
Expand Down
7 changes: 5 additions & 2 deletions packages/indexer-cli/src/commands/indexer/provision/thaw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ import chalk from 'chalk'

import { loadValidatedConfig } from '../../../config'
import { createIndexerManagementClient } from '../../../client'
import { extractProtocolNetworkOption } from '../../../command-helpers'
import {
extractProtocolNetworkOption,
getRawPositionalArgs,
} from '../../../command-helpers'
import gql from 'graphql-tag'
import { IndexerProvision, printIndexerProvisions } from '../../../provisions'

Expand Down Expand Up @@ -35,7 +38,7 @@ module.exports = {
return
}

const [amount] = parameters.array || []
const [amount] = getRawPositionalArgs(parameters.array || [])

try {
if (!amount) {
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2852,7 +2852,7 @@
dependencies:
"@types/node" "*"

"@types/yargs-parser@*":
"@types/yargs-parser@*", "@types/yargs-parser@21.0.3":
version "21.0.3"
resolved "https://registry.yarnpkg.com/@types/yargs-parser/-/yargs-parser-21.0.3.tgz#815e30b786d2e8f0dcd85fd5bcf5e1a04d008f15"
integrity sha512-I4q9QU9MQv4oEOz4tAHJtNz1cwuLxn2F3xcc2iV5WdqLPpUnj30aUuxt1mAxYTG+oe8CZMV/+6rU4S4gRDzqtQ==
Expand Down
Loading