diff --git a/pkgs/clan-cli/clan_cli/__init__.py b/pkgs/clan-cli/clan_cli/__init__.py index 762502d30..b45d2c37e 100644 --- a/pkgs/clan-cli/clan_cli/__init__.py +++ b/pkgs/clan-cli/clan_cli/__init__.py @@ -15,6 +15,7 @@ from . import ( clan, secrets, select, + templates, state, vms, ) @@ -195,6 +196,13 @@ For more detailed information, visit: {help_hyperlink("getting-started", "https: clan.register_parser(parser_flake) + parser_templates = subparsers.add_parser( + "templates", + help="Subcommands to interact with templates", + formatter_class=argparse.RawTextHelpFormatter, + ) + templates.register_parser(parser_templates) + parser_flash = subparsers.add_parser( "flash", help="Flashes your machine to an USB drive", diff --git a/pkgs/clan-cli/clan_cli/clan/__init__.py b/pkgs/clan-cli/clan_cli/clan/__init__.py index d55091a94..41c8ae718 100644 --- a/pkgs/clan-cli/clan_cli/clan/__init__.py +++ b/pkgs/clan-cli/clan_cli/clan/__init__.py @@ -4,7 +4,6 @@ import argparse from clan_cli.clan.inspect import register_inspect_parser from .create import register_create_parser -from .list import register_list_parser # takes a (sub)parser and configures it @@ -19,5 +18,3 @@ def register_parser(parser: argparse.ArgumentParser) -> None: register_create_parser(create_parser) inspect_parser = subparser.add_parser("inspect", help="Inspect a clan ") register_inspect_parser(inspect_parser) - list_parser = subparser.add_parser("list", help="List clan templates") - register_list_parser(list_parser) diff --git a/pkgs/clan-cli/clan_cli/clan/list.py b/pkgs/clan-cli/clan_cli/clan/list.py deleted file mode 100644 index 58ae350d0..000000000 --- a/pkgs/clan-cli/clan_cli/clan/list.py +++ /dev/null @@ -1,45 +0,0 @@ -import argparse -import logging - -from clan_lib.templates import list_templates - -log = logging.getLogger(__name__) - - -def list_command(args: argparse.Namespace) -> None: - templates = list_templates(args.flake) - - builtin_clan_templates = templates.builtins.get("clan", {}) - - print("Available templates") - print("├── ") - for i, (name, template) in enumerate(builtin_clan_templates.items()): - is_last_template = i == len(builtin_clan_templates.items()) - 1 - if not is_last_template: - print(f"│ ├── {name}: {template.get('description', 'no description')}") - else: - print(f"│ └── {name}: {template.get('description', 'no description')}") - - for i, (input_name, input_templates) in enumerate(templates.custom.items()): - custom_clan_templates = input_templates.get("clan", {}) - is_last_input = i == len(templates.custom.items()) - 1 - prefix = "│" if not is_last_input else " " - if not is_last_input: - print(f"├── inputs.{input_name}:") - else: - print(f"└── inputs.{input_name}:") - - for i, (name, template) in enumerate(custom_clan_templates.items()): - is_last_template = i == len(builtin_clan_templates.items()) - 1 - if not is_last_template: - print( - f"{prefix} ├── {name}: {template.get('description', 'no description')}" - ) - else: - print( - f"{prefix} └── {name}: {template.get('description', 'no description')}" - ) - - -def register_list_parser(parser: argparse.ArgumentParser) -> None: - parser.set_defaults(func=list_command) diff --git a/pkgs/clan-cli/clan_cli/templates/__init__.py b/pkgs/clan-cli/clan_cli/templates/__init__.py new file mode 100644 index 000000000..5e052f111 --- /dev/null +++ b/pkgs/clan-cli/clan_cli/templates/__init__.py @@ -0,0 +1,15 @@ +# !/usr/bin/env python3 +import argparse +from .list import register_list_parser + + +# takes a (sub)parser and configures it +def register_parser(parser: argparse.ArgumentParser) -> None: + subparser = parser.add_subparsers( + title="command", + description="the command to run", + help="the command to run", + required=True, + ) + list_parser = subparser.add_parser("list", help="List avilable templates") + register_list_parser(list_parser) diff --git a/pkgs/clan-cli/clan_cli/templates/list.py b/pkgs/clan-cli/clan_cli/templates/list.py new file mode 100644 index 000000000..b9e26dc10 --- /dev/null +++ b/pkgs/clan-cli/clan_cli/templates/list.py @@ -0,0 +1,60 @@ +import argparse +import logging + +from clan_lib.nix_models.clan import TemplateClanType +from clan_lib.templates import list_templates + +log = logging.getLogger(__name__) + + +def list_command(args: argparse.Namespace) -> None: + templates = list_templates(args.flake) + + # Display all templates + for i, (template_type, _builtin_template_set) in enumerate( + templates.builtins.items() + ): + builtin_template_set: TemplateClanType | None = templates.builtins.get( + template_type, None + ) # type: ignore + if not builtin_template_set: + continue + + print(f"Avilable '{template_type}' templates") + print("├── ") + for i, (name, template) in enumerate(builtin_template_set.items()): + description = template.get("description", "no description") + is_last_template = i == len(builtin_template_set.items()) - 1 + if not is_last_template: + print(f"│ ├── {name}: {description}") + else: + print(f"│ └── {name}: {description}") + + for i, (input_name, input_templates) in enumerate(templates.custom.items()): + custom_templates: TemplateClanType | None = input_templates.get( + template_type, None + ) # type: ignore + if not custom_templates: + continue + + is_last_input = i == len(templates.custom.items()) - 1 + prefix = "│" if not is_last_input else " " + if not is_last_input: + print(f"├── inputs.{input_name}:") + else: + print(f"└── inputs.{input_name}:") + + for i, (name, template) in enumerate(custom_templates.items()): + is_last_template = i == len(custom_templates.items()) - 1 + if not is_last_template: + print( + f"{prefix} ├── {name}: {template.get('description', 'no description')}" + ) + else: + print( + f"{prefix} └── {name}: {template.get('description', 'no description')}" + ) + + +def register_list_parser(parser: argparse.ArgumentParser) -> None: + parser.set_defaults(func=list_command)