Merge pull request 'pkgs/cli: Add dynamic completer for target-host' (#2362) from kenji/clan-core:kenji-complete-deployment-address into main

This commit is contained in:
clan-bot
2024-11-10 18:37:22 +00:00
3 changed files with 55 additions and 4 deletions

View File

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

View File

@@ -10,7 +10,11 @@ from tempfile import TemporaryDirectory
from clan_cli.api import API
from clan_cli.clan_uri import FlakeId
from clan_cli.cmd import Log, run
from clan_cli.completions import add_dynamic_completer, complete_machines
from clan_cli.completions import (
add_dynamic_completer,
complete_machines,
complete_target_host,
)
from clan_cli.facts.generate import generate_facts
from clan_cli.machines.hardware import HardwareConfig
from clan_cli.machines.machines import Machine
@@ -233,10 +237,11 @@ def register_install_parser(parser: argparse.ArgumentParser) -> None:
"--json",
help="specify the json file for ssh data (generated by starting the clan installer)",
)
group.add_argument(
target_host_parser = group.add_argument(
"--target-host",
help="ssh address to install to in the form of user@host:2222",
)
add_dynamic_completer(target_host_parser, complete_target_host)
group.add_argument(
"-P",
"--png",

View File

@@ -8,7 +8,11 @@ import sys
from clan_cli.api import API
from clan_cli.clan_uri import FlakeId
from clan_cli.cmd import run
from clan_cli.completions import add_dynamic_completer, complete_machines
from clan_cli.completions import (
add_dynamic_completer,
complete_machines,
complete_target_host,
)
from clan_cli.errors import ClanError
from clan_cli.facts.generate import generate_facts
from clan_cli.facts.upload import upload_secrets
@@ -217,11 +221,13 @@ def register_update_parser(parser: argparse.ArgumentParser) -> None:
help="Host key (.ssh/known_hosts) check mode.",
)
parser.add_argument(
target_host_parser = parser.add_argument(
"--target-host",
type=str,
help="Address of the machine to update, in the format of user@host:1234.",
)
add_dynamic_completer(target_host_parser, complete_target_host)
parser.add_argument(
"--darwin",
type=str,