Skip to content

Commit d093597

Browse files
rsercanoImmanuelSegol
authored andcommitted
feat(cli): enhance listpeers output
Co-authored-by: ImmanuelSegol <3ditds@gmail.com>
1 parent f08eef9 commit d093597

File tree

2 files changed

+84
-3
lines changed

2 files changed

+84
-3
lines changed

lib/cli/commands/listpeers.ts

Lines changed: 77 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,85 @@
1-
import { callback, loadXudClient } from '../command';
1+
import Table, { HorizontalTable } from 'cli-table3';
2+
import colors from 'colors/safe';
23
import { Arguments } from 'yargs';
3-
import { ListPeersRequest } from '../../proto/xudrpc_pb';
4+
import { ListPeersRequest, ListPeersResponse, Peer } from '../../proto/xudrpc_pb';
5+
import { callback, loadXudClient } from '../command';
6+
import { generateHeaders } from '../utils';
7+
8+
const HEADERS = [
9+
'Peer',
10+
'Pairs',
11+
'Details',
12+
];
13+
14+
const createTable = () => {
15+
const table = new Table({
16+
head: generateHeaders(HEADERS),
17+
}) as HorizontalTable;
18+
return table;
19+
};
20+
21+
const trimPubKey = (key: string) => {
22+
if (key.length <= 0) {
23+
return '';
24+
}
25+
return `${key.slice(0, 10)}...${key.slice(key.length - 10)}`;
26+
};
27+
28+
const formatPairList = (pairs: string[]) => {
29+
let pairString = '';
30+
pairs.forEach((pair) => {
31+
/* eslint disable-next-line */
32+
pairString = `${pairString}${pairString ? '\n' : ''}${pair}`;
33+
});
34+
return pairString;
35+
};
36+
37+
const formatLndPubKeys = (lndKeys: string[][]) => {
38+
let str = '';
39+
lndKeys.forEach((client) => {
40+
/* eslint disable-next-line */
41+
str = `${str}${str ? '\n' : ''}${client[0]} lnd key: ${trimPubKey(client[1])}`;
42+
});
43+
return str;
44+
};
45+
46+
const formatPeers = (peers: ListPeersResponse.AsObject) => {
47+
const formattedPeers: string[][] = [];
48+
peers.peersList.forEach((peer: Peer.AsObject) => {
49+
50+
const address = `${peer.nodePubKey}
51+
@${peer.address}` ;
52+
53+
const details = [
54+
`Alias:
55+
56+
Address ([node_key]@[host]:[port]):
57+
${address}`,
58+
formatPairList(peer.pairsList),
59+
`inbound: ${peer.inbound}\
60+
\nversion: ${peer.xudVersion}\
61+
\ntime connected: ${peer.secondsConnected.toString()} seconds\
62+
\n${formatLndPubKeys(peer.lndPubKeysMap)}\
63+
${peer.raidenAddress ? `\nraiden address: ${trimPubKey(peer.raidenAddress)}` : ''}`,
64+
];
65+
formattedPeers.push(details);
66+
});
67+
return formattedPeers;
68+
};
69+
70+
const displayTables = (peers: ListPeersResponse.AsObject) => {
71+
const table = createTable();
72+
formatPeers(peers).forEach((peer) => {
73+
table.push(peer);
74+
});
75+
console.log(colors.underline(colors.bold('\nPeers:')));
76+
console.log(table.toString());
77+
};
478

579
export const command = 'listpeers';
680

781
export const describe = 'list connected peers';
882

983
export const handler = (argv: Arguments) => {
10-
loadXudClient(argv).listPeers(new ListPeersRequest(), callback(argv));
84+
loadXudClient(argv).listPeers(new ListPeersRequest(), callback(argv, displayTables));
1185
};

lib/cli/utils.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import colors from 'colors/safe';
12
import os from 'os';
23
import path from 'path';
34

@@ -20,6 +21,12 @@ export function getDefaultCertPath() {
2021
}
2122
}
2223

24+
export const generateHeaders = (headers: string[]) => {
25+
return headers.map((header) => {
26+
return colors.blue(header);
27+
});
28+
};
29+
2330
/** Returns a number of coins as an integer number of satoshis. */
2431
export const coinsToSats = (coinsQuantity: number) => {
2532
return Math.round(coinsQuantity * SATOSHIS_PER_COIN);

0 commit comments

Comments
 (0)