Inventory: automatically create emtpy file on write

This commit is contained in:
Johannes Kirschbauer
2025-02-10 12:13:54 +07:00
committed by hsjobeki
parent dee284d669
commit 9b706c148b
3 changed files with 15 additions and 6 deletions

View File

@@ -473,10 +473,18 @@ def patch(d: dict[str, Any], path: str, content: Any) -> None:
@API.register
def patch_inventory_with(base_dir: Path, section: str, content: dict[str, Any]) -> None:
"""
Pass only the section to update and the content to update with.
Make sure you pass only attributes that you would like to persist.
ATTENTION: Don't pass nix eval values unintentionally.
"""
inventory_file = get_inventory_path(base_dir)
curr_inventory = {}
with inventory_file.open("r") as f:
curr_inventory = json.load(f)
if inventory_file.exists():
with inventory_file.open("r") as f:
curr_inventory = json.load(f)
patch(curr_inventory, section, content)

View File

@@ -12,12 +12,11 @@ from clan_cli.flake import Flake
from clan_cli.git import commit_file
from clan_cli.inventory import (
Machine as InventoryMachine,
patch_inventory_with,
dataclass_to_dict,
)
from clan_cli.inventory import (
MachineDeploy,
get_inventory,
dataclass_to_dict,
patch_inventory_with,
)
from clan_cli.machines.list import list_nixos_machines
from clan_cli.templates import (

View File

@@ -1,4 +1,6 @@
# Functions to test
from typing import Any
import pytest
from clan_cli.errors import ClanError
from clan_cli.inventory import (
@@ -370,7 +372,7 @@ def test_dont_persist_defaults() -> None:
"enabled": True,
"config": {"foo": "bar"},
}
data_disk = {}
data_disk: dict[str, Any] = {}
writeables = determine_writeability(prios, data_eval, data_disk)
assert writeables == {"writeable": {"config", "enabled"}, "non_writeable": set()}