diff --git a/bench/cli.py b/bench/cli.py index 410b3057..041bc94f 100755 --- a/bench/cli.py +++ b/bench/cli.py @@ -30,7 +30,7 @@ from bench.utils import ( from bench.utils.bench import get_env_cmd # these variables are used to show dynamic outputs on the terminal -fancy = False +dynamic_feed = False bench.LOG_BUFFER = [] # set when commands are executed via the CLI diff --git a/bench/commands/__init__.py b/bench/commands/__init__.py index 547bf08c..35de746c 100755 --- a/bench/commands/__init__.py +++ b/bench/commands/__init__.py @@ -1,40 +1,28 @@ +# imports - third party imports import click -from click.core import _check_multicommand -def print_bench_version(ctx, param, value): - """Prints current bench version""" - if not value or ctx.resilient_parsing: - return - - import bench - click.echo(bench.VERSION) - ctx.exit() - -class MultiCommandGroup(click.Group): - def add_command(self, cmd, name=None): - """Registers another :class:`Command` with this group. If the name - is not provided, the name of the command is used. - - Note: This is a custom Group that allows passing a list of names for - the command name. - """ - name = name or cmd.name - if name is None: - raise TypeError('Command has no name.') - _check_multicommand(self, name, cmd, register=True) - - try: - self.commands[name] = cmd - except TypeError: - if isinstance(name, list): - for _name in name: - self.commands[_name] = cmd +# imports - module imports +from bench.utils.cli import ( + MultiCommandGroup, + print_bench_version, + use_experimental_feature, +) @click.group(cls=MultiCommandGroup) -@click.option('--version', is_flag=True, is_eager=True, callback=print_bench_version, expose_value=False) -def bench_command(bench_path='.'): +@click.option( + "--version", + is_flag=True, + is_eager=True, + callback=print_bench_version, + expose_value=False, +) +@click.option( + "--use-feature", is_eager=True, callback=use_experimental_feature, expose_value=False +) +def bench_command(bench_path="."): import bench + bench.set_frappe_version(bench_path=bench_path) diff --git a/bench/exceptions.py b/bench/exceptions.py index 011f0400..d4ecb119 100644 --- a/bench/exceptions.py +++ b/bench/exceptions.py @@ -23,3 +23,6 @@ class ValidationError(Exception): class CannotUpdateReleaseBench(ValidationError): pass + +class FeatureDoesNotExistError(CommandFailedError): + pass diff --git a/bench/utils/__init__.py b/bench/utils/__init__.py index 9cd363ed..81a00612 100644 --- a/bench/utils/__init__.py +++ b/bench/utils/__init__.py @@ -61,7 +61,7 @@ def log(message, level=0, no_log=False): color, prefix = levels.get(level, levels[0]) - if bench.cli.from_command_line and bench.cli.fancy: + if bench.cli.from_command_line and bench.cli.dynamic_feed: bench.LOG_BUFFER.append( {"prefix": prefix, "message": message, "color": color,} ) diff --git a/bench/utils/cli.py b/bench/utils/cli.py new file mode 100644 index 00000000..ee81d220 --- /dev/null +++ b/bench/utils/cli.py @@ -0,0 +1,42 @@ +import click +from click.core import _check_multicommand + + +def print_bench_version(ctx, param, value): + """Prints current bench version""" + if not value or ctx.resilient_parsing: + return + + import bench + click.echo(bench.VERSION) + ctx.exit() + + +class MultiCommandGroup(click.Group): + def add_command(self, cmd, name=None): + """Registers another :class:`Command` with this group. If the name + is not provided, the name of the command is used. + + Note: This is a custom Group that allows passing a list of names for + the command name. + """ + name = name or cmd.name + if name is None: + raise TypeError('Command has no name.') + _check_multicommand(self, name, cmd, register=True) + + try: + self.commands[name] = cmd + except TypeError: + if isinstance(name, list): + for _name in name: + self.commands[_name] = cmd + + +def use_experimental_feature(ctx, param, value): + if value == "dynamic-feed": + import bench.cli + bench.cli.dynamic_feed = True + else: + from bench.exceptions import FeatureDoesNotExistError + raise FeatureDoesNotExistError(f"Feature {value} does not exist") diff --git a/bench/utils/render.py b/bench/utils/render.py index 48417155..11a6e0f6 100644 --- a/bench/utils/render.py +++ b/bench/utils/render.py @@ -37,7 +37,7 @@ def step(title: str = None, success: str = None): def wrapper_fn(*args, **kwargs): import bench.cli - if bench.cli.from_command_line and bench.cli.fancy: + if bench.cli.from_command_line and bench.cli.dynamic_feed: kw = args[0].__dict__ _title = f"{click.style('⏼', fg='bright_yellow')} {title.format(**kw)}" @@ -45,7 +45,7 @@ def step(title: str = None, success: str = None): retval = fn(*args) - if bench.cli.from_command_line and bench.cli.fancy: + if bench.cli.from_command_line and bench.cli.dynamic_feed: click.clear() for l in bench.LOG_BUFFER: