From ea1c1a5185b5e1765fde60458b3d01f4083a7807 Mon Sep 17 00:00:00 2001 From: Johannes Kirschbauer Date: Wed, 4 Jun 2025 20:38:45 +0200 Subject: [PATCH 1/2] Chore(api): narrow down get_machine Dont require the user to pass the machine into the same getter function The workflow contradicts itself here --- pkgs/clan-cli/clan_cli/machines/list.py | 4 ++-- pkgs/clan-cli/clan_lib/machines/actions.py | 9 +++++---- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/pkgs/clan-cli/clan_cli/machines/list.py b/pkgs/clan-cli/clan_cli/machines/list.py index 121bcdd56..7c26c1b80 100644 --- a/pkgs/clan-cli/clan_cli/machines/list.py +++ b/pkgs/clan-cli/clan_cli/machines/list.py @@ -69,7 +69,7 @@ def query_machines_by_tags(flake: Flake, tags: list[str]) -> dict[str, Machine]: filtered_machines = {} for machine in machines.values(): - inv_machine = get_machine(machine) + inv_machine = get_machine(machine.flake, machine.name) machine_tags = inv_machine.get("tags", []) if all(tag in machine_tags for tag in tags): filtered_machines[machine.name] = machine @@ -97,7 +97,7 @@ def extract_header(c: str) -> str: @API.register def get_machine_details(machine: Machine) -> MachineDetails: - machine_inv = get_machine(machine) + machine_inv = get_machine(machine.flake, machine.name) hw_config = HardwareConfig.detect_type(machine) machine_dir = specific_machine_dir(machine) diff --git a/pkgs/clan-cli/clan_lib/machines/actions.py b/pkgs/clan-cli/clan_lib/machines/actions.py index 067b05142..dc00dba7e 100644 --- a/pkgs/clan-cli/clan_lib/machines/actions.py +++ b/pkgs/clan-cli/clan_lib/machines/actions.py @@ -1,5 +1,6 @@ from clan_lib.api import API from clan_lib.errors import ClanError +from clan_lib.flake.flake import Flake from clan_lib.machines.machines import Machine from clan_lib.nix_models.clan import ( InventoryMachine, @@ -9,13 +10,13 @@ from clan_lib.persist.util import set_value_by_path @API.register -def get_machine(machine: Machine) -> InventoryMachine: - inventory_store = InventoryStore(flake=machine.flake) +def get_machine(flake: Flake, name: str) -> InventoryMachine: + inventory_store = InventoryStore(flake=flake) inventory = inventory_store.read() - machine_inv = inventory.get("machines", {}).get(machine.name) + machine_inv = inventory.get("machines", {}).get(name) if machine_inv is None: - msg = f"Machine {machine.name} not found in inventory" + msg = f"Machine {name} not found in inventory" raise ClanError(msg) return InventoryMachine(**machine_inv) From 730ab8a25ec244fac527fcee20d351809389f9f8 Mon Sep 17 00:00:00 2001 From: Johannes Kirschbauer Date: Wed, 4 Jun 2025 20:51:27 +0200 Subject: [PATCH 2/2] Chore(specific_machine_dir): use protocol to avoid direct dependency on machine class --- pkgs/clan-cli/clan_lib/dirs/__init__.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/pkgs/clan-cli/clan_lib/dirs/__init__.py b/pkgs/clan-cli/clan_lib/dirs/__init__.py index 0c3c7de89..53ee4b990 100644 --- a/pkgs/clan-cli/clan_lib/dirs/__init__.py +++ b/pkgs/clan-cli/clan_lib/dirs/__init__.py @@ -4,17 +4,24 @@ import sys import urllib.parse from enum import Enum from pathlib import Path -from typing import TYPE_CHECKING +from typing import TYPE_CHECKING, Protocol from clan_lib.errors import ClanError if TYPE_CHECKING: from clan_lib.flake import Flake - from clan_lib.machines.machines import Machine log = logging.getLogger(__name__) +class MachineSpecProtocol(Protocol): + @property + def flake(self) -> "Flake": ... + + @property + def name(self) -> str: ... + + def get_clan_flake_toplevel_or_env() -> Path | None: if clan_dir := os.environ.get("CLAN_DIR"): return Path(clan_dir) @@ -145,7 +152,7 @@ def machines_dir(flake: "Flake") -> Path: return Path(store_path) / "machines" -def specific_machine_dir(machine: "Machine") -> Path: +def specific_machine_dir(machine: "MachineSpecProtocol") -> Path: return machines_dir(machine.flake) / machine.name