Skip to content

Commit

Permalink
CLI commands: use built-in JSON functionality instead of re-implement…
Browse files Browse the repository at this point in the history
…ing it as needed
  • Loading branch information
mat-if committed Jul 25, 2024
1 parent 105a03e commit 89f8e0f
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 46 deletions.
3 changes: 1 addition & 2 deletions ironfish-cli/src/commands/chain/blocks/info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import * as ui from '../../../ui'

export default class BlockInfo extends IronfishCommand {
static description = 'Show the block header of a requested hash or sequence'
static enableJsonFlag = true

static args = {
search: Args.string({
Expand All @@ -16,8 +17,6 @@ export default class BlockInfo extends IronfishCommand {
}),
}

static enableJsonFlag: boolean = true

async start(): Promise<unknown> {
const { args } = await this.parse(BlockInfo)
const { search } = args
Expand Down
33 changes: 8 additions & 25 deletions ironfish-cli/src/commands/config/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
import { ConfigOptions } from '@ironfish/sdk'
import { Args, Flags } from '@oclif/core'
import jsonColorizer from 'json-colorizer'
import { IronfishCommand } from '../../command'
import { RemoteFlags } from '../../flags'
import { ColorFlag, ColorFlagKey, RemoteFlags } from '../../flags'
import * as ui from '../../ui'

export class GetCommand extends IronfishCommand {
static description = `Print out one config value`
static enableJsonFlag = true

static args = {
name: Args.string({
Expand All @@ -19,26 +20,17 @@ export class GetCommand extends IronfishCommand {

static flags = {
...RemoteFlags,
[ColorFlagKey]: ColorFlag,
user: Flags.boolean({
description: 'Only show config from the users datadir and not overrides',
}),
local: Flags.boolean({
default: false,
description: 'Dont connect to the node when displaying the config',
}),
color: Flags.boolean({
default: true,
allowNo: true,
description: 'Should colorize the output',
}),
json: Flags.boolean({
default: false,
allowNo: true,
description: 'Output the config value as json',
}),
}

async start(): Promise<void> {
async start(): Promise<unknown> {
const { args, flags } = await this.parse(GetCommand)
const { name } = args

Expand All @@ -54,19 +46,10 @@ export class GetCommand extends IronfishCommand {
this.exit(0)
}

let output = ''
const config = { [key]: response.content[key] }

if (flags.json) {
output = JSON.stringify(response.content[key], undefined, ' ')

if (flags.color) {
output = jsonColorizer(output)
}
} else {
output = String(response.content[key])
}
this.log(ui.card(config))

this.log(output)
this.exit(0)
return config
}
}
14 changes: 7 additions & 7 deletions ironfish-cli/src/commands/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
import { Flags } from '@oclif/core'
import jsonColorizer from 'json-colorizer'
import { IronfishCommand } from '../../command'
import { ColorFlag, ColorFlagKey } from '../../flags'
import { RemoteFlags } from '../../flags'
import * as ui from '../../ui'

export class ShowCommand extends IronfishCommand {
static description = `Print out the entire config`
static enableJsonFlag = true

static flags = {
...RemoteFlags,
Expand All @@ -22,16 +23,15 @@ export class ShowCommand extends IronfishCommand {
}),
}

async start(): Promise<void> {
async start(): Promise<unknown> {
const { flags } = await this.parse(ShowCommand)

const client = await this.connectRpc(flags.local)
const response = await client.config.getConfig({ user: flags.user })
const config = response.content

let output = JSON.stringify(response.content, undefined, ' ')
if (flags.color) {
output = jsonColorizer(output)
}
this.log(output)
this.log(ui.card(config))

return config
}
}
19 changes: 8 additions & 11 deletions ironfish-cli/src/commands/wallet/export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
import { AccountFormat, ErrorUtils, LanguageUtils } from '@ironfish/sdk'
import { Args, Flags } from '@oclif/core'
import fs from 'fs'
import jsonColorizer from 'json-colorizer'
import path from 'path'
import { IronfishCommand } from '../../command'
import { ColorFlag, ColorFlagKey, EnumLanguageKeyFlag, RemoteFlags } from '../../flags'
import { confirmOrQuit } from '../../ui'

export class ExportCommand extends IronfishCommand {
static description = `Export an account`
static enableJsonFlag = true

static flags = {
...RemoteFlags,
Expand All @@ -29,10 +29,6 @@ export class ExportCommand extends IronfishCommand {
required: false,
choices: LanguageUtils.LANGUAGE_KEYS,
}),
json: Flags.boolean({
default: false,
description: 'Output the account as JSON, rather than the default bech32',
}),
path: Flags.string({
description: 'The path to export the account to',
required: false,
Expand All @@ -50,9 +46,9 @@ export class ExportCommand extends IronfishCommand {
}),
}

async start(): Promise<void> {
async start(): Promise<unknown> {
const { flags, args } = await this.parse(ExportCommand)
const { color, local, path: exportPath, viewonly: viewOnly } = flags
const { local, path: exportPath, viewonly: viewOnly } = flags
const { account } = args

if (flags.language) {
Expand All @@ -73,10 +69,7 @@ export class ExportCommand extends IronfishCommand {
language: flags.language,
})

let output = response.content.account
if (color && flags.json && !exportPath) {
output = jsonColorizer(output)
}
const output = response.content.account

if (exportPath) {
let resolved = this.sdk.fileSystem.resolve(exportPath)
Expand Down Expand Up @@ -110,5 +103,9 @@ export class ExportCommand extends IronfishCommand {
}

this.log(output)

if (flags.json) {
return output
}
}
}
9 changes: 8 additions & 1 deletion ironfish-cli/src/ui/json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,12 @@
import jsonColorizer from 'json-colorizer'

export function json(data: unknown): string {
return jsonColorizer(JSON.stringify(data, undefined, ' '))
let output = data

// Only try to stringify JSON output if it is not already a string
if (typeof data !== 'string') {
output = JSON.stringify(data, undefined, ' ')
}

return jsonColorizer(output)
}

0 comments on commit 89f8e0f

Please sign in to comment.