From 0e8de87e3834a580bd1d2fe0f1094ebfc96bec0a Mon Sep 17 00:00:00 2001 From: Hamish Coleman Date: Fri, 5 Nov 2021 09:23:33 +0000 Subject: [PATCH] Add a sort option to the CLI n2n-ctl tool --- scripts/n2n-ctl | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/scripts/n2n-ctl b/scripts/n2n-ctl index ab59f36..4e63eda 100755 --- a/scripts/n2n-ctl +++ b/scripts/n2n-ctl @@ -106,7 +106,7 @@ class JsonUDP(): return self._call('w', cmdline) -def str_table(rows, columns): +def str_table(rows, columns, orderby): """Given an array of dicts, do a simple table print""" result = list() widths = collections.defaultdict(lambda: 0) @@ -127,6 +127,9 @@ def str_table(rows, columns): result += "{:{}.{}} ".format(col, widths[col], widths[col]) result += "\n" + if orderby is not None: + rows = sorted(rows, key=lambda row: row.get(orderby, 0)) + for row in rows: for col in columns: if col in row: @@ -149,7 +152,7 @@ def subcmd_show_supernodes(rpc, args): 'uptime', ] - return str_table(rows, columns) + return str_table(rows, columns, args.orderby) def subcmd_show_edges(rpc, args): @@ -162,7 +165,7 @@ def subcmd_show_edges(rpc, args): 'desc', ] - return str_table(rows, columns) + return str_table(rows, columns, args.orderby) def subcmd_show_help(rpc, args): @@ -216,6 +219,8 @@ def main(): help='Also show raw internal data') ap.add_argument('--raw', action='store_true', help='Force cmd to avoid any pretty printing') + ap.add_argument('--orderby', action='store', + help='Hint to a pretty printer on how to sort') group = ap.add_mutually_exclusive_group() group.add_argument('--read', action='store_true',