Skip to content

Commit

Permalink
[consutil] Add brief option to show line command (sonic-net#1176)
Browse files Browse the repository at this point in the history
Signed-off-by: Jing Kan jika@microsoft.com
  • Loading branch information
Blueve authored Oct 20, 2020
1 parent c395e14 commit 510d0ad
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 18 deletions.
23 changes: 12 additions & 11 deletions consutil/lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def run_command(cmd, abort=True):
return output if abort else (output, error)

# returns a list of all lines
def getAllLines():
def getAllLines(brief=False):
config_db = ConfigDBConnector()
config_db.connect()

Expand All @@ -73,16 +73,17 @@ def getAllLines():
line[LINE_KEY] = k
lines.append(line)

# Querying device directory to get all available console ports
cmd = "ls " + DEVICE_PREFIX + "*"
output, _ = run_command(cmd, abort=False)
availableTtys = output.split('\n')
availableTtys = list(filter(lambda dev: re.match(DEVICE_PREFIX + r"\d+", dev) != None, availableTtys))
for tty in availableTtys:
k = tty[len(DEVICE_PREFIX):]
if k not in keys:
line = { LINE_KEY: k }
lines.append(line)
# Querying device directory to get all available console ports
if not brief:
cmd = "ls " + DEVICE_PREFIX + "*"
output, _ = run_command(cmd, abort=False)
availableTtys = output.split('\n')
availableTtys = list(filter(lambda dev: re.match(DEVICE_PREFIX + r"\d+", dev) != None, availableTtys))
for tty in availableTtys:
k = tty[len(DEVICE_PREFIX):]
if k not in keys:
line = { LINE_KEY: k }
lines.append(line)
return lines

# returns a dictionary of busy lines and their info
Expand Down
9 changes: 5 additions & 4 deletions consutil/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ def consutil():

# 'show' subcommand
@consutil.command()
def show():
"""Show all lines and their info"""
lines = getAllLines()
@click.option('--brief', '-b', metavar='<brief_mode>', required=False, is_flag=True)
def show(brief):
"""Show all lines and their info include available ttyUSB devices unless specified brief mode"""
lines = getAllLines(brief)
busyLines = getBusyLines()

# sort lines for table rendering
Expand Down Expand Up @@ -59,7 +60,7 @@ def clear(target):
"""Clear preexisting connection to line"""
targetLine = getLine(target)
if not targetLine:
click.echo("Target [{}] does not exist".format(linenum))
click.echo("Target [{}] does not exist".format(target))
sys.exit(ERR_DEV)
lineNumber = targetLine[LINE_KEY]

Expand Down
7 changes: 4 additions & 3 deletions show/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -1827,10 +1827,11 @@ def reboot_cause():
# 'line' command ("show line")
#
@cli.command('line')
@click.option('--brief', '-b', metavar='<brief_mode>', required=False, is_flag=True)
@click.option('--verbose', is_flag=True, help="Enable verbose output")
def line(verbose):
"""Show all /dev/ttyUSB lines and their info"""
cmd = "consutil show"
def line(brief, verbose):
"""Show all console lines and their info include available ttyUSB devices unless specified brief mode"""
cmd = "consutil show" + (" -b" if brief else "")
run_command(cmd, display_cmd=verbose)
return

Expand Down

0 comments on commit 510d0ad

Please sign in to comment.