Inventory: automatically create emtpy file on write
This commit is contained in:
committed by
hsjobeki
parent
dee284d669
commit
9b706c148b
@@ -473,10 +473,18 @@ def patch(d: dict[str, Any], path: str, content: Any) -> None:
|
|||||||
|
|
||||||
@API.register
|
@API.register
|
||||||
def patch_inventory_with(base_dir: Path, section: str, content: dict[str, Any]) -> None:
|
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)
|
inventory_file = get_inventory_path(base_dir)
|
||||||
|
|
||||||
curr_inventory = {}
|
curr_inventory = {}
|
||||||
with inventory_file.open("r") as f:
|
if inventory_file.exists():
|
||||||
curr_inventory = json.load(f)
|
with inventory_file.open("r") as f:
|
||||||
|
curr_inventory = json.load(f)
|
||||||
|
|
||||||
patch(curr_inventory, section, content)
|
patch(curr_inventory, section, content)
|
||||||
|
|
||||||
|
|||||||
@@ -12,12 +12,11 @@ from clan_cli.flake import Flake
|
|||||||
from clan_cli.git import commit_file
|
from clan_cli.git import commit_file
|
||||||
from clan_cli.inventory import (
|
from clan_cli.inventory import (
|
||||||
Machine as InventoryMachine,
|
Machine as InventoryMachine,
|
||||||
patch_inventory_with,
|
|
||||||
dataclass_to_dict,
|
|
||||||
)
|
)
|
||||||
from clan_cli.inventory import (
|
from clan_cli.inventory import (
|
||||||
MachineDeploy,
|
MachineDeploy,
|
||||||
get_inventory,
|
dataclass_to_dict,
|
||||||
|
patch_inventory_with,
|
||||||
)
|
)
|
||||||
from clan_cli.machines.list import list_nixos_machines
|
from clan_cli.machines.list import list_nixos_machines
|
||||||
from clan_cli.templates import (
|
from clan_cli.templates import (
|
||||||
|
|||||||
@@ -1,4 +1,6 @@
|
|||||||
# Functions to test
|
# Functions to test
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
from clan_cli.errors import ClanError
|
from clan_cli.errors import ClanError
|
||||||
from clan_cli.inventory import (
|
from clan_cli.inventory import (
|
||||||
@@ -370,7 +372,7 @@ def test_dont_persist_defaults() -> None:
|
|||||||
"enabled": True,
|
"enabled": True,
|
||||||
"config": {"foo": "bar"},
|
"config": {"foo": "bar"},
|
||||||
}
|
}
|
||||||
data_disk = {}
|
data_disk: dict[str, Any] = {}
|
||||||
writeables = determine_writeability(prios, data_eval, data_disk)
|
writeables = determine_writeability(prios, data_eval, data_disk)
|
||||||
assert writeables == {"writeable": {"config", "enabled"}, "non_writeable": set()}
|
assert writeables == {"writeable": {"config", "enabled"}, "non_writeable": set()}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user