diff --git a/lib/cli/command.ts b/lib/cli/command.ts index b04c5e9e3..90bd1c09a 100644 --- a/lib/cli/command.ts +++ b/lib/cli/command.ts @@ -40,15 +40,19 @@ interface grpcResponse { * @param argv the command line arguments * @param callback the callback function to perform a command */ -export const callback = (error: Error | null, response: grpcResponse) => { - if (error) { - console.error(`${error.name}: ${error.message}`); - } else { - const responseObj = response.toObject(); - if (Object.keys(responseObj).length === 0) { - console.log('success'); +export const callback = (formatOutput?: Function) => { + return (error: Error | null, response: grpcResponse) => { + if (error) { + console.error(`${error.name}: ${error.message}`); } else { - console.log(JSON.stringify(responseObj, undefined, 2)); + const responseObj = response.toObject(); + if (Object.keys(responseObj).length === 0) { + console.log('success'); + } else { + formatOutput + ? formatOutput(responseObj) + : console.log(JSON.stringify(responseObj, undefined, 2)); + } } - } + }; }; diff --git a/lib/cli/commands/addcurrency.ts b/lib/cli/commands/addcurrency.ts index c7b65363a..dccd93708 100644 --- a/lib/cli/commands/addcurrency.ts +++ b/lib/cli/commands/addcurrency.ts @@ -34,5 +34,5 @@ export const handler = (argv: Arguments) => { request.setSwapClient(Number(SwapClients[argv.swap_client])); request.setTokenAddress(argv.token_address); request.setDecimalPlaces(argv.decimal_places); - loadXudClient(argv).addCurrency(request, callback); + loadXudClient(argv).addCurrency(request, callback()); }; diff --git a/lib/cli/commands/addpair.ts b/lib/cli/commands/addpair.ts index 1b5375c14..472684cbc 100644 --- a/lib/cli/commands/addpair.ts +++ b/lib/cli/commands/addpair.ts @@ -21,5 +21,5 @@ export const handler = (argv: Arguments) => { const request = new AddPairRequest(); request.setBaseCurrency(argv.base_currency.toUpperCase()); request.setQuoteCurrency(argv.quote_currency.toUpperCase()); - loadXudClient(argv).addPair(request, callback); + loadXudClient(argv).addPair(request, callback()); }; diff --git a/lib/cli/commands/ban.ts b/lib/cli/commands/ban.ts index f6f7789ff..ba37858a4 100644 --- a/lib/cli/commands/ban.ts +++ b/lib/cli/commands/ban.ts @@ -16,5 +16,5 @@ export const builder = { export const handler = (argv: Arguments) => { const request = new BanRequest(); request.setNodePubKey(argv.node_pub_key); - loadXudClient(argv).ban(request, callback); + loadXudClient(argv).ban(request, callback()); }; diff --git a/lib/cli/commands/cancelorder.ts b/lib/cli/commands/cancelorder.ts index f72f6f5ce..15f5f1e80 100644 --- a/lib/cli/commands/cancelorder.ts +++ b/lib/cli/commands/cancelorder.ts @@ -15,5 +15,5 @@ export const builder = { export const handler = (argv: Arguments) => { const request = new RemoveOrderRequest(); request.setOrderId(argv.order_id); - loadXudClient(argv).removeOrder(request, callback); + loadXudClient(argv).removeOrder(request, callback()); }; diff --git a/lib/cli/commands/channelbalance.ts b/lib/cli/commands/channelbalance.ts index c8a19e294..446edf65a 100644 --- a/lib/cli/commands/channelbalance.ts +++ b/lib/cli/commands/channelbalance.ts @@ -18,5 +18,5 @@ export const handler = (argv: Arguments) => { if (argv.currency) { request.setCurrency(argv.currency.toUpperCase()); } - loadXudClient(argv).channelBalance(request, callback); + loadXudClient(argv).channelBalance(request, callback()); }; diff --git a/lib/cli/commands/connect.ts b/lib/cli/commands/connect.ts index 7ff79f536..7fd7b4575 100644 --- a/lib/cli/commands/connect.ts +++ b/lib/cli/commands/connect.ts @@ -16,5 +16,5 @@ export const builder = { export const handler = (argv: Arguments) => { const request = new ConnectRequest(); request.setNodeUri(argv.node_uri); - loadXudClient(argv).connect(request, callback); + loadXudClient(argv).connect(request, callback()); }; diff --git a/lib/cli/commands/executeswap.ts b/lib/cli/commands/executeswap.ts index ed3b8c3c2..a5857f916 100644 --- a/lib/cli/commands/executeswap.ts +++ b/lib/cli/commands/executeswap.ts @@ -28,5 +28,5 @@ export const handler = (argv: Arguments) => { request.setPairId(argv.pair_id); request.setPeerPubKey(argv.maker_peer_pub_key); request.setQuantity(argv.quantity); - loadXudClient(argv).executeSwap(request, callback); + loadXudClient(argv).executeSwap(request, callback()); }; diff --git a/lib/cli/commands/getinfo.ts b/lib/cli/commands/getinfo.ts index 51d15d65a..1f267eed6 100644 --- a/lib/cli/commands/getinfo.ts +++ b/lib/cli/commands/getinfo.ts @@ -7,5 +7,5 @@ export const command = 'getinfo'; export const describe = 'get general info from the xud node'; export const handler = (argv: Arguments) => { - loadXudClient(argv).getInfo(new GetInfoRequest(), callback); + loadXudClient(argv).getInfo(new GetInfoRequest(), callback()); }; diff --git a/lib/cli/commands/getnodeinfo.ts b/lib/cli/commands/getnodeinfo.ts index bf0e6082a..4c3c83dde 100644 --- a/lib/cli/commands/getnodeinfo.ts +++ b/lib/cli/commands/getnodeinfo.ts @@ -16,5 +16,5 @@ export const builder = { export const handler = (argv: Arguments) => { const request = new GetNodeInfoRequest(); request.setNodePubKey(argv.node_pub_key); - loadXudClient(argv).getNodeInfo(request, callback); + loadXudClient(argv).getNodeInfo(request, callback()); }; diff --git a/lib/cli/commands/getorders.ts b/lib/cli/commands/getorders.ts index cd88008cd..969cfd815 100644 --- a/lib/cli/commands/getorders.ts +++ b/lib/cli/commands/getorders.ts @@ -17,5 +17,5 @@ export const handler = (argv: Arguments) => { const pairId = argv.pair_id ? argv.pair_id.toUpperCase() : undefined; request.setPairId(pairId); request.setIncludeOwnOrders(true); - loadXudClient(argv).getOrders(request, callback); + loadXudClient(argv).getOrders(request, callback()); }; diff --git a/lib/cli/commands/listpairs.ts b/lib/cli/commands/listpairs.ts index a1b23c7a2..e262cee6b 100644 --- a/lib/cli/commands/listpairs.ts +++ b/lib/cli/commands/listpairs.ts @@ -7,5 +7,5 @@ export const command = 'listpairs'; export const describe = 'get order book\'s available pairs'; export const handler = (argv: Arguments) => { - loadXudClient(argv).listPairs(new ListPairsRequest(), callback); + loadXudClient(argv).listPairs(new ListPairsRequest(), callback()); }; diff --git a/lib/cli/commands/listpeers.ts b/lib/cli/commands/listpeers.ts index af26873b9..72d804571 100644 --- a/lib/cli/commands/listpeers.ts +++ b/lib/cli/commands/listpeers.ts @@ -7,5 +7,5 @@ export const command = 'listpeers'; export const describe = 'list connected peers'; export const handler = (argv: Arguments) => { - loadXudClient(argv).listPeers(new ListPeersRequest(), callback); + loadXudClient(argv).listPeers(new ListPeersRequest(), callback()); }; diff --git a/lib/cli/commands/removecurrency.ts b/lib/cli/commands/removecurrency.ts index dc1fe7b67..dbedbca50 100644 --- a/lib/cli/commands/removecurrency.ts +++ b/lib/cli/commands/removecurrency.ts @@ -16,5 +16,5 @@ export const builder = { export const handler = (argv: Arguments) => { const request = new RemoveCurrencyRequest(); request.setCurrency(argv.currency.toUpperCase()); - loadXudClient(argv).removeCurrency(request, callback); + loadXudClient(argv).removeCurrency(request, callback()); }; diff --git a/lib/cli/commands/removepair.ts b/lib/cli/commands/removepair.ts index 8d47fbe77..94e614c53 100644 --- a/lib/cli/commands/removepair.ts +++ b/lib/cli/commands/removepair.ts @@ -16,5 +16,5 @@ export const builder = { export const handler = (argv: Arguments) => { const request = new RemovePairRequest(); request.setPairId(argv.pair_id.toUpperCase()); - loadXudClient(argv).removePair(request, callback); + loadXudClient(argv).removePair(request, callback()); }; diff --git a/lib/cli/commands/shutdown.ts b/lib/cli/commands/shutdown.ts index aea8c5f23..b7dc35333 100644 --- a/lib/cli/commands/shutdown.ts +++ b/lib/cli/commands/shutdown.ts @@ -7,5 +7,5 @@ export const command = 'shutdown'; export const describe = 'gracefully shutdown the xud node'; export const handler = (argv: Arguments) => { - loadXudClient(argv).shutdown(new ShutdownRequest(), callback); + loadXudClient(argv).shutdown(new ShutdownRequest(), callback()); }; diff --git a/lib/cli/commands/unban.ts b/lib/cli/commands/unban.ts index e9823b466..e4a708768 100644 --- a/lib/cli/commands/unban.ts +++ b/lib/cli/commands/unban.ts @@ -22,5 +22,5 @@ export const handler = (argv: Arguments) => { const request = new UnbanRequest(); request.setNodePubKey(argv.node_pub_key); request.setReconnect(argv.reconnect); - loadXudClient(argv).unban(request, callback); + loadXudClient(argv).unban(request, callback()); }; diff --git a/lib/cli/utils.ts b/lib/cli/utils.ts index 9cf183d7f..60919e5b4 100644 --- a/lib/cli/utils.ts +++ b/lib/cli/utils.ts @@ -53,6 +53,6 @@ export const orderHandler = (argv: Arguments, isSell = false) => { console.log(JSON.stringify(response.toObject(), undefined, 2)); }); } else { - loadXudClient(argv).placeOrderSync(request, callback); + loadXudClient(argv).placeOrderSync(request, callback()); } }; diff --git a/test/unit/Command.spec.ts b/test/unit/Command.spec.ts new file mode 100644 index 000000000..02ec78abc --- /dev/null +++ b/test/unit/Command.spec.ts @@ -0,0 +1,21 @@ +import { expect } from 'chai'; +import { callback } from '../../lib/cli/command'; + +describe('Command.callback', () => { + it('should call displayTable callback when provided', () => { + let called = false; + const displayTable = () => { + called = true; + }; + const mockGrpcResponse = { + toObject: () => { + return { + orderMap: [], + }; + }, + }; + /* tslint:disable */ + callback(displayTable)(null, mockGrpcResponse); + expect(called).to.equal(true); + }); +});