From 3ca6c4afacef01397835b611b489c27949f883f4 Mon Sep 17 00:00:00 2001 From: a-kenji Date: Sun, 10 Nov 2024 19:12:01 +0100 Subject: [PATCH] pkgs/cli: Add dynamic completer for `target-host` --- pkgs/clan-cli/clan_cli/completions.py | 40 +++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/pkgs/clan-cli/clan_cli/completions.py b/pkgs/clan-cli/clan_cli/completions.py index c5658f1e2..8d9a7d8da 100644 --- a/pkgs/clan-cli/clan_cli/completions.py +++ b/pkgs/clan-cli/clan_cli/completions.py @@ -260,6 +260,46 @@ def complete_groups( return groups_dict +def complete_target_host( + prefix: str, parsed_args: argparse.Namespace, **kwargs: Any +) -> Iterable[str]: + """ + Provides completion functionality for target_host for a specific machine + """ + target_hosts: list[str] = [] + machine: str = parsed_args.machine + + def run_cmd() -> None: + try: + if (clan_dir_result := clan_dir(None)) is not None: + flake = clan_dir_result + else: + flake = "." + target_host_result = json.loads( + run( + nix_eval( + flags=[ + f"{flake}#nixosConfigurations.{machine}.config.clan.core.networking.targetHost", + ], + ), + ).stdout.strip() + ) + + target_hosts.append(target_host_result) + except subprocess.CalledProcessError: + pass + + thread = threading.Thread(target=run_cmd) + thread.start() + thread.join(timeout=COMPLETION_TIMEOUT) + + if thread.is_alive(): + return iter([]) + + providers_dict = {name: "target_host" for name in target_hosts} + return providers_dict + + def add_dynamic_completer( action: argparse.Action, completer: Callable[..., Iterable[str]],