Merge pull request 'clan: add dynamic completions for fact generation services' (#1525) from a-kenji-clan/complete-services into main
This commit is contained in:
@@ -6,6 +6,9 @@ from collections.abc import Callable, Iterable
|
|||||||
from types import ModuleType
|
from types import ModuleType
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
|
from .cmd import run
|
||||||
|
from .nix import nix_eval
|
||||||
|
|
||||||
"""
|
"""
|
||||||
This module provides dynamic completions.
|
This module provides dynamic completions.
|
||||||
The completions should feel fast.
|
The completions should feel fast.
|
||||||
@@ -72,6 +75,49 @@ def complete_machines(
|
|||||||
return machines_dict
|
return machines_dict
|
||||||
|
|
||||||
|
|
||||||
|
def complete_services_for_machine(
|
||||||
|
prefix: str, parsed_args: argparse.Namespace, **kwargs: Any
|
||||||
|
) -> Iterable[str]:
|
||||||
|
"""
|
||||||
|
Provides completion functionality for machine facts generation services.
|
||||||
|
"""
|
||||||
|
services: list[str] = []
|
||||||
|
# TODO: consolidate, if multiple machines are used
|
||||||
|
machines: list[str] = parsed_args.machines
|
||||||
|
|
||||||
|
def run_cmd() -> None:
|
||||||
|
try:
|
||||||
|
if (clan_dir_result := clan_dir(None)) is not None:
|
||||||
|
flake = clan_dir_result
|
||||||
|
else:
|
||||||
|
flake = "."
|
||||||
|
services_result = json.loads(
|
||||||
|
run(
|
||||||
|
nix_eval(
|
||||||
|
flags=[
|
||||||
|
f"{flake}#nixosConfigurations.{machines[0]}.config.clanCore.facts.services",
|
||||||
|
"--apply",
|
||||||
|
"builtins.attrNames",
|
||||||
|
],
|
||||||
|
),
|
||||||
|
).stdout.strip()
|
||||||
|
)
|
||||||
|
|
||||||
|
services.extend(services_result)
|
||||||
|
except subprocess.CalledProcessError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
thread = threading.Thread(target=run_cmd)
|
||||||
|
thread.start()
|
||||||
|
thread.join(timeout=COMPLETION_TIMEOUT)
|
||||||
|
|
||||||
|
if thread.is_alive():
|
||||||
|
return iter([])
|
||||||
|
|
||||||
|
services_dict = {name: "service" for name in services}
|
||||||
|
return services_dict
|
||||||
|
|
||||||
|
|
||||||
def add_dynamic_completer(
|
def add_dynamic_completer(
|
||||||
action: argparse.Action,
|
action: argparse.Action,
|
||||||
completer: Callable[..., Iterable[str]],
|
completer: Callable[..., Iterable[str]],
|
||||||
|
|||||||
@@ -9,7 +9,11 @@ from tempfile import TemporaryDirectory
|
|||||||
|
|
||||||
from clan_cli.cmd import run
|
from clan_cli.cmd import run
|
||||||
|
|
||||||
from ..completions import add_dynamic_completer, complete_machines
|
from ..completions import (
|
||||||
|
add_dynamic_completer,
|
||||||
|
complete_machines,
|
||||||
|
complete_services_for_machine,
|
||||||
|
)
|
||||||
from ..errors import ClanError
|
from ..errors import ClanError
|
||||||
from ..git import commit_files
|
from ..git import commit_files
|
||||||
from ..machines.inventory import get_all_machines, get_selected_machines
|
from ..machines.inventory import get_all_machines, get_selected_machines
|
||||||
@@ -226,12 +230,14 @@ def register_generate_parser(parser: argparse.ArgumentParser) -> None:
|
|||||||
)
|
)
|
||||||
add_dynamic_completer(machines_parser, complete_machines)
|
add_dynamic_completer(machines_parser, complete_machines)
|
||||||
|
|
||||||
parser.add_argument(
|
service_parser = parser.add_argument(
|
||||||
"--service",
|
"--service",
|
||||||
type=str,
|
type=str,
|
||||||
help="service to generate facts for, if empty, generate facts for every service",
|
help="service to generate facts for, if empty, generate facts for every service",
|
||||||
default=None,
|
default=None,
|
||||||
)
|
)
|
||||||
|
add_dynamic_completer(service_parser, complete_services_for_machine)
|
||||||
|
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"--regenerate",
|
"--regenerate",
|
||||||
type=bool,
|
type=bool,
|
||||||
|
|||||||
Reference in New Issue
Block a user