Refactor(lib/inventory): move machine actions into clan_lib/machines/actions.py

This commit is contained in:
Johannes Kirschbauer
2025-05-26 17:50:18 +02:00
parent d7b111ef0a
commit 349968c631
2 changed files with 1 additions and 1 deletions

View File

@@ -1,33 +0,0 @@
from clan_lib.api import API
from clan_lib.errors import ClanError
from clan_lib.machines.machines import Machine
from clan_lib.nix_models.inventory import (
Machine as InventoryMachine,
)
from clan_lib.persist.inventory_store import InventoryStore
from clan_lib.persist.util import apply_patch
@API.register
def get_machine(machine: Machine) -> InventoryMachine:
inventory_store = InventoryStore(flake=machine.flake)
inventory = inventory_store.read()
machine_inv = inventory.get("machines", {}).get(machine.name)
if machine_inv is None:
msg = f"Machine {machine.name} not found in inventory"
raise ClanError(msg)
return InventoryMachine(**machine_inv)
@API.register
def update_machine(machine: Machine, update: InventoryMachine) -> None:
assert machine.name == update.get("name", machine.name), "Machine name mismatch"
inventory_store = InventoryStore(flake=machine.flake)
inventory = inventory_store.read()
apply_patch(inventory, f"machines.{machine.name}", update)
inventory_store.write(
inventory, message=f"Update information about machine {machine.name}"
)

View File

@@ -9,13 +9,13 @@ from clan_lib.api.modules import parse_frontmatter
from clan_lib.dirs import specific_machine_dir
from clan_lib.errors import ClanError
from clan_lib.flake import Flake
from clan_lib.machines.actions import get_machine
from clan_lib.machines.machines import Machine
from clan_lib.nix_models.inventory import Machine as InventoryMachine
from clan_lib.persist.inventory_store import InventoryStore
from clan_cli.completions import add_dynamic_completer, complete_tags
from clan_cli.machines.hardware import HardwareConfig
from clan_cli.machines.inventory import get_machine
log = logging.getLogger(__name__)