From d3020daf2eecd7049c215ca97568778928e7fa33 Mon Sep 17 00:00:00 2001 From: 0xmuralik Date: Mon, 27 Mar 2023 13:14:02 +0530 Subject: [PATCH 1/6] bond and records outputs --- src/cmds/cns-cmds/bond-cmds/associate.ts | 3 ++- src/cmds/cns-cmds/bond-cmds/cancel.ts | 3 ++- src/cmds/cns-cmds/bond-cmds/create.ts | 3 ++- src/cmds/cns-cmds/bond-cmds/dissociate.ts | 3 ++- src/cmds/cns-cmds/bond-cmds/records-cmds/dissociate.ts | 3 ++- src/cmds/cns-cmds/bond-cmds/records-cmds/reassociate.ts | 3 ++- src/cmds/cns-cmds/bond-cmds/refill.ts | 4 +++- src/cmds/cns-cmds/bond-cmds/withdraw.ts | 3 ++- src/cmds/cns-cmds/record-cmds/publish.ts | 2 +- watcher.yml | 7 +++++++ 10 files changed, 25 insertions(+), 9 deletions(-) create mode 100644 watcher.yml diff --git a/src/cmds/cns-cmds/bond-cmds/associate.ts b/src/cmds/cns-cmds/bond-cmds/associate.ts index 5f20a2f..755d10c 100644 --- a/src/cmds/cns-cmds/bond-cmds/associate.ts +++ b/src/cmds/cns-cmds/bond-cmds/associate.ts @@ -30,5 +30,6 @@ export const handler = async (argv: Arguments) => { const registry = new Registry(gqlEndpoint, restEndpoint, chainId); const fee = getGasAndFees(argv, cnsConfig); const result = await registry.associateBond({ recordId: id, bondId }, privateKey, fee); - console.log(JSON.stringify(result, undefined, 2)); + const success = `{"success":${result.code==0}}` + console.log(argv.verbose ? JSON.stringify(result, undefined, 2): JSON.stringify(JSON.parse(success))); } diff --git a/src/cmds/cns-cmds/bond-cmds/cancel.ts b/src/cmds/cns-cmds/bond-cmds/cancel.ts index dda59ef..ca3d3e7 100644 --- a/src/cmds/cns-cmds/bond-cmds/cancel.ts +++ b/src/cmds/cns-cmds/bond-cmds/cancel.ts @@ -22,5 +22,6 @@ export const handler = async (argv: Arguments) => { const registry = new Registry(gqlEndpoint, restEndpoint, chainId); const fee = getGasAndFees(argv, cnsConfig); const result = await registry.cancelBond({ id }, privateKey, fee); - console.log(JSON.stringify(result, undefined, 2)); + const success = `{"success":${result.code==0}}` + console.log(argv.verbose ? JSON.stringify(result, undefined, 2): JSON.stringify(JSON.parse(success))); } diff --git a/src/cmds/cns-cmds/bond-cmds/create.ts b/src/cmds/cns-cmds/bond-cmds/create.ts index 9e3a79d..cf4be9e 100644 --- a/src/cmds/cns-cmds/bond-cmds/create.ts +++ b/src/cmds/cns-cmds/bond-cmds/create.ts @@ -36,5 +36,6 @@ export const handler = async (argv: Arguments) => { const fee = getGasAndFees(argv, cnsConfig); const bondId = await registry.getNextBondId(privateKey); const result = await registry.createBond({ denom, amount }, privateKey, fee); - console.log(true ? JSON.stringify(result, undefined, 2)+"\nBondId:"+bondId : bondId); + const jsonString=`{"bondId":"${bondId}"}` + console.log(verbose ? JSON.stringify(result, undefined, 2) : JSON.stringify(JSON.parse(jsonString),null,2)); } diff --git a/src/cmds/cns-cmds/bond-cmds/dissociate.ts b/src/cmds/cns-cmds/bond-cmds/dissociate.ts index 76d9185..7a60a1d 100644 --- a/src/cmds/cns-cmds/bond-cmds/dissociate.ts +++ b/src/cmds/cns-cmds/bond-cmds/dissociate.ts @@ -22,5 +22,6 @@ export const handler = async (argv: Arguments) => { const registry = new Registry(gqlEndpoint, restEndpoint, chainId); const fee = getGasAndFees(argv, cnsConfig); const result = await registry.dissociateBond({ recordId: id }, privateKey, fee); - console.log(JSON.stringify(result, undefined, 2)); + const success = `{"success":${result.code==0}}` + console.log(argv.verbose ? JSON.stringify(result, undefined, 2): JSON.stringify(JSON.parse(success))); } 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 136e4d4..2d50c00 100644 --- a/src/cmds/cns-cmds/bond-cmds/records-cmds/dissociate.ts +++ b/src/cmds/cns-cmds/bond-cmds/records-cmds/dissociate.ts @@ -28,5 +28,6 @@ export const handler = async (argv: Arguments) => { const registry = new Registry(gqlEndpoint, restEndpoint, chainId); const fee = getGasAndFees(argv, cnsConfig); const result = await registry.dissociateRecords({ bondId }, privateKey, fee); - console.log(JSON.stringify(result, undefined, 2)); + const success = `{"success":${result.code==0}}` + console.log(argv.verbose ? JSON.stringify(result, undefined, 2): JSON.stringify(JSON.parse(success))); } 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 4e909e1..4696834 100644 --- a/src/cmds/cns-cmds/bond-cmds/records-cmds/reassociate.ts +++ b/src/cmds/cns-cmds/bond-cmds/records-cmds/reassociate.ts @@ -33,5 +33,6 @@ export const handler = async (argv: Arguments) => { const registry = new Registry(gqlEndpoint, restEndpoint, chainId); const fee = getGasAndFees(argv, cnsConfig); const result = await registry.reassociateRecords({ oldBondId, newBondId }, privateKey, fee); - console.log(JSON.stringify(result, undefined, 2)); + const success = `{"success":${result.code==0}}` + console.log(argv.verbose ? JSON.stringify(result, undefined, 2): JSON.stringify(JSON.parse(success))); } diff --git a/src/cmds/cns-cmds/bond-cmds/refill.ts b/src/cmds/cns-cmds/bond-cmds/refill.ts index 0a23269..1627d3e 100644 --- a/src/cmds/cns-cmds/bond-cmds/refill.ts +++ b/src/cmds/cns-cmds/bond-cmds/refill.ts @@ -3,6 +3,7 @@ import assert from 'assert'; import { Registry } from '@cerc-io/laconic-sdk'; import { getConfig, getConnectionInfo, getGasAndFees } from '../../../util'; +import { isNil } from 'lodash'; export const command = 'refill'; @@ -36,5 +37,6 @@ export const handler = async (argv: Arguments) => { const registry = new Registry(gqlEndpoint, restEndpoint, chainId); const fee = getGasAndFees(argv, cnsConfig); const result = await registry.refillBond({ id, denom, amount }, privateKey, fee); - console.log(JSON.stringify(result, undefined, 2)); + const success = `{"success":${result.code==0}}` + console.log(argv.verbose ? JSON.stringify(result, undefined, 2): JSON.stringify(JSON.parse(success))); } diff --git a/src/cmds/cns-cmds/bond-cmds/withdraw.ts b/src/cmds/cns-cmds/bond-cmds/withdraw.ts index 1060e23..79cd313 100644 --- a/src/cmds/cns-cmds/bond-cmds/withdraw.ts +++ b/src/cmds/cns-cmds/bond-cmds/withdraw.ts @@ -36,5 +36,6 @@ export const handler = async (argv: Arguments) => { const registry = new Registry(gqlEndpoint, restEndpoint, chainId); const fee = getGasAndFees(argv, cnsConfig); const result = await registry.withdrawBond({ id, denom, amount }, privateKey, fee); - console.log(JSON.stringify(result, undefined, 2)); + const success = `{"success":${result.code==0}}` + console.log(argv.verbose ? JSON.stringify(result, undefined, 2): JSON.stringify(JSON.parse(success))); } diff --git a/src/cmds/cns-cmds/record-cmds/publish.ts b/src/cmds/cns-cmds/record-cmds/publish.ts index 8944bc8..96756c1 100644 --- a/src/cmds/cns-cmds/record-cmds/publish.ts +++ b/src/cmds/cns-cmds/record-cmds/publish.ts @@ -40,5 +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); - console.log(verbose ? JSON.stringify(result, undefined, 2) : result.data); + console.log(verbose ? JSON.stringify(result, undefined, 2) : JSON.stringify(result.data,undefined,2)); } diff --git a/watcher.yml b/watcher.yml new file mode 100644 index 0000000..58502aa --- /dev/null +++ b/watcher.yml @@ -0,0 +1,7 @@ +record: + type: WebsiteRegistrationRecord + url: 'https://cerc.io' + repo_registration_record_cid: QmSnuWmxptJZdLJpKRarxBMS2Ju2oANVrgbr2xWbie9b2D + build_artifact_cid: QmP8jTG1m9GSDJLCbeWhVSVgEzCPPwXRdCRuJtQ5Tz9Kc9 + tls_cert_cid: QmbWqxBEKC3P8tqsKc98xmWNzrzDtRLMiMPL8wBuTGsMnR + version: 1.0.35 From 1de17a40ef88f97841efe4d2d4b8b95898a6d901 Mon Sep 17 00:00:00 2001 From: 0xmuralik Date: Mon, 27 Mar 2023 13:58:41 +0530 Subject: [PATCH 2/6] authority and auction output --- src/cmds/cns-cmds/auction-cmds/bid-cmds/commit.ts | 5 ++--- src/cmds/cns-cmds/auction-cmds/bid-cmds/reveal.ts | 3 ++- src/cmds/cns-cmds/authority-cmds/bond-cmds/set.ts | 3 ++- src/cmds/cns-cmds/authority-cmds/reserve.ts | 3 ++- 4 files changed, 8 insertions(+), 6 deletions(-) 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 a9c6527..328a533 100644 --- a/src/cmds/cns-cmds/auction-cmds/bid-cmds/commit.ts +++ b/src/cmds/cns-cmds/auction-cmds/bid-cmds/commit.ts @@ -43,7 +43,6 @@ export const handler = async (argv: Arguments) => { const fee = getGasAndFees(argv, cnsConfig); const result = await registry.commitBid({ auctionId, commitHash }, privateKey, fee); - console.log(JSON.stringify(result, undefined, 2)); - - console.log(`\nReveal file: ${revealFilePath}`); + const revealFile = `{"reveal_file":"${revealFilePath}"}` + console.log(argv.verbose ? JSON.stringify(result, undefined, 2)+ JSON.stringify(JSON.parse(revealFile)) : JSON.stringify(JSON.parse(revealFile))); } 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 f7290d9..cd6eae8 100644 --- a/src/cmds/cns-cmds/auction-cmds/bid-cmds/reveal.ts +++ b/src/cmds/cns-cmds/auction-cmds/bid-cmds/reveal.ts @@ -28,5 +28,6 @@ export const handler = async (argv: Arguments) => { const reveal = fs.readFileSync(path.resolve(filePath)); const result = await registry.revealBid({ auctionId, reveal: reveal.toString('hex') }, privateKey, fee); - console.log(JSON.stringify(result, undefined, 2)); + const success = `{"success":${result.code==0}}` + console.log(argv.verbose ? JSON.stringify(result, undefined, 2): JSON.stringify(JSON.parse(success))); } 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 2bf39cd..ca2cb12 100644 --- a/src/cmds/cns-cmds/authority-cmds/bond-cmds/set.ts +++ b/src/cmds/cns-cmds/authority-cmds/bond-cmds/set.ts @@ -24,5 +24,6 @@ export const handler = async (argv: Arguments) => { const registry = new Registry(gqlEndpoint, restEndpoint, chainId); const fee = getGasAndFees(argv, cnsConfig); const result = await registry.setAuthorityBond({ name, bondId }, privateKey, fee); - console.log(JSON.stringify(result, undefined, 2)); + const success = `{"success":${result.code==0}}` + console.log(argv.verbose ? JSON.stringify(result, undefined, 2): JSON.stringify(JSON.parse(success))); } diff --git a/src/cmds/cns-cmds/authority-cmds/reserve.ts b/src/cmds/cns-cmds/authority-cmds/reserve.ts index 7bedf23..84a53ad 100644 --- a/src/cmds/cns-cmds/authority-cmds/reserve.ts +++ b/src/cmds/cns-cmds/authority-cmds/reserve.ts @@ -31,5 +31,6 @@ export const handler = async (argv: Arguments) => { const fee = getGasAndFees(argv, cnsConfig); const result = await registry.reserveAuthority({ name, owner }, privateKey, fee); - console.log(JSON.stringify(result, undefined, 2)); + const success = `{"success":${result.code==0}}` + console.log(argv.verbose ? JSON.stringify(result, undefined, 2): JSON.stringify(JSON.parse(success))); } From 9b9e8829ec20253d43934aca6820a384a114f41a Mon Sep 17 00:00:00 2001 From: 0xmuralik Date: Mon, 27 Mar 2023 16:58:14 +0530 Subject: [PATCH 3/6] name outputs --- src/cmds/cns-cmds/name-cmds/delete.ts | 3 ++- src/cmds/cns-cmds/name-cmds/resolve.ts | 3 ++- src/cmds/cns-cmds/name-cmds/set.ts | 3 ++- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/cmds/cns-cmds/name-cmds/delete.ts b/src/cmds/cns-cmds/name-cmds/delete.ts index 32ff8f9..0c59532 100644 --- a/src/cmds/cns-cmds/name-cmds/delete.ts +++ b/src/cmds/cns-cmds/name-cmds/delete.ts @@ -23,5 +23,6 @@ export const handler = async (argv: Arguments) => { const fee = getGasAndFees(argv, cnsConfig); const result = await registry.deleteName({ crn: name }, privateKey, fee); - console.log(JSON.stringify(result, undefined, 2)); + const success = `{"success":${result.code==0}}` + console.log(argv.verbose ? JSON.stringify(result, undefined, 2): JSON.stringify(JSON.parse(success))); } diff --git a/src/cmds/cns-cmds/name-cmds/resolve.ts b/src/cmds/cns-cmds/name-cmds/resolve.ts index c6b651c..c5d6e5a 100644 --- a/src/cmds/cns-cmds/name-cmds/resolve.ts +++ b/src/cmds/cns-cmds/name-cmds/resolve.ts @@ -21,5 +21,6 @@ export const handler = async (argv: Arguments) => { const registry = new Registry(gqlEndpoint, restEndpoint, chainId); const result = await registry.resolveNames([name]); - console.log(JSON.stringify(result, undefined, 4)); + const success = `{"success":${result.code==0}}` + console.log(argv.verbose ? JSON.stringify(result, undefined, 2): JSON.stringify(JSON.parse(success))); } diff --git a/src/cmds/cns-cmds/name-cmds/set.ts b/src/cmds/cns-cmds/name-cmds/set.ts index addb989..9c541e4 100644 --- a/src/cmds/cns-cmds/name-cmds/set.ts +++ b/src/cmds/cns-cmds/name-cmds/set.ts @@ -25,5 +25,6 @@ export const handler = async (argv: Arguments) => { const fee = getGasAndFees(argv, cnsConfig); const result = await registry.setName({ crn: name, cid: id }, privateKey, fee); - console.log(JSON.stringify(result, undefined, 2)); + const success = `{"success":${result.code==0}}` + console.log(argv.verbose ? JSON.stringify(result, undefined, 2): JSON.stringify(JSON.parse(success))); } From 8ebb80beed37bc38b1cf6b4c0f6d1ef711872485 Mon Sep 17 00:00:00 2001 From: 0xmuralik Date: Mon, 27 Mar 2023 17:35:47 +0530 Subject: [PATCH 4/6] json format --- src/cmds/cns-cmds/account-cmds/get.ts | 8 ++++++-- src/cmds/cns-cmds/auction-cmds/bid-cmds/commit.ts | 6 ++++++ src/cmds/cns-cmds/auction-cmds/bid-cmds/reveal.ts | 7 ++++++- src/cmds/cns-cmds/auction-cmds/get.ts | 6 +++++- src/cmds/cns-cmds/authority-cmds/bond-cmds/set.ts | 6 +++++- src/cmds/cns-cmds/authority-cmds/reserve.ts | 6 +++++- src/cmds/cns-cmds/authority-cmds/whois.ts | 6 +++++- src/cmds/cns-cmds/bond-cmds/associate.ts | 6 +++++- src/cmds/cns-cmds/bond-cmds/cancel.ts | 6 +++++- src/cmds/cns-cmds/bond-cmds/create.ts | 7 ++++++- src/cmds/cns-cmds/bond-cmds/dissociate.ts | 6 +++++- src/cmds/cns-cmds/bond-cmds/get.ts | 7 ++++++- src/cmds/cns-cmds/bond-cmds/list.ts | 7 ++++++- src/cmds/cns-cmds/bond-cmds/records-cmds/dissociate.ts | 6 +++++- src/cmds/cns-cmds/bond-cmds/records-cmds/reassociate.ts | 6 +++++- src/cmds/cns-cmds/bond-cmds/refill.ts | 6 +++++- src/cmds/cns-cmds/bond-cmds/withdraw.ts | 6 +++++- src/cmds/cns-cmds/name-cmds/delete.ts | 6 +++++- src/cmds/cns-cmds/name-cmds/lookup.ts | 6 +++++- src/cmds/cns-cmds/name-cmds/resolve.ts | 6 +++++- src/cmds/cns-cmds/name-cmds/set.ts | 6 +++++- src/cmds/cns-cmds/record-cmds/get.ts | 6 +++++- src/cmds/cns-cmds/record-cmds/list.ts | 6 +++++- src/cmds/cns-cmds/record-cmds/publish.ts | 6 +++++- src/cmds/cns-cmds/tokens-cmds/send.ts | 6 +++++- src/index.ts | 5 +++++ 26 files changed, 136 insertions(+), 25 deletions(-) diff --git a/src/cmds/cns-cmds/account-cmds/get.ts b/src/cmds/cns-cmds/account-cmds/get.ts index da3486d..2fad6fa 100644 --- a/src/cmds/cns-cmds/account-cmds/get.ts +++ b/src/cmds/cns-cmds/account-cmds/get.ts @@ -23,6 +23,10 @@ export const handler = async (argv: Arguments) => { const registry = new Registry(gqlEndpoint, restEndpoint, chainId); const result = await registry.getAccounts([address]); - - console.log(JSON.stringify(result, undefined, 2)); + + if (argv.output=="json"){ + console.log(JSON.stringify(result, undefined, 2)); + } else { + console.log(result) + } } 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 328a533..89d6e29 100644 --- a/src/cmds/cns-cmds/auction-cmds/bid-cmds/commit.ts +++ b/src/cmds/cns-cmds/auction-cmds/bid-cmds/commit.ts @@ -45,4 +45,10 @@ 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) + } } 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 cd6eae8..3a41ca0 100644 --- a/src/cmds/cns-cmds/auction-cmds/bid-cmds/reveal.ts +++ b/src/cmds/cns-cmds/auction-cmds/bid-cmds/reveal.ts @@ -29,5 +29,10 @@ export const handler = async (argv: Arguments) => { const reveal = fs.readFileSync(path.resolve(filePath)); const result = await registry.revealBid({ auctionId, reveal: reveal.toString('hex') }, privateKey, fee); const success = `{"success":${result.code==0}}` - console.log(argv.verbose ? JSON.stringify(result, undefined, 2): JSON.stringify(JSON.parse(success))); + + 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); + } } diff --git a/src/cmds/cns-cmds/auction-cmds/get.ts b/src/cmds/cns-cmds/auction-cmds/get.ts index 00f3bc9..eda8aea 100644 --- a/src/cmds/cns-cmds/auction-cmds/get.ts +++ b/src/cmds/cns-cmds/auction-cmds/get.ts @@ -21,5 +21,9 @@ export const handler = async (argv: Arguments) => { const registry = new Registry(gqlEndpoint, restEndpoint, chainId); const result = await registry.getAuctionsByIds([id as string]); - console.log(JSON.stringify(result, undefined, 2)); + if (argv.output=="json"){ + console.log(JSON.stringify(result, undefined, 2)); + } else { + console.log(result) + } } 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 ca2cb12..65d9289 100644 --- a/src/cmds/cns-cmds/authority-cmds/bond-cmds/set.ts +++ b/src/cmds/cns-cmds/authority-cmds/bond-cmds/set.ts @@ -25,5 +25,9 @@ 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}}` - console.log(argv.verbose ? JSON.stringify(result, undefined, 2): JSON.stringify(JSON.parse(success))); + 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); + } } diff --git a/src/cmds/cns-cmds/authority-cmds/reserve.ts b/src/cmds/cns-cmds/authority-cmds/reserve.ts index 84a53ad..9791b06 100644 --- a/src/cmds/cns-cmds/authority-cmds/reserve.ts +++ b/src/cmds/cns-cmds/authority-cmds/reserve.ts @@ -32,5 +32,9 @@ export const handler = async (argv: Arguments) => { const result = await registry.reserveAuthority({ name, owner }, privateKey, fee); const success = `{"success":${result.code==0}}` - console.log(argv.verbose ? JSON.stringify(result, undefined, 2): JSON.stringify(JSON.parse(success))); + 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); + } } diff --git a/src/cmds/cns-cmds/authority-cmds/whois.ts b/src/cmds/cns-cmds/authority-cmds/whois.ts index 118f24c..3faea15 100644 --- a/src/cmds/cns-cmds/authority-cmds/whois.ts +++ b/src/cmds/cns-cmds/authority-cmds/whois.ts @@ -21,5 +21,9 @@ export const handler = async (argv: Arguments) => { const registry = new Registry(gqlEndpoint, restEndpoint, chainId); const result = await registry.lookupAuthorities([name], true); - console.log(JSON.stringify(result, undefined, 2)); + if (argv.output=="json"){ + console.log(JSON.stringify(result, undefined, 2)); + } else { + console.log(result) + } } diff --git a/src/cmds/cns-cmds/bond-cmds/associate.ts b/src/cmds/cns-cmds/bond-cmds/associate.ts index 755d10c..978f98d 100644 --- a/src/cmds/cns-cmds/bond-cmds/associate.ts +++ b/src/cmds/cns-cmds/bond-cmds/associate.ts @@ -31,5 +31,9 @@ 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}}` - console.log(argv.verbose ? JSON.stringify(result, undefined, 2): JSON.stringify(JSON.parse(success))); + 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); + } } diff --git a/src/cmds/cns-cmds/bond-cmds/cancel.ts b/src/cmds/cns-cmds/bond-cmds/cancel.ts index ca3d3e7..41aea64 100644 --- a/src/cmds/cns-cmds/bond-cmds/cancel.ts +++ b/src/cmds/cns-cmds/bond-cmds/cancel.ts @@ -23,5 +23,9 @@ 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}}` - console.log(argv.verbose ? JSON.stringify(result, undefined, 2): JSON.stringify(JSON.parse(success))); + 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); + } } diff --git a/src/cmds/cns-cmds/bond-cmds/create.ts b/src/cmds/cns-cmds/bond-cmds/create.ts index cf4be9e..a19d274 100644 --- a/src/cmds/cns-cmds/bond-cmds/create.ts +++ b/src/cmds/cns-cmds/bond-cmds/create.ts @@ -37,5 +37,10 @@ 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}"}` - console.log(verbose ? JSON.stringify(result, undefined, 2) : JSON.stringify(JSON.parse(jsonString),null,2)); + + 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); + } } diff --git a/src/cmds/cns-cmds/bond-cmds/dissociate.ts b/src/cmds/cns-cmds/bond-cmds/dissociate.ts index 7a60a1d..c4ed5da 100644 --- a/src/cmds/cns-cmds/bond-cmds/dissociate.ts +++ b/src/cmds/cns-cmds/bond-cmds/dissociate.ts @@ -23,5 +23,9 @@ 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}}` - console.log(argv.verbose ? JSON.stringify(result, undefined, 2): JSON.stringify(JSON.parse(success))); + 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); + } } diff --git a/src/cmds/cns-cmds/bond-cmds/get.ts b/src/cmds/cns-cmds/bond-cmds/get.ts index ad2af5c..15d0a93 100644 --- a/src/cmds/cns-cmds/bond-cmds/get.ts +++ b/src/cmds/cns-cmds/bond-cmds/get.ts @@ -21,5 +21,10 @@ export const handler = async (argv: Arguments) => { const registry = new Registry(gqlEndpoint, restEndpoint, chainId); const result = await registry.getBondsByIds([id as string]); - console.log(JSON.stringify(result, undefined, 2)); + + if (argv.output=="json"){ + console.log(JSON.stringify(result, undefined, 2)); + } else { + console.log(result) + } } diff --git a/src/cmds/cns-cmds/bond-cmds/list.ts b/src/cmds/cns-cmds/bond-cmds/list.ts index 965fa79..c9a8504 100644 --- a/src/cmds/cns-cmds/bond-cmds/list.ts +++ b/src/cmds/cns-cmds/bond-cmds/list.ts @@ -25,5 +25,10 @@ export const handler = async (argv: Arguments) => { const { owner } = argv; const result = await registry.queryBonds({ owner }); - console.log(JSON.stringify(result, undefined, 2)); + + if (argv.output=="json"){ + console.log(JSON.stringify(result, undefined, 2)); + } else { + console.log(result) + } } 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 2d50c00..4523521 100644 --- a/src/cmds/cns-cmds/bond-cmds/records-cmds/dissociate.ts +++ b/src/cmds/cns-cmds/bond-cmds/records-cmds/dissociate.ts @@ -29,5 +29,9 @@ 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}}` - console.log(argv.verbose ? JSON.stringify(result, undefined, 2): JSON.stringify(JSON.parse(success))); + 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); + } } 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 4696834..5c4cf2c 100644 --- a/src/cmds/cns-cmds/bond-cmds/records-cmds/reassociate.ts +++ b/src/cmds/cns-cmds/bond-cmds/records-cmds/reassociate.ts @@ -34,5 +34,9 @@ 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}}` - console.log(argv.verbose ? JSON.stringify(result, undefined, 2): JSON.stringify(JSON.parse(success))); + 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); + } } diff --git a/src/cmds/cns-cmds/bond-cmds/refill.ts b/src/cmds/cns-cmds/bond-cmds/refill.ts index 1627d3e..7e610d7 100644 --- a/src/cmds/cns-cmds/bond-cmds/refill.ts +++ b/src/cmds/cns-cmds/bond-cmds/refill.ts @@ -38,5 +38,9 @@ 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}}` - console.log(argv.verbose ? JSON.stringify(result, undefined, 2): JSON.stringify(JSON.parse(success))); + 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); + } } diff --git a/src/cmds/cns-cmds/bond-cmds/withdraw.ts b/src/cmds/cns-cmds/bond-cmds/withdraw.ts index 79cd313..0fe6d61 100644 --- a/src/cmds/cns-cmds/bond-cmds/withdraw.ts +++ b/src/cmds/cns-cmds/bond-cmds/withdraw.ts @@ -37,5 +37,9 @@ 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}}` - console.log(argv.verbose ? JSON.stringify(result, undefined, 2): JSON.stringify(JSON.parse(success))); + 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); + } } diff --git a/src/cmds/cns-cmds/name-cmds/delete.ts b/src/cmds/cns-cmds/name-cmds/delete.ts index 0c59532..c1e891a 100644 --- a/src/cmds/cns-cmds/name-cmds/delete.ts +++ b/src/cmds/cns-cmds/name-cmds/delete.ts @@ -24,5 +24,9 @@ export const handler = async (argv: Arguments) => { const result = await registry.deleteName({ crn: name }, privateKey, fee); const success = `{"success":${result.code==0}}` - console.log(argv.verbose ? JSON.stringify(result, undefined, 2): JSON.stringify(JSON.parse(success))); + 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); + } } diff --git a/src/cmds/cns-cmds/name-cmds/lookup.ts b/src/cmds/cns-cmds/name-cmds/lookup.ts index 492f760..56cffda 100644 --- a/src/cmds/cns-cmds/name-cmds/lookup.ts +++ b/src/cmds/cns-cmds/name-cmds/lookup.ts @@ -27,5 +27,9 @@ export const handler = async (argv: Arguments) => { const registry = new Registry(gqlEndpoint, restEndpoint, chainId); const result = await registry.lookupNames([name], argv.history as boolean); - console.log(JSON.stringify(result, undefined, 2)); + if (argv.output=="json"){ + console.log(JSON.stringify(result, undefined, 2)); + } else { + console.log(result) + } } diff --git a/src/cmds/cns-cmds/name-cmds/resolve.ts b/src/cmds/cns-cmds/name-cmds/resolve.ts index c5d6e5a..15d3504 100644 --- a/src/cmds/cns-cmds/name-cmds/resolve.ts +++ b/src/cmds/cns-cmds/name-cmds/resolve.ts @@ -22,5 +22,9 @@ export const handler = async (argv: Arguments) => { const result = await registry.resolveNames([name]); const success = `{"success":${result.code==0}}` - console.log(argv.verbose ? JSON.stringify(result, undefined, 2): JSON.stringify(JSON.parse(success))); + 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); + } } diff --git a/src/cmds/cns-cmds/name-cmds/set.ts b/src/cmds/cns-cmds/name-cmds/set.ts index 9c541e4..2258720 100644 --- a/src/cmds/cns-cmds/name-cmds/set.ts +++ b/src/cmds/cns-cmds/name-cmds/set.ts @@ -26,5 +26,9 @@ export const handler = async (argv: Arguments) => { const result = await registry.setName({ crn: name, cid: id }, privateKey, fee); const success = `{"success":${result.code==0}}` - console.log(argv.verbose ? JSON.stringify(result, undefined, 2): JSON.stringify(JSON.parse(success))); + 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); + } } diff --git a/src/cmds/cns-cmds/record-cmds/get.ts b/src/cmds/cns-cmds/record-cmds/get.ts index 9d0fc81..a24e9ef 100644 --- a/src/cmds/cns-cmds/record-cmds/get.ts +++ b/src/cmds/cns-cmds/record-cmds/get.ts @@ -21,5 +21,9 @@ export const handler = async (argv: Arguments) => { const registry = new Registry(gqlEndpoint, restEndpoint, chainId); const result = await registry.getRecordsByIds([id as string]); - console.log(JSON.stringify(result, undefined, 2)); + if (argv.output=="json"){ + console.log(JSON.stringify(result, undefined, 2)); + } else { + console.log(result) + } } diff --git a/src/cmds/cns-cmds/record-cmds/list.ts b/src/cmds/cns-cmds/record-cmds/list.ts index d29ea02..ea4496c 100644 --- a/src/cmds/cns-cmds/record-cmds/list.ts +++ b/src/cmds/cns-cmds/record-cmds/list.ts @@ -36,5 +36,9 @@ export const handler = async (argv: Arguments) => { const registry = new Registry(gqlEndpoint, restEndpoint, chainId); const result = await registry.queryRecords({ bondId, type, name }, all as boolean); - console.log(JSON.stringify(result, undefined, 2)); + if (argv.output=="json"){ + console.log(JSON.stringify(result, undefined, 2)); + } else { + console.log(result) + } } diff --git a/src/cmds/cns-cmds/record-cmds/publish.ts b/src/cmds/cns-cmds/record-cmds/publish.ts index 96756c1..cef81ed 100644 --- a/src/cmds/cns-cmds/record-cmds/publish.ts +++ b/src/cmds/cns-cmds/record-cmds/publish.ts @@ -40,5 +40,9 @@ export const handler = async (argv: Arguments) => { const fee = getGasAndFees(argv, cnsConfig); const result = await registry.setRecord({ privateKey: userKey, record, bondId }, txKey as string, fee); - console.log(verbose ? JSON.stringify(result, undefined, 2) : JSON.stringify(result.data,undefined,2)); + 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); + } } diff --git a/src/cmds/cns-cmds/tokens-cmds/send.ts b/src/cmds/cns-cmds/tokens-cmds/send.ts index 1262d41..8ee6c61 100644 --- a/src/cmds/cns-cmds/tokens-cmds/send.ts +++ b/src/cmds/cns-cmds/tokens-cmds/send.ts @@ -40,5 +40,9 @@ export const handler = async (argv: Arguments) => { const fee = getGasAndFees(argv, cnsConfig); await registry.sendCoins({ denom, amount, destinationAddress }, privateKey, fee); const result = await registry.getAccounts([fromAddress, destinationAddress]); - console.log(JSON.stringify(result, undefined, 2)); + if (argv.output=="json"){ + console.log(JSON.stringify(result, undefined, 2)); + } else { + console.log(result) + } } diff --git a/src/index.ts b/src/index.ts index 29723e2..eff7e67 100644 --- a/src/index.ts +++ b/src/index.ts @@ -15,6 +15,11 @@ yargs(hideBin(process.argv)) default: 'config.yml', describe: 'Config file path.', type: 'string' + }, + output: { + alias: 'o', + describe: 'Gives output in json format when specified.', + type: 'string' } }) .commandDir('cmds') From 0c0eb79fa31da698f59debee78bb309bc43cedd8 Mon Sep 17 00:00:00 2001 From: 0xmuralik Date: Mon, 27 Mar 2023 18:14:42 +0530 Subject: [PATCH 5/6] 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)); + } +} From 61a2aaab0f8c9e1a5fe4b2c439a046c9fa30a652 Mon Sep 17 00:00:00 2001 From: 0xmuralik Date: Mon, 27 Mar 2023 19:37:16 +0530 Subject: [PATCH 6/6] fix send output --- src/cmds/cns-cmds/tokens-cmds/send.ts | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/cmds/cns-cmds/tokens-cmds/send.ts b/src/cmds/cns-cmds/tokens-cmds/send.ts index 8ee6c61..983a097 100644 --- a/src/cmds/cns-cmds/tokens-cmds/send.ts +++ b/src/cmds/cns-cmds/tokens-cmds/send.ts @@ -2,7 +2,7 @@ import { Arguments } from 'yargs'; import assert from 'assert'; import { Account, Registry } from '@cerc-io/laconic-sdk'; -import { getConfig, getConnectionInfo, getGasAndFees } from '../../../util'; +import { getConfig, getConnectionInfo, getGasAndFees, queryOutput } from '../../../util'; export const command = 'send'; @@ -40,9 +40,5 @@ export const handler = async (argv: Arguments) => { const fee = getGasAndFees(argv, cnsConfig); await registry.sendCoins({ denom, amount, destinationAddress }, privateKey, fee); const result = await registry.getAccounts([fromAddress, destinationAddress]); - if (argv.output=="json"){ - console.log(JSON.stringify(result, undefined, 2)); - } else { - console.log(result) - } + queryOutput(result,argv.output) }