Skip to content

Commit

Permalink
op-mode: T6753: Fix json output for mtr / monitor traceroute (#4122)
Browse files Browse the repository at this point in the history
(cherry picked from commit 17c9b44)
  • Loading branch information
natali-rs1985 authored and mergify[bot] committed Oct 8, 2024
1 parent 47b1ace commit 1b057f4
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/op_mode/mtr.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@
6: '/bin/mtr -6',
}


class List(list):
def first(self):
return self.pop(0) if self else ''
Expand Down Expand Up @@ -218,12 +219,15 @@ def complete(prefix):


def convert(command, args):
to_json = False
while args:
shortname = args.first()
longnames = complete(shortname)
if len(longnames) != 1:
expension_failure(shortname, longnames)
longname = longnames[0]
if longname == 'json':
to_json = True
if options[longname]['type'] == 'noarg':
command = options[longname]['mtr'].format(
command=command, value='')
Expand All @@ -232,7 +236,7 @@ def convert(command, args):
else:
command = options[longname]['mtr'].format(
command=command, value=args.first())
return command
return command, to_json


if __name__ == '__main__':
Expand All @@ -242,7 +246,6 @@ def convert(command, args):
if not host:
sys.exit("mtr: Missing host")


if host == '--get-options' or host == '--get-options-nested':
if host == '--get-options-nested':
args.first() # pop monitor
Expand Down Expand Up @@ -302,5 +305,8 @@ def convert(command, args):
except ValueError:
sys.exit(f'mtr: Unknown host: {host}')

command = convert(mtr[version], args)
call(f'{command} --curses --displaymode 0 {host}')
command, to_json = convert(mtr[version], args)
if to_json:
call(f'{command} {host}')
else:
call(f'{command} --curses --displaymode 0 {host}')

0 comments on commit 1b057f4

Please sign in to comment.