Add a sort option to the CLI n2n-ctl tool

This commit is contained in:
Hamish Coleman 2021-11-05 09:23:33 +00:00
parent 9c3a2c3301
commit 0e8de87e38

View File

@ -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',