From dc915387d99eff80a2a056b2efec52dcd232f52b Mon Sep 17 00:00:00 2001 From: a-kenji Date: Sat, 12 Jul 2025 15:11:17 +0200 Subject: [PATCH] pkgs/clan(templates): Add shell completions --- pkgs/clan-cli/clan_cli/completions.py | 20 +++++++++++++++++++ .../clan-cli/clan_cli/templates/apply_disk.py | 7 ++++++- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/pkgs/clan-cli/clan_cli/completions.py b/pkgs/clan-cli/clan_cli/completions.py index 127f8f2de..6b965e081 100644 --- a/pkgs/clan-cli/clan_cli/completions.py +++ b/pkgs/clan-cli/clan_cli/completions.py @@ -249,6 +249,26 @@ def complete_groups( return groups_dict +def complete_templates_disko( + prefix: str, parsed_args: argparse.Namespace, **kwargs: Any +) -> Iterable[str]: + """ + Provides completion functionality for disko templates + """ + + from clan_lib.templates import list_templates + + flake = clan_dir_result if (clan_dir_result := clan_dir(None)) is not None else "." + + list_all_templates = list_templates(Flake(flake)) + disko_template_list = list_all_templates.builtins.get("disko") + if disko_template_list: + disko_templates = list(disko_template_list) + disko_dict = dict.fromkeys(disko_templates, "disko") + return disko_dict + return [] + + def complete_target_host( prefix: str, parsed_args: argparse.Namespace, **kwargs: Any ) -> Iterable[str]: diff --git a/pkgs/clan-cli/clan_cli/templates/apply_disk.py b/pkgs/clan-cli/clan_cli/templates/apply_disk.py index c460ea64d..2ac993941 100644 --- a/pkgs/clan-cli/clan_cli/templates/apply_disk.py +++ b/pkgs/clan-cli/clan_cli/templates/apply_disk.py @@ -6,6 +6,8 @@ from typing import Any from clan_lib.api.disk import set_machine_disk_schema from clan_lib.machines.machines import Machine +from clan_cli.completions import add_dynamic_completer, complete_templates_disko + log = logging.getLogger(__name__) @@ -48,12 +50,15 @@ def register_apply_disk_template_parser(parser: argparse.ArgumentParser) -> None required=True, help="The machine to apply the template to", ) - parser.add_argument( + + template_action = parser.add_argument( "--template", type=str, required=True, help="The name of the disk template to apply", ) + add_dynamic_completer(template_action, complete_templates_disko) + parser.add_argument( "--set", help="Set a placeholder in the template to a value",