diff --git a/doxy/cli.py b/doxy/cli.py index b5fb462..73e82bb 100644 --- a/doxy/cli.py +++ b/doxy/cli.py @@ -24,7 +24,7 @@ except FileNotFoundError as exc: def complete_service_name(ctx, param, incomplete): return [ k - for k in find_services(Path(CONFIG.root_directory)) + for k in find_services(Path(CONFIG.root_directory), False) if k.startswith(incomplete) ] @@ -55,9 +55,16 @@ def main(ctx, format, service_root): @main.command(help="list available services", aliases=["l", "ls"]) +@click.option( + "--sub-services", + "-s", + is_flag=True, + default=False, + help="list sub services from compose file", +) @click.pass_context -def list(ctx): - output.print_services(ctx, find_services(Path(CONFIG.root_directory))) +def list(ctx, sub_services): + output.print_services(ctx, find_services(Path(CONFIG.root_directory), sub_services)) @main.command(help="edit the compose file") diff --git a/doxy/output.py b/doxy/output.py index 8f2f499..23d8b84 100644 --- a/doxy/output.py +++ b/doxy/output.py @@ -6,17 +6,27 @@ from rich.rule import Rule from rich.tree import Tree -def _print_services_fancy(services: List[str]): +def _print_services_fancy(services: List): print(Rule("Listing services")) tree = Tree("[bold]Available Services") for service in services: - tree.add(service) + if isinstance(service, tuple): + compose, subservices = service + node = tree.add(compose) + for subservice in subservices: + node.add(subservice) + else: + tree.add(service) print(tree) def _print_services_simple(services: List[str]): for service in services: - echo(service) + if isinstance(service, tuple): + compose, subservices = service + echo(f"{compose}\t{','.join(subservices)}") + else: + echo(service) def print_services(ctx: Context, services: List[str]): diff --git a/doxy/services.py b/doxy/services.py index dd2bb22..5d045a8 100644 --- a/doxy/services.py +++ b/doxy/services.py @@ -6,6 +6,7 @@ from pathlib import Path from typing import List import click +import yaml def only_if_service_exists(fn): @@ -26,8 +27,24 @@ def only_if_service_exists(fn): return update_wrapper(wrapper, fn) -def find_services(root: Path) -> List[str]: - return [_.split("/")[0] for _ in glob.glob("*/docker-compose.y*ml", root_dir=root)] +def load_docker_compose(compose_file: Path) -> dict: + with open(compose_file) as fd: + compose_yaml = yaml.safe_load(fd.read()) + return compose_yaml + + +def find_services(root: Path, sub_services: bool) -> List: + if not sub_services: + services = [ + _.split("/")[0] for _ in glob.glob("*/docker-compose.y*ml", root_dir=root) + ] + else: + services = [] + for compose_file in glob.glob("*/docker-compose.y*ml", root_dir=root): + services.append( + (compose_file.split("/")[0], get_subservices(root / compose_file)) + ) + return services def get_compose_file(service_path: Path) -> Path: @@ -38,6 +55,11 @@ def get_compose_file(service_path: Path) -> Path: raise FileNotFoundError +def get_subservices(compose_file: Path) -> List[str]: + compose_yaml = load_docker_compose(compose_file) + return compose_yaml["services"].keys() + + def docker_compose_command(commands: List[str], compose_file: Path): ctx = click.get_current_context() config = ctx.obj["CONFIG"]