Refactor(cli/list_machines): rename to list_full_machines

This makes it clear that this should be used with care
It is potentially more expensive to create the full object, therefore it should be discouraged by its longer name
This listing is implemented based on the basic listing, where each item is turned into the bigger machine class
This commit is contained in:
Johannes Kirschbauer
2025-06-09 13:40:46 +02:00
parent 0b6bc81efe
commit 665b2095b2
7 changed files with 23 additions and 21 deletions

View File

@@ -16,7 +16,7 @@ from clan_lib.nix import (
nix_metadata, nix_metadata,
) )
from clan_cli.machines.list import list_machines from clan_cli.machines.list import list_full_machines
from clan_cli.vms.inspect import VmConfig, inspect_vm from clan_cli.vms.inspect import VmConfig, inspect_vm
@@ -58,7 +58,7 @@ def inspect_flake(flake_url: str | Path, machine_name: str) -> FlakeConfig:
system = config["system"] system = config["system"]
# Check if the machine exists # Check if the machine exists
machines: dict[str, Machine] = list_machines(Flake(str(flake_url))) machines: dict[str, Machine] = list_full_machines(Flake(str(flake_url)))
if machine_name not in machines: if machine_name not in machines:
msg = f"Machine {machine_name} not found in {flake_url}. Available machines: {', '.join(machines)}" msg = f"Machine {machine_name} not found in {flake_url}. Available machines: {', '.join(machines)}"
raise ClanError(msg) raise ClanError(msg)

View File

