From 0c0eb79fa31da698f59debee78bb309bc43cedd8 Mon Sep 17 00:00:00 2001 From: 0xmuralik Date: Mon, 27 Mar 2023 18:14:42 +0530 Subject: [PATCH] eliminate dry code --- src/cmds/cns-cmds/account-cmds/get.ts | 10 +++------- .../cns-cmds/auction-cmds/bid-cmds/commit.ts | 9 ++------- .../cns-cmds/auction-cmds/bid-cmds/reveal.ts | 8 ++------ src/cmds/cns-cmds/auction-cmds/get.ts | 8 ++------ .../cns-cmds/authority-cmds/bond-cmds/set.ts | 9 +++------ src/cmds/cns-cmds/authority-cmds/reserve.ts | 8 ++------ src/cmds/cns-cmds/authority-cmds/whois.ts | 8 ++------ src/cmds/cns-cmds/bond-cmds/associate.ts | 9 +++------ src/cmds/cns-cmds/bond-cmds/cancel.ts | 9 +++------ src/cmds/cns-cmds/bond-cmds/create.ts | 11 ++++------- src/cmds/cns-cmds/bond-cmds/dissociate.ts | 9 +++------ src/cmds/cns-cmds/bond-cmds/get.ts | 10 +++------- src/cmds/cns-cmds/bond-cmds/list.ts | 10 +++------- .../bond-cmds/records-cmds/dissociate.ts | 9 +++------ .../bond-cmds/records-cmds/reassociate.ts | 9 +++------ src/cmds/cns-cmds/bond-cmds/refill.ts | 9 +++------ src/cmds/cns-cmds/bond-cmds/withdraw.ts | 9 +++------ src/cmds/cns-cmds/name-cmds/delete.ts | 9 +++------ src/cmds/cns-cmds/name-cmds/lookup.ts | 8 ++------ src/cmds/cns-cmds/name-cmds/resolve.ts | 9 +++------ src/cmds/cns-cmds/name-cmds/set.ts | 9 +++------ src/cmds/cns-cmds/record-cmds/get.ts | 8 ++------ src/cmds/cns-cmds/record-cmds/list.ts | 8 ++------ src/cmds/cns-cmds/record-cmds/publish.ts | 8 ++------ src/util/index.ts | 1 + src/util/output.ts | 16 ++++++++++++++++ 26 files changed, 81 insertions(+), 149 deletions(-) create mode 100644 src/util/output.ts diff --git a/src/cmds/cns-cmds/account-cmds/get.ts b/src/cmds/cns-cmds/account-cmds/get.ts index 2fad6fa..4562ef5 100644 --- a/src/cmds/cns-cmds/account-cmds/get.ts +++ b/src/cmds/cns-cmds/account-cmds/get.ts @@ -2,7 +2,7 @@ import { Arguments } from 'yargs'; import assert from 'assert'; import { Account, Registry } from '@cerc-io/laconic-sdk'; -import { getConfig, getConnectionInfo } from '../../../util'; +import { getConfig, getConnectionInfo, queryOutput } from '../../../util'; export const command = 'get'; @@ -23,10 +23,6 @@ export const handler = async (argv: Arguments) => { const registry = new Registry(gqlEndpoint, restEndpoint, chainId); const result = await registry.getAccounts([address]); - - if (argv.output=="json"){ - console.log(JSON.stringify(result, undefined, 2)); - } else { - console.log(result) - } + + queryOutput(result,argv.output); } diff --git a/src/cmds/cns-cmds/auction-cmds/bid-cmds/commit.ts b/src/cmds/cns-cmds/auction-cmds/bid-cmds/commit.ts index 89d6e29..54e2a67 100644 --- a/src/cmds/cns-cmds/auction-cmds/bid-cmds/commit.ts +++ b/src/cmds/cns-cmds/auction-cmds/bid-cmds/commit.ts @@ -5,7 +5,7 @@ import { Account, createBid, Registry } from '@cerc-io/laconic-sdk'; import { ensureDir } from 'fs-extra'; import fs from 'fs'; -import { getConfig, getConnectionInfo, getGasAndFees } from '../../../../util'; +import { getConfig, getConnectionInfo, getGasAndFees, txOutput } from '../../../../util'; const OUT_DIR = 'out'; @@ -44,11 +44,6 @@ export const handler = async (argv: Arguments) => { const result = await registry.commitBid({ auctionId, commitHash }, privateKey, fee); const revealFile = `{"reveal_file":"${revealFilePath}"}` - console.log(argv.verbose ? JSON.stringify(result, undefined, 2)+ JSON.stringify(JSON.parse(revealFile)) : JSON.stringify(JSON.parse(revealFile))); - if (argv.output=="json"){ - console.log(argv.verbose ? JSON.stringify(result, undefined, 2)+"\n"+JSON.stringify(JSON.parse(revealFile)) : JSON.stringify(JSON.parse(revealFile))); - } else { - console.log(argv.verbose ? result+"\n"+revealFile:revealFile) - } + txOutput(result,revealFile,argv.output,argv.verbose) } diff --git a/src/cmds/cns-cmds/auction-cmds/bid-cmds/reveal.ts b/src/cmds/cns-cmds/auction-cmds/bid-cmds/reveal.ts index 3a41ca0..5200863 100644 --- a/src/cmds/cns-cmds/auction-cmds/bid-cmds/reveal.ts +++ b/src/cmds/cns-cmds/auction-cmds/bid-cmds/reveal.ts @@ -4,7 +4,7 @@ import path from 'path'; import { Registry } from '@cerc-io/laconic-sdk'; import fs from 'fs'; -import { getConfig, getConnectionInfo, getGasAndFees } from '../../../../util'; +import { getConfig, getConnectionInfo, getGasAndFees, txOutput } from '../../../../util'; export const command = 'reveal [auction-id] [file-path]'; @@ -30,9 +30,5 @@ export const handler = async (argv: Arguments) => { const result = await registry.revealBid({ auctionId, reveal: reveal.toString('hex') }, privateKey, fee); const success = `{"success":${result.code==0}}` - if (argv.output=="json"){ - console.log(argv.verbose ? JSON.stringify(result, undefined, 2) : JSON.stringify(JSON.parse(success))); - } else { - console.log(argv.verbose ? result : success); - } + txOutput(result,success,argv.output,argv.verbose) } diff --git a/src/cmds/cns-cmds/auction-cmds/get.ts b/src/cmds/cns-cmds/auction-cmds/get.ts index eda8aea..c3bab65 100644 --- a/src/cmds/cns-cmds/auction-cmds/get.ts +++ b/src/cmds/cns-cmds/auction-cmds/get.ts @@ -2,7 +2,7 @@ import { Arguments } from 'yargs'; import assert from 'assert'; import { Registry } from '@cerc-io/laconic-sdk'; -import { getConfig, getConnectionInfo } from '../../../util'; +import { getConfig, getConnectionInfo, queryOutput } from '../../../util'; export const command = 'get [id]'; @@ -21,9 +21,5 @@ export const handler = async (argv: Arguments) => { const registry = new Registry(gqlEndpoint, restEndpoint, chainId); const result = await registry.getAuctionsByIds([id as string]); - if (argv.output=="json"){ - console.log(JSON.stringify(result, undefined, 2)); - } else { - console.log(result) - } + queryOutput(result,argv.output) } diff --git a/src/cmds/cns-cmds/authority-cmds/bond-cmds/set.ts b/src/cmds/cns-cmds/authority-cmds/bond-cmds/set.ts index 65d9289..7839ae5 100644 --- a/src/cmds/cns-cmds/authority-cmds/bond-cmds/set.ts +++ b/src/cmds/cns-cmds/authority-cmds/bond-cmds/set.ts @@ -2,7 +2,7 @@ import { Arguments } from 'yargs'; import assert from 'assert'; import { Registry } from '@cerc-io/laconic-sdk'; -import { getConfig, getConnectionInfo, getGasAndFees } from '../../../../util'; +import { getConfig, getConnectionInfo, getGasAndFees, txOutput } from '../../../../util'; export const command = 'set [name] [bond-id]'; @@ -25,9 +25,6 @@ export const handler = async (argv: Arguments) => { const fee = getGasAndFees(argv, cnsConfig); const result = await registry.setAuthorityBond({ name, bondId }, privateKey, fee); const success = `{"success":${result.code==0}}` - if (argv.output=="json"){ - console.log(argv.verbose ? JSON.stringify(result, undefined, 2) : JSON.stringify(JSON.parse(success))); - } else { - console.log(argv.verbose ? result : success); - } + + txOutput(result,success,argv.output,argv.verbose) } diff --git a/src/cmds/cns-cmds/authority-cmds/reserve.ts b/src/cmds/cns-cmds/authority-cmds/reserve.ts index 9791b06..437e8cc 100644 --- a/src/cmds/cns-cmds/authority-cmds/reserve.ts +++ b/src/cmds/cns-cmds/authority-cmds/reserve.ts @@ -2,7 +2,7 @@ import { Arguments } from 'yargs'; import assert from 'assert'; import { Registry } from '@cerc-io/laconic-sdk'; -import { getConfig, getConnectionInfo, getGasAndFees } from '../../../util'; +import { getConfig, getConnectionInfo, getGasAndFees, txOutput } from '../../../util'; export const command = 'reserve [name]'; @@ -32,9 +32,5 @@ export const handler = async (argv: Arguments) => { const result = await registry.reserveAuthority({ name, owner }, privateKey, fee); const success = `{"success":${result.code==0}}` - if (argv.output=="json"){ - console.log(argv.verbose ? JSON.stringify(result, undefined, 2) : JSON.stringify(JSON.parse(success))); - } else { - console.log(argv.verbose ? result : success); - } + txOutput(result,success,argv.output,argv.verbose) } diff --git a/src/cmds/cns-cmds/authority-cmds/whois.ts b/src/cmds/cns-cmds/authority-cmds/whois.ts index 3faea15..6fbf9f7 100644 --- a/src/cmds/cns-cmds/authority-cmds/whois.ts +++ b/src/cmds/cns-cmds/authority-cmds/whois.ts @@ -2,7 +2,7 @@ import { Arguments } from 'yargs'; import assert from 'assert'; import { Registry } from '@cerc-io/laconic-sdk'; -import { getConfig, getConnectionInfo } from '../../../util'; +import { getConfig, getConnectionInfo, queryOutput } from '../../../util'; export const command = 'whois [name]'; @@ -21,9 +21,5 @@ export const handler = async (argv: Arguments) => { const registry = new Registry(gqlEndpoint, restEndpoint, chainId); const result = await registry.lookupAuthorities([name], true); - if (argv.output=="json"){ - console.log(JSON.stringify(result, undefined, 2)); - } else { - console.log(result) - } + queryOutput(result,argv.output) } diff --git a/src/cmds/cns-cmds/bond-cmds/associate.ts b/src/cmds/cns-cmds/bond-cmds/associate.ts index 978f98d..de6ebf0 100644 --- a/src/cmds/cns-cmds/bond-cmds/associate.ts +++ b/src/cmds/cns-cmds/bond-cmds/associate.ts @@ -2,7 +2,7 @@ import { Arguments } from 'yargs'; import assert from 'assert'; import { Registry } from '@cerc-io/laconic-sdk'; -import { getConfig, getConnectionInfo, getGasAndFees } from '../../../util'; +import { getConfig, getConnectionInfo, getGasAndFees ,txOutput} from '../../../util'; export const command = 'associate'; @@ -31,9 +31,6 @@ export const handler = async (argv: Arguments) => { const fee = getGasAndFees(argv, cnsConfig); const result = await registry.associateBond({ recordId: id, bondId }, privateKey, fee); const success = `{"success":${result.code==0}}` - if (argv.output=="json"){ - console.log(argv.verbose ? JSON.stringify(result, undefined, 2) : JSON.stringify(JSON.parse(success))); - } else { - console.log(argv.verbose ? result : success); - } + txOutput(result,success,argv.output,argv.verbose) + } diff --git a/src/cmds/cns-cmds/bond-cmds/cancel.ts b/src/cmds/cns-cmds/bond-cmds/cancel.ts index 41aea64..de860e8 100644 --- a/src/cmds/cns-cmds/bond-cmds/cancel.ts +++ b/src/cmds/cns-cmds/bond-cmds/cancel.ts @@ -2,7 +2,7 @@ import { Arguments } from 'yargs'; import assert from 'assert'; import { Registry } from '@cerc-io/laconic-sdk'; -import { getConfig, getConnectionInfo, getGasAndFees } from '../../../util'; +import { getConfig, getConnectionInfo, getGasAndFees,txOutput } from '../../../util'; export const command = 'cancel'; @@ -23,9 +23,6 @@ export const handler = async (argv: Arguments) => { const fee = getGasAndFees(argv, cnsConfig); const result = await registry.cancelBond({ id }, privateKey, fee); const success = `{"success":${result.code==0}}` - if (argv.output=="json"){ - console.log(argv.verbose ? JSON.stringify(result, undefined, 2) : JSON.stringify(JSON.parse(success))); - } else { - console.log(argv.verbose ? result : success); - } + txOutput(result,success,argv.output,argv.verbose) + } diff --git a/src/cmds/cns-cmds/bond-cmds/create.ts b/src/cmds/cns-cmds/bond-cmds/create.ts index a19d274..8111fb2 100644 --- a/src/cmds/cns-cmds/bond-cmds/create.ts +++ b/src/cmds/cns-cmds/bond-cmds/create.ts @@ -2,7 +2,7 @@ import { Arguments } from 'yargs'; import assert from 'assert'; import { Registry } from '@cerc-io/laconic-sdk'; -import { getConfig, getConnectionInfo, getGasAndFees } from '../../../util'; +import { getConfig, getConnectionInfo, getGasAndFees ,txOutput} from '../../../util'; export const command = 'create'; @@ -37,10 +37,7 @@ export const handler = async (argv: Arguments) => { const bondId = await registry.getNextBondId(privateKey); const result = await registry.createBond({ denom, amount }, privateKey, fee); const jsonString=`{"bondId":"${bondId}"}` - - if (argv.output=="json"){ - console.log(argv.verbose ? JSON.stringify(result, undefined, 2) : JSON.stringify(JSON.parse(jsonString))); - } else { - console.log(argv.verbose ? result : jsonString); - } + + txOutput(result,jsonString,argv.output,argv.verbose) + } diff --git a/src/cmds/cns-cmds/bond-cmds/dissociate.ts b/src/cmds/cns-cmds/bond-cmds/dissociate.ts index c4ed5da..96fda73 100644 --- a/src/cmds/cns-cmds/bond-cmds/dissociate.ts +++ b/src/cmds/cns-cmds/bond-cmds/dissociate.ts @@ -2,7 +2,7 @@ import { Arguments } from 'yargs'; import assert from 'assert'; import { Registry } from '@cerc-io/laconic-sdk'; -import { getConfig, getConnectionInfo, getGasAndFees } from '../../../util'; +import { getConfig, getConnectionInfo, getGasAndFees ,txOutput} from '../../../util'; export const command = 'dissociate'; @@ -23,9 +23,6 @@ export const handler = async (argv: Arguments) => { const fee = getGasAndFees(argv, cnsConfig); const result = await registry.dissociateBond({ recordId: id }, privateKey, fee); const success = `{"success":${result.code==0}}` - if (argv.output=="json"){ - console.log(argv.verbose ? JSON.stringify(result, undefined, 2) : JSON.stringify(JSON.parse(success))); - } else { - console.log(argv.verbose ? result : success); - } + txOutput(result,success,argv.output,argv.verbose) + } diff --git a/src/cmds/cns-cmds/bond-cmds/get.ts b/src/cmds/cns-cmds/bond-cmds/get.ts index 15d0a93..1c61a37 100644 --- a/src/cmds/cns-cmds/bond-cmds/get.ts +++ b/src/cmds/cns-cmds/bond-cmds/get.ts @@ -2,7 +2,7 @@ import { Arguments } from 'yargs'; import assert from 'assert'; import { Registry } from '@cerc-io/laconic-sdk'; -import { getConfig, getConnectionInfo } from '../../../util'; +import { getConfig, getConnectionInfo ,queryOutput} from '../../../util'; export const command = 'get'; @@ -21,10 +21,6 @@ export const handler = async (argv: Arguments) => { const registry = new Registry(gqlEndpoint, restEndpoint, chainId); const result = await registry.getBondsByIds([id as string]); - - if (argv.output=="json"){ - console.log(JSON.stringify(result, undefined, 2)); - } else { - console.log(result) - } + + queryOutput(result,argv.output) } diff --git a/src/cmds/cns-cmds/bond-cmds/list.ts b/src/cmds/cns-cmds/bond-cmds/list.ts index c9a8504..94e350d 100644 --- a/src/cmds/cns-cmds/bond-cmds/list.ts +++ b/src/cmds/cns-cmds/bond-cmds/list.ts @@ -2,7 +2,7 @@ import { Arguments } from 'yargs'; import assert from 'assert'; import { Registry } from '@cerc-io/laconic-sdk'; -import { getConfig, getConnectionInfo } from '../../../util'; +import { getConfig, getConnectionInfo ,queryOutput} from '../../../util'; export const command = 'list'; @@ -25,10 +25,6 @@ export const handler = async (argv: Arguments) => { const { owner } = argv; const result = await registry.queryBonds({ owner }); - - if (argv.output=="json"){ - console.log(JSON.stringify(result, undefined, 2)); - } else { - console.log(result) - } + + queryOutput(result,argv.output) } diff --git a/src/cmds/cns-cmds/bond-cmds/records-cmds/dissociate.ts b/src/cmds/cns-cmds/bond-cmds/records-cmds/dissociate.ts index 4523521..44edbcb 100644 --- a/src/cmds/cns-cmds/bond-cmds/records-cmds/dissociate.ts +++ b/src/cmds/cns-cmds/bond-cmds/records-cmds/dissociate.ts @@ -2,7 +2,7 @@ import { Arguments } from 'yargs'; import assert from 'assert'; import { Registry } from '@cerc-io/laconic-sdk'; -import { getConfig, getConnectionInfo, getGasAndFees } from '../../../../util'; +import { getConfig, getConnectionInfo, getGasAndFees,txOutput } from '../../../../util'; export const command = 'dissociate'; @@ -29,9 +29,6 @@ export const handler = async (argv: Arguments) => { const fee = getGasAndFees(argv, cnsConfig); const result = await registry.dissociateRecords({ bondId }, privateKey, fee); const success = `{"success":${result.code==0}}` - if (argv.output=="json"){ - console.log(argv.verbose ? JSON.stringify(result, undefined, 2) : JSON.stringify(JSON.parse(success))); - } else { - console.log(argv.verbose ? result : success); - } + txOutput(result,success,argv.output,argv.verbose) + } diff --git a/src/cmds/cns-cmds/bond-cmds/records-cmds/reassociate.ts b/src/cmds/cns-cmds/bond-cmds/records-cmds/reassociate.ts index 5c4cf2c..8893124 100644 --- a/src/cmds/cns-cmds/bond-cmds/records-cmds/reassociate.ts +++ b/src/cmds/cns-cmds/bond-cmds/records-cmds/reassociate.ts @@ -2,7 +2,7 @@ import { Arguments } from 'yargs'; import assert from 'assert'; import { Registry } from '@cerc-io/laconic-sdk'; -import { getConfig, getConnectionInfo, getGasAndFees } from '../../../../util'; +import { getConfig, getConnectionInfo, getGasAndFees ,txOutput} from '../../../../util'; export const command = 'reassociate'; @@ -34,9 +34,6 @@ export const handler = async (argv: Arguments) => { const fee = getGasAndFees(argv, cnsConfig); const result = await registry.reassociateRecords({ oldBondId, newBondId }, privateKey, fee); const success = `{"success":${result.code==0}}` - if (argv.output=="json"){ - console.log(argv.verbose ? JSON.stringify(result, undefined, 2) : JSON.stringify(JSON.parse(success))); - } else { - console.log(argv.verbose ? result : success); - } + txOutput(result,success,argv.output,argv.verbose) + } diff --git a/src/cmds/cns-cmds/bond-cmds/refill.ts b/src/cmds/cns-cmds/bond-cmds/refill.ts index 7e610d7..a439000 100644 --- a/src/cmds/cns-cmds/bond-cmds/refill.ts +++ b/src/cmds/cns-cmds/bond-cmds/refill.ts @@ -2,7 +2,7 @@ import { Arguments } from 'yargs'; import assert from 'assert'; import { Registry } from '@cerc-io/laconic-sdk'; -import { getConfig, getConnectionInfo, getGasAndFees } from '../../../util'; +import { getConfig, getConnectionInfo, getGasAndFees ,txOutput} from '../../../util'; import { isNil } from 'lodash'; export const command = 'refill'; @@ -38,9 +38,6 @@ export const handler = async (argv: Arguments) => { const fee = getGasAndFees(argv, cnsConfig); const result = await registry.refillBond({ id, denom, amount }, privateKey, fee); const success = `{"success":${result.code==0}}` - if (argv.output=="json"){ - console.log(argv.verbose ? JSON.stringify(result, undefined, 2) : JSON.stringify(JSON.parse(success))); - } else { - console.log(argv.verbose ? result : success); - } + txOutput(result,success,argv.output,argv.verbose) + } diff --git a/src/cmds/cns-cmds/bond-cmds/withdraw.ts b/src/cmds/cns-cmds/bond-cmds/withdraw.ts index 0fe6d61..041563d 100644 --- a/src/cmds/cns-cmds/bond-cmds/withdraw.ts +++ b/src/cmds/cns-cmds/bond-cmds/withdraw.ts @@ -2,7 +2,7 @@ import { Arguments } from 'yargs'; import assert from 'assert'; import { Registry } from '@cerc-io/laconic-sdk'; -import { getConfig, getConnectionInfo, getGasAndFees } from '../../../util'; +import { getConfig, getConnectionInfo, getGasAndFees,txOutput } from '../../../util'; export const command = 'withdraw'; @@ -37,9 +37,6 @@ export const handler = async (argv: Arguments) => { const fee = getGasAndFees(argv, cnsConfig); const result = await registry.withdrawBond({ id, denom, amount }, privateKey, fee); const success = `{"success":${result.code==0}}` - if (argv.output=="json"){ - console.log(argv.verbose ? JSON.stringify(result, undefined, 2) : JSON.stringify(JSON.parse(success))); - } else { - console.log(argv.verbose ? result : success); - } + txOutput(result,success,argv.output,argv.verbose) + } diff --git a/src/cmds/cns-cmds/name-cmds/delete.ts b/src/cmds/cns-cmds/name-cmds/delete.ts index c1e891a..f008786 100644 --- a/src/cmds/cns-cmds/name-cmds/delete.ts +++ b/src/cmds/cns-cmds/name-cmds/delete.ts @@ -2,7 +2,7 @@ import { Arguments } from 'yargs'; import assert from 'assert'; import { Registry } from '@cerc-io/laconic-sdk'; -import { getConfig, getConnectionInfo, getGasAndFees } from '../../../util'; +import { getConfig, getConnectionInfo, getGasAndFees ,txOutput} from '../../../util'; export const command = 'delete [name]'; @@ -24,9 +24,6 @@ export const handler = async (argv: Arguments) => { const result = await registry.deleteName({ crn: name }, privateKey, fee); const success = `{"success":${result.code==0}}` - if (argv.output=="json"){ - console.log(argv.verbose ? JSON.stringify(result, undefined, 2) : JSON.stringify(JSON.parse(success))); - } else { - console.log(argv.verbose ? result : success); - } + txOutput(result,success,argv.output,argv.verbose) + } diff --git a/src/cmds/cns-cmds/name-cmds/lookup.ts b/src/cmds/cns-cmds/name-cmds/lookup.ts index 56cffda..df5e43c 100644 --- a/src/cmds/cns-cmds/name-cmds/lookup.ts +++ b/src/cmds/cns-cmds/name-cmds/lookup.ts @@ -2,7 +2,7 @@ import { Arguments } from 'yargs'; import assert from 'assert'; import { Registry } from '@cerc-io/laconic-sdk'; -import { getConfig, getConnectionInfo } from '../../../util'; +import { getConfig, getConnectionInfo ,queryOutput} from '../../../util'; export const command = 'lookup [name]'; @@ -27,9 +27,5 @@ export const handler = async (argv: Arguments) => { const registry = new Registry(gqlEndpoint, restEndpoint, chainId); const result = await registry.lookupNames([name], argv.history as boolean); - if (argv.output=="json"){ - console.log(JSON.stringify(result, undefined, 2)); - } else { - console.log(result) - } + queryOutput(result,argv.output) } diff --git a/src/cmds/cns-cmds/name-cmds/resolve.ts b/src/cmds/cns-cmds/name-cmds/resolve.ts index 15d3504..d125f9f 100644 --- a/src/cmds/cns-cmds/name-cmds/resolve.ts +++ b/src/cmds/cns-cmds/name-cmds/resolve.ts @@ -2,7 +2,7 @@ import { Arguments } from 'yargs'; import assert from 'assert'; import { Registry } from '@cerc-io/laconic-sdk'; -import { getConfig, getConnectionInfo } from '../../../util'; +import { getConfig, getConnectionInfo,txOutput } from '../../../util'; export const command = 'resolve [name]'; @@ -22,9 +22,6 @@ export const handler = async (argv: Arguments) => { const result = await registry.resolveNames([name]); const success = `{"success":${result.code==0}}` - if (argv.output=="json"){ - console.log(argv.verbose ? JSON.stringify(result, undefined, 2) : JSON.stringify(JSON.parse(success))); - } else { - console.log(argv.verbose ? result : success); - } + txOutput(result,success,argv.output,argv.verbose) + } diff --git a/src/cmds/cns-cmds/name-cmds/set.ts b/src/cmds/cns-cmds/name-cmds/set.ts index 2258720..bb1bfeb 100644 --- a/src/cmds/cns-cmds/name-cmds/set.ts +++ b/src/cmds/cns-cmds/name-cmds/set.ts @@ -2,7 +2,7 @@ import { Arguments } from 'yargs'; import assert from 'assert'; import { Registry } from '@cerc-io/laconic-sdk'; -import { getConfig, getConnectionInfo, getGasAndFees } from '../../../util'; +import { getConfig, getConnectionInfo, getGasAndFees,txOutput } from '../../../util'; export const command = 'set [name] [id]'; @@ -26,9 +26,6 @@ export const handler = async (argv: Arguments) => { const result = await registry.setName({ crn: name, cid: id }, privateKey, fee); const success = `{"success":${result.code==0}}` - if (argv.output=="json"){ - console.log(argv.verbose ? JSON.stringify(result, undefined, 2) : JSON.stringify(JSON.parse(success))); - } else { - console.log(argv.verbose ? result : success); - } + txOutput(result,success,argv.output,argv.verbose) + } diff --git a/src/cmds/cns-cmds/record-cmds/get.ts b/src/cmds/cns-cmds/record-cmds/get.ts index a24e9ef..fbdf61e 100644 --- a/src/cmds/cns-cmds/record-cmds/get.ts +++ b/src/cmds/cns-cmds/record-cmds/get.ts @@ -2,7 +2,7 @@ import { Arguments } from 'yargs'; import assert from 'assert'; import { Registry } from '@cerc-io/laconic-sdk'; -import { getConfig, getConnectionInfo } from '../../../util'; +import { getConfig, getConnectionInfo ,queryOutput} from '../../../util'; export const command = 'get'; @@ -21,9 +21,5 @@ export const handler = async (argv: Arguments) => { const registry = new Registry(gqlEndpoint, restEndpoint, chainId); const result = await registry.getRecordsByIds([id as string]); - if (argv.output=="json"){ - console.log(JSON.stringify(result, undefined, 2)); - } else { - console.log(result) - } + queryOutput(result,argv.output) } diff --git a/src/cmds/cns-cmds/record-cmds/list.ts b/src/cmds/cns-cmds/record-cmds/list.ts index ea4496c..49f5cee 100644 --- a/src/cmds/cns-cmds/record-cmds/list.ts +++ b/src/cmds/cns-cmds/record-cmds/list.ts @@ -2,7 +2,7 @@ import { Arguments } from 'yargs'; import assert from 'assert'; import { Registry } from '@cerc-io/laconic-sdk'; -import { getConfig, getConnectionInfo } from '../../../util'; +import { getConfig, getConnectionInfo ,queryOutput} from '../../../util'; export const command = 'list'; @@ -36,9 +36,5 @@ export const handler = async (argv: Arguments) => { const registry = new Registry(gqlEndpoint, restEndpoint, chainId); const result = await registry.queryRecords({ bondId, type, name }, all as boolean); - if (argv.output=="json"){ - console.log(JSON.stringify(result, undefined, 2)); - } else { - console.log(result) - } + queryOutput(result,argv.output) } diff --git a/src/cmds/cns-cmds/record-cmds/publish.ts b/src/cmds/cns-cmds/record-cmds/publish.ts index cef81ed..beb01f9 100644 --- a/src/cmds/cns-cmds/record-cmds/publish.ts +++ b/src/cmds/cns-cmds/record-cmds/publish.ts @@ -5,7 +5,7 @@ import yaml from 'js-yaml'; import fs from 'fs'; import { Registry } from '@cerc-io/laconic-sdk'; -import { getConfig, getGasAndFees, getConnectionInfo } from '../../../util'; +import { getConfig, getGasAndFees, getConnectionInfo, txOutput } from '../../../util'; export const command = 'publish'; @@ -40,9 +40,5 @@ export const handler = async (argv: Arguments) => { const fee = getGasAndFees(argv, cnsConfig); const result = await registry.setRecord({ privateKey: userKey, record, bondId }, txKey as string, fee); - if (argv.output=="json"){ - console.log(argv.verbose ? JSON.stringify(result, undefined, 2) : JSON.stringify(result.data,undefined,2)); - } else { - console.log(argv.verbose ? result : result.data); - } + txOutput(result,JSON.stringify(result.data,undefined,2),argv.output,argv.verbose) } diff --git a/src/util/index.ts b/src/util/index.ts index 1ffc85c..c87a1cc 100644 --- a/src/util/index.ts +++ b/src/util/index.ts @@ -1,3 +1,4 @@ export * from './fees'; export * from './config'; export * from './common'; +export * from './output'; diff --git a/src/util/output.ts b/src/util/output.ts new file mode 100644 index 0000000..15f449b --- /dev/null +++ b/src/util/output.ts @@ -0,0 +1,16 @@ + +export const txOutput = (result:any,msg:string,output:unknown,verbose:unknown) => { + if (output=="json"){ + console.log(verbose ? JSON.parse(JSON.stringify(result)) : JSON.parse(msg)); + } else { + console.log(verbose ? JSON.stringify(result,undefined,2) : msg); + } +}; + +export const queryOutput = (result: any, output: unknown) => { + if (output=="json"){ + console.log(JSON.parse(JSON.stringify(result))); + } else { + console.log(JSON.stringify(result,undefined,2)); + } +}