Skip to content

Commit

Permalink
feat(cli): extend cli callback with optional formatting cb
Browse files Browse the repository at this point in the history
Changes callback to return a function instead so that we can inject an
optional formatting callback.
  • Loading branch information
Karl Ranna committed Nov 6, 2018
1 parent 1e5ee52 commit 843f683
Show file tree
Hide file tree
Showing 19 changed files with 51 additions and 26 deletions.
22 changes: 13 additions & 9 deletions lib/cli/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
}
};
};
2 changes: 1 addition & 1 deletion lib/cli/commands/addcurrency.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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());
};
2 changes: 1 addition & 1 deletion lib/cli/commands/addpair.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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());
};
2 changes: 1 addition & 1 deletion lib/cli/commands/ban.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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());
};
2 changes: 1 addition & 1 deletion lib/cli/commands/cancelorder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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());
};
2 changes: 1 addition & 1 deletion lib/cli/commands/channelbalance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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());
};
2 changes: 1 addition & 1 deletion lib/cli/commands/connect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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());
};
2 changes: 1 addition & 1 deletion lib/cli/commands/executeswap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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());
};
2 changes: 1 addition & 1 deletion lib/cli/commands/getinfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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());
};
2 changes: 1 addition & 1 deletion lib/cli/commands/getnodeinfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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());
};
2 changes: 1 addition & 1 deletion lib/cli/commands/getorders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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());
};
2 changes: 1 addition & 1 deletion lib/cli/commands/listpairs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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());
};
2 changes: 1 addition & 1 deletion lib/cli/commands/listpeers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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());
};
2 changes: 1 addition & 1 deletion lib/cli/commands/removecurrency.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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());
};
2 changes: 1 addition & 1 deletion lib/cli/commands/removepair.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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());
};
2 changes: 1 addition & 1 deletion lib/cli/commands/shutdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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());
};
2 changes: 1 addition & 1 deletion lib/cli/commands/unban.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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());
};
2 changes: 1 addition & 1 deletion lib/cli/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
};
21 changes: 21 additions & 0 deletions test/unit/Command.spec.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});

0 comments on commit 843f683

Please sign in to comment.