-
Notifications
You must be signed in to change notification settings - Fork 684
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[cli/feature]: split feature command into a separate file (#1034)
create new feature.py for both config and show, move the code into feature.py move common code into sonic-utiltities.common.cli.py Signed-off-by: Guohan Lu <[email protected]>
- Loading branch information
Showing
6 changed files
with
161 additions
and
139 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import click | ||
|
||
from utilities_common.cli import AbbreviationGroup, pass_db | ||
|
||
# | ||
# 'feature' group ('config feature ...') | ||
# | ||
@click.group(cls=AbbreviationGroup, name='feature', invoke_without_command=False) | ||
def feature(): | ||
"""Configure features""" | ||
pass | ||
|
||
# | ||
# 'state' command ('config feature state ...') | ||
# | ||
@feature.command('state', short_help="Enable/disable a feature") | ||
@click.argument('name', metavar='<feature-name>', required=True) | ||
@click.argument('state', metavar='<state>', required=True, type=click.Choice(["enabled", "disabled"])) | ||
@pass_db | ||
def feature_state(db, name, state): | ||
"""Enable/disable a feature""" | ||
state_data = db.cfgdb.get_entry('FEATURE', name) | ||
|
||
if not state_data: | ||
click.echo("Feature '{}' doesn't exist".format(name)) | ||
sys.exit(1) | ||
|
||
db.cfgdb.mod_entry('FEATURE', name, {'state': state}) | ||
|
||
# | ||
# 'autorestart' command ('config feature autorestart ...') | ||
# | ||
@feature.command(name='autorestart', short_help="Enable/disable autosrestart of a feature") | ||
@click.argument('name', metavar='<feature-name>', required=True) | ||
@click.argument('autorestart', metavar='<autorestart>', required=True, type=click.Choice(["enabled", "disabled"])) | ||
@pass_db | ||
def feature_autorestart(db, name, autorestart): | ||
"""Enable/disable autorestart of a feature""" | ||
feature_table = db.cfgdb.get_table('FEATURE') | ||
if not feature_table: | ||
click.echo("Unable to retrieve feature table from Config DB.") | ||
sys.exit(1) | ||
|
||
if not feature_table.has_key(name): | ||
click.echo("Unable to retrieve feature '{}'".format(name)) | ||
sys.exit(1) | ||
|
||
db.cfgdb.mod_entry('FEATURE', name, {'auto_restart': autorestart}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import click | ||
from natsort import natsorted | ||
from tabulate import tabulate | ||
|
||
from utilities_common.cli import AbbreviationGroup, pass_db | ||
|
||
# | ||
# 'feature' group (show feature ...) | ||
# | ||
@click.group(cls=AbbreviationGroup, name='feature', invoke_without_command=False) | ||
def feature(): | ||
"""Show feature status""" | ||
pass | ||
|
||
# | ||
# 'status' subcommand (show feature status) | ||
# | ||
@feature.command('status', short_help="Show feature state") | ||
@click.argument('feature_name', required=False) | ||
@pass_db | ||
def feature_status(db, feature_name): | ||
header = ['Feature', 'State', 'AutoRestart'] | ||
body = [] | ||
feature_table = db.cfgdb.get_table('FEATURE') | ||
if feature_name: | ||
if feature_table and feature_table.has_key(feature_name): | ||
body.append([feature_name, feature_table[feature_name]['state'], \ | ||
feature_table[feature_name]['auto_restart']]) | ||
else: | ||
click.echo("Can not find feature {}".format(feature_name)) | ||
sys.exit(1) | ||
else: | ||
for key in natsorted(feature_table.keys()): | ||
body.append([key, feature_table[key]['state'], feature_table[key]['auto_restart']]) | ||
click.echo(tabulate(body, header)) | ||
|
||
# | ||
# 'autorestart' subcommand (show feature autorestart) | ||
# | ||
@feature.command('autorestart', short_help="Show auto-restart state for a feature") | ||
@click.argument('feature_name', required=False) | ||
@pass_db | ||
def feature_autorestart(db, feature_name): | ||
header = ['Feature', 'AutoRestart'] | ||
body = [] | ||
feature_table = db.cfgdb.get_table('FEATURE') | ||
if feature_name: | ||
if feature_table and feature_table.has_key(feature_name): | ||
body.append([feature_name, feature_table[feature_name]['auto_restart']]) | ||
else: | ||
click.echo("Can not find feature {}".format(feature_name)) | ||
sys.exit(1) | ||
else: | ||
for name in natsorted(feature_table.keys()): | ||
body.append([name, feature_table[name]['auto_restart']]) | ||
click.echo(tabulate(body, header)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import click | ||
|
||
from utilities_common.db import Db | ||
|
||
pass_db = click.make_pass_decorator(Db, ensure=True) | ||
|
||
class AbbreviationGroup(click.Group): | ||
"""This subclass of click.Group supports abbreviated subgroup/subcommand names | ||
""" | ||
|
||
def get_command(self, ctx, cmd_name): | ||
# Try to get builtin commands as normal | ||
rv = click.Group.get_command(self, ctx, cmd_name) | ||
if rv is not None: | ||
return rv | ||
|
||
# Allow automatic abbreviation of the command. "status" for | ||
# instance will match "st". We only allow that however if | ||
# there is only one command. | ||
# If there are multiple matches and the shortest one is the common prefix of all the matches, return | ||
# the shortest one | ||
matches = [] | ||
shortest = None | ||
for x in self.list_commands(ctx): | ||
if x.lower().startswith(cmd_name.lower()): | ||
matches.append(x) | ||
if not shortest: | ||
shortest = x | ||
elif len(shortest) > len(x): | ||
shortest = x | ||
|
||
if not matches: | ||
return None | ||
elif len(matches) == 1: | ||
return click.Group.get_command(self, ctx, matches[0]) | ||
else: | ||
for x in matches: | ||
if not x.startswith(shortest): | ||
break | ||
else: | ||
return click.Group.get_command(self, ctx, shortest) | ||
|
||
ctx.fail('Too many matches: %s' % ', '.join(sorted(matches))) |