Clan create: migrate to inventory

This commit is contained in:
Johannes Kirschbauer
2024-07-10 15:11:45 +02:00
parent c23b44c4f3
commit 4c4f55f309
9 changed files with 79 additions and 158 deletions

View File

@@ -1,35 +1,20 @@
import json
from dataclasses import dataclass
from pathlib import Path
from clan_cli.api import API
from clan_cli.clan.create import ClanMetaInfo
from clan_cli.errors import ClanError
from clan_cli.inventory import Inventory, InventoryMeta
@dataclass
class UpdateOptions:
directory: str
meta: ClanMetaInfo | None = None
meta: InventoryMeta
@API.register
def update_clan_meta(options: UpdateOptions) -> ClanMetaInfo:
meta_file = Path(options.directory) / Path("clan/meta.json")
if not meta_file.exists():
raise ClanError(
"File not found",
description=f"Could not find {meta_file} to update.",
location="update_clan_meta",
)
def update_clan_meta(options: UpdateOptions) -> InventoryMeta:
inventory = Inventory.load_file(options.directory)
inventory.meta = options.meta
meta_content: dict[str, str] = {}
with open(meta_file) as f:
meta_content = json.load(f)
inventory.persist(options.directory, "Update clan meta")
meta_content = {**meta_content, **options.meta.__dict__}
with open(meta_file) as f:
json.dump(meta_content, f)
return ClanMetaInfo(**meta_content)
return inventory.meta