Classgen: add mapped keys and more stuff

This commit is contained in:
Johannes Kirschbauer
2024-07-18 19:18:58 +02:00
parent b752d2eb67
commit fbe27bfa0a
17 changed files with 178 additions and 321 deletions

View File

@@ -6,7 +6,7 @@ from pathlib import Path
from clan_cli.api import API
from clan_cli.arg_actions import AppendOptionAction
from clan_cli.inventory import Inventory, InventoryMeta
from clan_cli.inventory import Meta, load_inventory, save_inventory
from ..cmd import CmdOut, run
from ..errors import ClanError
@@ -29,7 +29,7 @@ class CreateOptions:
directory: Path | str
# Metadata for the clan
# Metadata can be shown with `clan show`
meta: InventoryMeta | None = None
meta: Meta | None = None
# URL to the template to use. Defaults to the "minimal" template
template_url: str = minimal_template_url
@@ -84,11 +84,11 @@ def create_clan(options: CreateOptions) -> CreateClanResponse:
## End: setup git
# Write inventory.json file
inventory = Inventory.load_file(directory)
inventory = load_inventory(directory)
if options.meta is not None:
inventory.meta = options.meta
# Persist creates a commit message for each change
inventory.persist(directory, "Init inventory")
save_inventory(inventory, directory, "Init inventory")
command = ["nix", "flake", "update"]
out = run(command, cwd=directory)
@@ -113,7 +113,7 @@ def register_create_parser(parser: argparse.ArgumentParser) -> None:
parser.add_argument(
"--meta",
help=f"""Metadata to set for the clan. Available options are: {", ".join([f.name for f in fields(InventoryMeta)]) }""",
help=f"""Metadata to set for the clan. Available options are: {", ".join([f.name for f in fields(Meta)]) }""",
nargs=2,
metavar=("name", "value"),
action=AppendOptionAction,

View File

@@ -6,7 +6,7 @@ from urllib.parse import urlparse
from clan_cli.api import API
from clan_cli.errors import ClanCmdError, ClanError
from clan_cli.inventory import InventoryMeta
from clan_cli.inventory import Meta
from ..cmd import run_no_stdout
from ..nix import nix_eval
@@ -15,7 +15,7 @@ log = logging.getLogger(__name__)
@API.register
def show_clan_meta(uri: str | Path) -> InventoryMeta:
def show_clan_meta(uri: str | Path) -> Meta:
cmd = nix_eval(
[
f"{uri}#clanInternals.inventory.meta",
@@ -61,7 +61,7 @@ def show_clan_meta(uri: str | Path) -> InventoryMeta:
description="Icon path must be a URL or a relative path.",
)
return InventoryMeta(
return Meta(
name=clan_meta.get("name"),
description=clan_meta.get("description", None),
icon=icon_path,

View File

@@ -1,20 +1,20 @@
from dataclasses import dataclass
from clan_cli.api import API
from clan_cli.inventory import Inventory, InventoryMeta
from clan_cli.inventory import Meta, load_inventory, save_inventory
@dataclass
class UpdateOptions:
directory: str
meta: InventoryMeta
meta: Meta
@API.register
def update_clan_meta(options: UpdateOptions) -> InventoryMeta:
inventory = Inventory.load_file(options.directory)
def update_clan_meta(options: UpdateOptions) -> Meta:
inventory = load_inventory(options.directory)
inventory.meta = options.meta
inventory.persist(options.directory, "Update clan meta")
save_inventory(inventory, options.directory, "Update clan metadata")
return inventory.meta