Skip to content

Commit

Permalink
indexer-cli: replace print.highlight with chalk colors
Browse files Browse the repository at this point in the history
  • Loading branch information
hopeyen authored and hopeyen committed Mar 23, 2023
1 parent c38a73c commit 2e1e0fc
Show file tree
Hide file tree
Showing 19 changed files with 196 additions and 163 deletions.
27 changes: 27 additions & 0 deletions packages/indexer-cli/src/__tests__/indexer/rules.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,16 @@ describe('Indexer rules tests', () => {
})

describe('Rules get...', () => {
cliTest(
'Indexer rules output format error',
['indexer', 'rules', 'get', 'all', '--output', 'josn'],
'references/indexer-output-format',
{
expectedExitCode: 1,
cwd: baseDir,
timeout: 10000,
},
)
cliTest(
'Indexer rules get deployment - success',
['indexer', 'rules', 'get', 'QmZZtzZkfzCWMNrajxBf22q7BC9HzoT5iJUK3S8qA6zNZr'],
Expand Down Expand Up @@ -451,6 +461,23 @@ describe('Indexer rules tests', () => {
timeout: 10000,
},
)
cliTest(
'Indexer rules get deployment yaml - success',
[
'indexer',
'rules',
'get',
'QmZZtzZkfzCWMNrajxBf22q7BC9HzoT5iJUK3S8qA6zNZr',
'--output',
'yaml',
],
'references/indexer-rule-deployment-yaml',
{
expectedExitCode: 0,
cwd: baseDir,
timeout: 10000,
},
)
cliTest(
'Indexer rules get global - success',
['indexer', 'rules', 'get', 'global'],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Invalid output format "josn"
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
identifier: QmZZtzZkfzCWMNrajxBf22q7BC9HzoT5iJUK3S8qA6zNZr
identifierType: deployment
allocationAmount: null
allocationLifetime: null
autoRenewal: true
parallelAllocations: null
maxAllocationPercentage: null
minSignal: null
maxSignal: null
minStake: null
minAverageQueryFees: null
custom: null
decisionBasis: rules
requireSupported: true
safety: true
18 changes: 9 additions & 9 deletions packages/indexer-cli/src/allocations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import yaml from 'yaml'
import { GluegunPrint } from 'gluegun'
import { table, getBorderCharacters } from 'table'
import { BigNumber, utils } from 'ethers'
import { outputColors, pickFields } from './command-helpers'
import { OutputFormat, parseOutputFormat, pickFields } from './command-helpers'
import { IndexerManagementClient } from '@graphprotocol/indexer-common'
import gql from 'graphql-tag'
import {
Expand Down Expand Up @@ -109,14 +109,14 @@ export const indexerAllocationFromGraphQL = (

export const printIndexerAllocations = (
print: GluegunPrint,
outputFormat: 'table' | 'json' | 'yaml',
outputFormat: OutputFormat,
allocationOrAllocations:
| Partial<IndexerAllocation>
| Partial<IndexerAllocation>[]
| null,
keys: (keyof IndexerAllocation)[],
): void => {
outputColors(print, outputFormat)
parseOutputFormat(print, outputFormat)
if (Array.isArray(allocationOrAllocations)) {
const allocations = allocationOrAllocations.map(allocation =>
formatIndexerAllocation(pickFields(allocation, keys)),
Expand All @@ -131,12 +131,12 @@ export const printIndexerAllocations = (
}

export const displayIndexerAllocations = (
outputFormat: 'table' | 'json' | 'yaml',
outputFormat: OutputFormat,
allocations: Partial<IndexerAllocation>[],
): string =>
outputFormat === 'json'
outputFormat === OutputFormat.Json
? JSON.stringify(allocations, null, 2)
: outputFormat === 'yaml'
: outputFormat === OutputFormat.Yaml
? yaml.stringify(allocations).trim()
: allocations.length === 0
? 'No allocations found'
Expand All @@ -151,12 +151,12 @@ export const displayIndexerAllocations = (
).trim()

export const displayIndexerAllocation = (
outputFormat: 'table' | 'json' | 'yaml',
outputFormat: OutputFormat,
allocation: Partial<IndexerAllocation>,
): string =>
outputFormat === 'json'
outputFormat === OutputFormat.Json
? JSON.stringify(allocation, null, 2)
: outputFormat === 'yaml'
: outputFormat === OutputFormat.Yaml
? yaml.stringify(allocation).trim()
: table([Object.keys(allocation), Object.values(allocation)], {
border: getBorderCharacters('norc'),
Expand Down
53 changes: 32 additions & 21 deletions packages/indexer-cli/src/command-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@
// parameters array and returns the result of that.

import { table, getBorderCharacters } from 'table'

export enum OutputFormat {
Table = 'table',
Json = 'json',
Yaml = 'yaml',
}
import yaml from 'yaml'
import { GluegunParameters, GluegunPrint } from 'gluegun'
import { utils } from 'ethers'
Expand Down Expand Up @@ -59,11 +65,11 @@ export const fixParameters = (
export const formatData = (
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
data: any,
format: 'json' | 'yaml' | 'table',
format: OutputFormat,
): string =>
format === 'json'
format === OutputFormat.Json
? JSON.stringify(data, null, 2)
: format === 'yaml'
: format === OutputFormat.Yaml
? yaml.stringify(data).trim()
: Array.isArray(data)
? data.length === 0
Expand Down Expand Up @@ -97,26 +103,23 @@ export function pickFields(
return obj
}

export function displayObjectData(
outputFormat: 'table' | 'json' | 'yaml',
data: object,
): string {
return outputFormat === 'json'
export function displayObjectData(outputFormat: OutputFormat, data: object): string {
return outputFormat === OutputFormat.Json
? JSON.stringify(data, null, 2)
: outputFormat === 'yaml'
: outputFormat === OutputFormat.Yaml
? yaml.stringify(data).trim()
: table([Object.keys(data), Object.values(data)], {
border: getBorderCharacters('norc'),
}).trim()
}

export function displayObjectArrayData(
outputFormat: 'table' | 'json' | 'yaml',
outputFormat: OutputFormat,
data: object[],
): string {
return outputFormat === 'json'
return outputFormat === OutputFormat.Json
? JSON.stringify(data, null, 2)
: outputFormat === 'yaml'
: outputFormat === OutputFormat.Yaml
? yaml.stringify(data).trim()
: data.length === 0
? 'No items found'
Expand All @@ -127,11 +130,10 @@ export function displayObjectArrayData(

export function printObjectOrArray(
print: GluegunPrint,
outputFormat: 'table' | 'json' | 'yaml',
outputFormat: OutputFormat,
data: object | object[],
keys: string[],
): void {
outputColors(print, outputFormat)
if (Array.isArray(data)) {
const formatted = data.map(item => pickFields(item, keys))
print.info(displayObjectArrayData(outputFormat, formatted))
Expand Down Expand Up @@ -172,14 +174,23 @@ export function validatePOI(poi: string | undefined): string | undefined {
return poi
}

export function outputColors(
export function parseOutputFormat(
print: GluegunPrint,
outputFormat: 'table' | 'json' | 'yaml',
): void {
if (outputFormat === 'table') {
print.colors.enable()
} else {
print.colors.disable()
outputFormat: string,
): OutputFormat | undefined {
switch (outputFormat) {
case OutputFormat.Table:
print.colors.enable()
return OutputFormat.Table
case OutputFormat.Json:
print.colors.disable()
return OutputFormat.Json
case OutputFormat.Yaml:
print.colors.disable()
return OutputFormat.Yaml
default:
print.error(`Invalid output format "${outputFormat}"`)
return
}
}

Expand Down
18 changes: 10 additions & 8 deletions packages/indexer-cli/src/commands/indexer/actions/approve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ import chalk from 'chalk'

import { loadValidatedConfig } from '../../../config'
import { createIndexerManagementClient } from '../../../client'
import { fixParameters, printObjectOrArray } from '../../../command-helpers'
import {
fixParameters,
printObjectOrArray,
parseOutputFormat,
} from '../../../command-helpers'
import { approveActions, fetchActions } from '../../../actions'
import { ActionStatus } from '@graphprotocol/indexer-common'

Expand All @@ -29,25 +33,23 @@ module.exports = {
const { h, help, o, output } = parameters.options
const [...actionIDs] = fixParameters(parameters, { h, help }) || []

const outputFormat = o || output || 'table'
const outputFormat = parseOutputFormat(print, o || output || 'table')
const toHelp = help || h || undefined

if (toHelp) {
inputSpinner.stopAndPersist({ symbol: '💁', text: HELP })
return
}
if (!outputFormat) {
process.exitCode = 1
return
}

let numericActionIDs: number[]

const config = loadValidatedConfig()
const client = await createIndexerManagementClient({ url: config.api })
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'`)
}
Expand Down
7 changes: 3 additions & 4 deletions packages/indexer-cli/src/commands/indexer/disputes/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import chalk from 'chalk'
import { loadValidatedConfig } from '../../../config'
import { createIndexerManagementClient } from '../../../client'
import { disputes, printDisputes } from '../../../disputes'
import { parseOutputFormat } from '../../../command-helpers'

const HELP = `
${chalk.bold(
Expand All @@ -25,15 +26,13 @@ module.exports = {

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

if (help || h) {
print.info(HELP)
return
}

if (!['json', 'yaml', 'table'].includes(outputFormat)) {
print.error(`Invalid output format "${outputFormat}"`)
if (!outputFormat) {
process.exitCode = 1
return
}
Expand Down
12 changes: 5 additions & 7 deletions packages/indexer-cli/src/commands/indexer/rules/clear.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { partition } from '@thi.ng/iterators'

import { loadValidatedConfig } from '../../../config'
import { createIndexerManagementClient } from '../../../client'
import { fixParameters } from '../../../command-helpers'
import { setIndexingRule, printIndexingRules, parseIndexingRule } from '../../../rules'
import { fixParameters, parseOutputFormat } from '../../../command-helpers'
import { setIndexingRule, displayRules, parseIndexingRule } from '../../../rules'
import { processIdentifier } from '@graphprotocol/indexer-common'

const HELP = `
Expand Down Expand Up @@ -33,15 +33,13 @@ module.exports = {

const { h, help, o, output } = parameters.options
const [id, ...keys] = fixParameters(parameters, { h, help }) || []
const outputFormat = o || output || 'table'
const outputFormat = parseOutputFormat(print, o || output || 'table')

if (help || h) {
print.info(HELP)
return
}

if (!['json', 'yaml', 'table'].includes(outputFormat)) {
print.error(`Invalid output format "${outputFormat}"`)
if (!outputFormat) {
process.exitCode = 1
return
}
Expand Down Expand Up @@ -92,7 +90,7 @@ module.exports = {

const client = await createIndexerManagementClient({ url: config.api })
const rule = await setIndexingRule(client, inputRule)
printIndexingRules(print, outputFormat, identifier, rule, [])
print.info(displayRules(outputFormat, identifier, rule, []))
} catch (error) {
print.error(error.toString())
process.exitCode = 1
Expand Down
8 changes: 3 additions & 5 deletions packages/indexer-cli/src/commands/indexer/rules/delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import chalk from 'chalk'

import { loadValidatedConfig } from '../../../config'
import { createIndexerManagementClient } from '../../../client'
import { fixParameters } from '../../../command-helpers'
import { fixParameters, parseOutputFormat } from '../../../command-helpers'
import { indexingRules, deleteIndexingRules } from '../../../rules'
import { processIdentifier } from '@graphprotocol/indexer-common'

Expand All @@ -26,15 +26,13 @@ module.exports = {

const { h, help, o, output } = parameters.options
const [id] = fixParameters(parameters, { h, help }) || []
const outputFormat = o || output || 'table'
const outputFormat = parseOutputFormat(print, o || output || 'table')

if (help || h) {
print.info(HELP)
return
}

if (!['json', 'yaml', 'table'].includes(outputFormat)) {
print.error(`Invalid output format "${outputFormat}"`)
if (!outputFormat) {
process.exitCode = 1
return
}
Expand Down
Loading

0 comments on commit 2e1e0fc

Please sign in to comment.