@@ -18,7 +18,7 @@ from clan_cli.completions import (
complete_machines, complete_machines,
complete_services_for_machine, complete_services_for_machine,
) )
from clan_cli.machines.list import list_machines from clan_cli.machines.list import list_full_machines
from .check import check_secrets from .check import check_secrets
from .public_modules import FactStoreBase from .public_modules import FactStoreBase
@@ -227,7 +227,7 @@ def generate_command(args: argparse.Namespace) -> None:
msg = "Could not find clan flake toplevel directory" msg = "Could not find clan flake toplevel directory"
raise ClanError(msg) raise ClanError(msg)
machines: list[Machine] = list(list_machines(args.flake).values()) machines: list[Machine] = list(list_full_machines(args.flake).values())
if len(args.machines) > 0: if len(args.machines) > 0:
machines = list( machines = list(
filter( filter(

View File

@@ -21,7 +21,7 @@ from clan_lib.templates import (
) )
from clan_cli.completions import add_dynamic_completer, complete_tags from clan_cli.completions import add_dynamic_completer, complete_tags
from clan_cli.machines.list import list_machines from clan_cli.machines.list import list_full_machines
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
@@ -71,7 +71,7 @@ def create_machine(
log.info(f"Found template '{template.name}' in '{template.input_variant}'") log.info(f"Found template '{template.name}' in '{template.input_variant}'")
machine_name = opts.machine.get("name") machine_name = opts.machine.get("name")
if opts.template_name in list_machines( if opts.template_name in list_full_machines(
Flake(str(clan_dir)) Flake(str(clan_dir))
) and not opts.machine.get("name"): ) and not opts.machine.get("name"):
msg = f"{opts.template_name} is already defined in {clan_dir}" msg = f"{opts.template_name} is already defined in {clan_dir}"

View File

@@ -9,10 +9,9 @@ from clan_lib.api.modules import parse_frontmatter
from clan_lib.dirs import specific_machine_dir from clan_lib.dirs import specific_machine_dir
from clan_lib.errors import ClanError from clan_lib.errors import ClanError
from clan_lib.flake import Flake from clan_lib.flake import Flake
from clan_lib.machines.actions import get_machine from clan_lib.machines.actions import get_machine, list_machines
from clan_lib.machines.machines import Machine from clan_lib.machines.machines import Machine
from clan_lib.nix_models.clan import InventoryMachine from clan_lib.nix_models.clan import InventoryMachine
from clan_lib.persist.inventory_store import InventoryStore
from clan_cli.completions import add_dynamic_completer, complete_tags from clan_cli.completions import add_dynamic_completer, complete_tags
from clan_cli.machines.hardware import HardwareConfig from clan_cli.machines.hardware import HardwareConfig
@@ -20,17 +19,20 @@ from clan_cli.machines.hardware import HardwareConfig
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
def list_machines( def list_full_machines(
flake: Flake, nix_options: list[str] | None = None flake: Flake, nix_options: list[str] | None = None
) -> dict[str, Machine]: ) -> dict[str, Machine]:
inventory_store = InventoryStore(flake=flake) """
inventory = inventory_store.read() Like `list_machines`, but returns a full 'machine' instance for each machine.
res = {} """
machines = list_machines(flake)
res: dict[str, Machine] = {}
if nix_options is None: if nix_options is None:
nix_options = [] nix_options = []
for inv_machine in inventory.get("machines", {}).values(): for inv_machine in machines.values():
name = inv_machine.get("name") name = inv_machine.get("name")
# Technically, this should not happen, but we are defensive here. # Technically, this should not happen, but we are defensive here.
if name is None: if name is None:
@@ -53,7 +55,7 @@ def query_machines_by_tags(flake: Flake, tags: list[str]) -> dict[str, Machine]:
then only machines that have those respective tags specified will be listed. then only machines that have those respective tags specified will be listed.
It is an intersection of the tags and machines. It is an intersection of the tags and machines.
""" """
machines = list_machines(flake) machines = list_full_machines(flake)
filtered_machines = {} filtered_machines = {}
for machine in machines.values(): for machine in machines.values():
@@ -113,7 +115,7 @@ def list_command(args: argparse.Namespace) -> None:
for name in query_machines_by_tags(flake, args.tags): for name in query_machines_by_tags(flake, args.tags):
print(name) print(name)
else: else:
for name in list_machines(flake): for name in list_full_machines(flake):
print(name) print(name)

View File

@@ -22,7 +22,7 @@ from clan_cli.completions import (
) )
from clan_cli.facts.generate import generate_facts from clan_cli.facts.generate import generate_facts
from clan_cli.facts.upload import upload_secrets from clan_cli.facts.upload import upload_secrets
from clan_cli.machines.list import list_machines from clan_cli.machines.list import list_full_machines
from clan_cli.vars.generate import generate_vars from clan_cli.vars.generate import generate_vars
from clan_cli.vars.upload import upload_secret_vars from clan_cli.vars.upload import upload_secret_vars
@@ -225,7 +225,7 @@ def update_command(args: argparse.Namespace) -> None:
machines: list[Machine] = [] machines: list[Machine] = []
# if no machines are passed, we will update all machines # if no machines are passed, we will update all machines
selected_machines = ( selected_machines = (
args.machines if args.machines else list_machines(args.flake).keys() args.machines if args.machines else list_full_machines(args.flake).keys()
) )
if args.target_host is not None and len(args.machines) > 1: if args.target_host is not None and len(args.machines) > 1:

View File

@@ -14,7 +14,7 @@ from clan_cli.completions import (
complete_machines, complete_machines,
complete_services_for_machine, complete_services_for_machine,
) )
from clan_cli.machines.list import list_machines from clan_cli.machines.list import list_full_machines
from clan_cli.vars._types import StoreBase from clan_cli.vars._types import StoreBase
from clan_cli.vars.migration import check_can_migrate, migrate_files from clan_cli.vars.migration import check_can_migrate, migrate_files
from clan_lib.api import API from clan_lib.api import API
@@ -511,7 +511,7 @@ def generate_command(args: argparse.Namespace) -> None:
msg = "Could not find clan flake toplevel directory" msg = "Could not find clan flake toplevel directory"
raise ClanError(msg) raise ClanError(msg)
machines: list[Machine] = list(list_machines(args.flake, args.option).values()) machines: list[Machine] = list(list_full_machines(args.flake, args.option).values())
if len(args.machines) > 0: if len(args.machines) > 0:
machines = list( machines = list(

View File

@@ -18,8 +18,8 @@ def list_machines(flake: Flake) -> dict[str, InventoryMachine]:
inventory_store = InventoryStore(flake=flake) inventory_store = InventoryStore(flake=flake)
inventory = inventory_store.read() inventory = inventory_store.read()
machine = inventory.get("machines", {}) machines = inventory.get("machines", {})
return machine return machines
@API.register @API.register