Code formatting

This commit is contained in:
jpk 2023-01-16 14:42:00 +01:00
parent 67d0f91a5b
commit 6b23f27e2f
3 changed files with 56 additions and 18 deletions

View File

@ -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

View File

@ -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"

29
doxy/output.py Normal file
View File

@ -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")