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 f380046050
commit 048bd37e3b
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)