From 6b23f27e2fc48179aa040394df1f2521280f6066 Mon Sep 17 00:00:00 2001 From: jpk Date: Mon, 16 Jan 2023 14:42:00 +0100 Subject: [PATCH] Code formatting --- README.md | 3 +++ doxy/cli.py | 42 ++++++++++++++++++++++++------------------ doxy/output.py | 29 +++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+), 18 deletions(-) create mode 100644 doxy/output.py diff --git a/README.md b/README.md index 68a6353..a4da5b8 100644 --- a/README.md +++ b/README.md @@ -37,6 +37,7 @@ exit 0 Usage: doxy [OPTIONS] COMMAND [ARGS]... Options: + -f, --format [fancy|simple] output formatting [default: fancy] --help Show this message and exit. Commands: @@ -58,6 +59,8 @@ Available Services └── my-service-demo ``` +When `doxy -f simple` is used the output is easier to process by pipes. + ### Start a service and detach ```shell $ doxy control other-service up -d diff --git a/doxy/cli.py b/doxy/cli.py index efcacf7..0d74993 100644 --- a/doxy/cli.py +++ b/doxy/cli.py @@ -6,7 +6,7 @@ from rich import print from rich.rule import Rule from rich.tree import Tree -from doxy import services +from doxy import output, services from doxy.config import Config try: @@ -17,22 +17,6 @@ except FileNotFoundError as exc: sys.exit(1) -@click.group() -@click.pass_context -def main(ctx): - ctx.ensure_object(dict) - ctx.obj["CONFIG"] = CONFIG - - -@click.command(help="list available services") -def list(): - print(Rule(f"Listing services")) - tree = Tree("[bold]Available Services") - for service in services.find_services(Path(CONFIG.root_directory)): - tree.add(service) - print(tree) - - def complete_service_name(ctx, param, incomplete): return [ k @@ -41,6 +25,28 @@ def complete_service_name(ctx, param, incomplete): ] +@click.group() +@click.option( + "--format", + "-f", + type=click.Choice(["fancy", "simple"], case_sensitive=False), + default="fancy", + show_default=True, + help="output formatting", +) +@click.pass_context +def main(ctx, format): + ctx.ensure_object(dict) + ctx.obj["CONFIG"] = CONFIG + ctx.obj["FORMAT"] = format.lower() + + +@click.command(help="list available services") +@click.pass_context +def list(ctx): + output.print_services(ctx, services.find_services(Path(CONFIG.root_directory))) + + @click.command(help="edit the compose file") @click.argument("service", nargs=1, shell_complete=complete_service_name) @click.pass_context @@ -71,7 +77,7 @@ def control(ctx, service, command): services.docker_compose_command(command, compose_file) -@click.command("update", help="pull the latest service images and restart") +@click.command(help="pull the latest service images and restart") @click.argument("service", nargs=1, shell_complete=complete_service_name) @click.option( "--remove", "-r", is_flag=True, default=False, help="remove unused volumes" diff --git a/doxy/output.py b/doxy/output.py new file mode 100644 index 0000000..c073ac5 --- /dev/null +++ b/doxy/output.py @@ -0,0 +1,29 @@ +from typing import List + +from click import Context, echo +from rich import print +from rich.rule import Rule +from rich.tree import Tree + + +def _print_services_fancy(services: List[str]): + print(Rule(f"Listing services")) + tree = Tree("[bold]Available Services") + for service in services: + tree.add(service) + print(tree) + + +def _print_services_simple(services: List[str]): + for service in services: + echo(service) + + +def print_services(ctx: Context, services: List[str]): + match ctx.obj["FORMAT"]: + case "fancy": + _print_services_fancy(services) + case "simple": + _print_services_simple(services) + case _: + echo("Unknown format choice")