Skip to content

Commit 2278d10

Browse files
authored
[show] Properly replace port name with alias in command output (sonic-net#664)
1 parent 592413b commit 2278d10

File tree

1 file changed

+27
-12
lines changed

1 file changed

+27
-12
lines changed

show/main.py

+27-12
Original file line numberDiff line numberDiff line change
@@ -333,26 +333,41 @@ def run_command_in_alias_mode(command):
333333
elif output[0].isdigit():
334334
output = " " + output
335335
print_output_in_alias_mode(output, index)
336+
336337
elif command.startswith("nbrshow"):
337338
"""show arp"""
338339
index = 2
339340
if "Vlan" in output:
340341
output = output.replace('Vlan', ' Vlan')
341342
print_output_in_alias_mode(output, index)
342343

344+
elif command.startswith("sudo teamshow"):
345+
"""
346+
sudo teamshow
347+
Search for port names either at the start of a line or preceded immediately by
348+
whitespace and followed immediately by either the end of a line or whitespace
349+
OR followed immediately by '(D)', '(S)', '(D*)' or '(S*)'
350+
"""
351+
converted_output = raw_output
352+
for port_name in iface_alias_converter.port_dict.keys():
353+
converted_output = re.sub(r"(^|\s){}(\([DS]\*{{0,1}}\)(?:$|\s))".format(port_name),
354+
r"\1{}\2".format(iface_alias_converter.name_to_alias(port_name)),
355+
converted_output)
356+
click.echo(converted_output.rstrip('\n'))
357+
343358
else:
344-
if index:
345-
for port_name in iface_alias_converter.port_dict.keys():
346-
regex = re.compile(r"\b{}\b".format(port_name))
347-
result = re.findall(regex, raw_output)
348-
if result:
349-
interface_name = ''.join(result)
350-
if not raw_output.startswith(" PortID:"):
351-
raw_output = raw_output.replace(
352-
interface_name,
353-
iface_alias_converter.name_to_alias(
354-
interface_name))
355-
click.echo(raw_output.rstrip('\n'))
359+
"""
360+
Default command conversion
361+
Search for port names either at the start of a line or preceded immediately by
362+
whitespace and followed immediately by either the end of a line or whitespace
363+
or a comma followed by whitespace
364+
"""
365+
converted_output = raw_output
366+
for port_name in iface_alias_converter.port_dict.keys():
367+
converted_output = re.sub(r"(^|\s){}($|,{{0,1}}\s)".format(port_name),
368+
r"\1{}\2".format(iface_alias_converter.name_to_alias(port_name)),
369+
converted_output)
370+
click.echo(converted_output.rstrip('\n'))
356371

357372
rc = process.poll()
358373
if rc != 0:

0 commit comments

Comments
 (0)