Clan create: migrate to inventory

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

View File

@@ -1,12 +1,12 @@
# !/usr/bin/env python3
import argparse
import json
import os
from dataclasses import dataclass, fields
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 ..cmd import CmdOut, run
from ..errors import ClanError
@@ -24,19 +24,12 @@ class CreateClanResponse:
flake_update: CmdOut
@dataclass
class ClanMetaInfo:
name: str
description: str | None
icon: str | None
@dataclass
class CreateOptions:
directory: Path | str
# Metadata for the clan
# Metadata can be shown with `clan show`
meta: ClanMetaInfo | None = None
meta: InventoryMeta | None = None
# URL to the template to use. Defaults to the "minimal" template
template_url: str = minimal_template_url
@@ -70,17 +63,18 @@ def create_clan(options: CreateOptions) -> CreateClanResponse:
)
out = run(command, cwd=directory)
# Write meta.json file if meta is provided
# Write inventory.json file
inventory = Inventory.load_file(directory)
if options.meta is not None:
meta_file = Path(directory / "clan/meta.json")
meta_file.parent.mkdir(parents=True, exist_ok=True)
with open(meta_file, "w") as f:
json.dump(options.meta.__dict__, f)
inventory.meta = options.meta
command = nix_shell(["nixpkgs#git"], ["git", "init"])
out = run(command, cwd=directory)
cmd_responses["git init"] = out
# Persist also create a commit message for each change
inventory.persist(directory, "Init inventory")
command = nix_shell(["nixpkgs#git"], ["git", "add", "."])
out = run(command, cwd=directory)
cmd_responses["git add"] = out
@@ -118,7 +112,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(ClanMetaInfo)]) }""",
help=f"""Metadata to set for the clan. Available options are: {", ".join([f.name for f in fields(InventoryMeta)]) }""",
nargs=2,
metavar=("name", "value"),
action=AppendOptionAction,