From 8ebb80beed37bc38b1cf6b4c0f6d1ef711872485 Mon Sep 17 00:00:00 2001 From: 0xmuralik Date: Mon, 27 Mar 2023 17:35:47 +0530 Subject: [PATCH] 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')