Files
clan-core/pkgs/clan-cli/clan_lib/machines/actions.py
Johannes Kirschbauer ea1c1a5185 Chore(api): narrow down get_machine
Dont require the user to pass the machine into the same getter function
The workflow contradicts itself here
2025-06-04 20:38:45 +02:00

35 lines
1.1 KiB
Python

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,
)
from clan_lib.persist.inventory_store import InventoryStore
from clan_lib.persist.util import set_value_by_path
@API.register
def get_machine(flake: Flake, name: str) -> InventoryMachine:
inventory_store = InventoryStore(flake=flake)
inventory = inventory_store.read()
machine_inv = inventory.get("machines", {}).get(name)
if machine_inv is None:
msg = f"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()
set_value_by_path(inventory, f"machines.{machine.name}", update)
inventory_store.write(
inventory, message=f"Update information about machine {machine.name}"
)