clan-cli: create flake refactor to create clan

This commit is contained in:
Johannes Kirschbauer 2024-06-05 09:45:47 +02:00
parent f2d2102127
commit db88e63148
Signed by: hsjobeki
SSH Key Fingerprint: SHA256:vX3utDqig7Ph5L0JPv87ZTPb/w7cMzREKVZzzLFg9qU

View File

@ -1,66 +1,90 @@
# !/usr/bin/env python3
import argparse
from dataclasses import dataclass
from pathlib import Path
from clan_cli.api import API
from ..cmd import CmdOut, run
from ..errors import ClanError
from ..nix import nix_command, nix_shell
DEFAULT_URL: str = "git+https://git.clan.lol/clan/clan-core"
DEFAULT_TEMPLATE_URL: str = "git+https://git.clan.lol/clan/clan-core"
def create_flake(directory: Path, url: str) -> dict[str, CmdOut]:
@dataclass
class CreateClanResponse:
git_init: CmdOut
git_add: CmdOut
git_config: CmdOut
flake_update: CmdOut
@API.register
def create_clan(directory: Path, template_url: str) -> CreateClanResponse:
if not directory.exists():
directory.mkdir()
else:
raise ClanError(f"Flake at '{directory}' already exists")
response = {}
raise ClanError(
location=f"{directory.resolve()}",
msg="Cannot create clan",
description="Directory already exists",
)
cmd_responses = {}
command = nix_command(
[
"flake",
"init",
"-t",
url,
template_url,
]
)
out = run(command, cwd=directory)
command = nix_shell(["nixpkgs#git"], ["git", "init"])
out = run(command, cwd=directory)
response["git init"] = out
cmd_responses["git init"] = out
command = nix_shell(["nixpkgs#git"], ["git", "add", "."])
out = run(command, cwd=directory)
response["git add"] = out
cmd_responses["git add"] = out
command = nix_shell(["nixpkgs#git"], ["git", "config", "user.name", "clan-tool"])
out = run(command, cwd=directory)
response["git config"] = out
cmd_responses["git config"] = out
command = nix_shell(
["nixpkgs#git"], ["git", "config", "user.email", "clan@example.com"]
)
out = run(command, cwd=directory)
response["git config"] = out
cmd_responses["git config"] = out
command = ["nix", "flake", "update"]
out = run(command, cwd=directory)
response["flake update"] = out
cmd_responses["flake update"] = out
response = CreateClanResponse(
git_init=cmd_responses["git init"],
git_add=cmd_responses["git add"],
git_config=cmd_responses["git config"],
flake_update=cmd_responses["flake update"],
)
return response
def create_flake_command(args: argparse.Namespace) -> None:
create_flake(args.path, args.url)
# takes a (sub)parser and configures it
def register_create_parser(parser: argparse.ArgumentParser) -> None:
parser.add_argument(
"--url",
type=str,
help="url for the flake",
default=DEFAULT_URL,
help="url to the clan template",
default=DEFAULT_TEMPLATE_URL,
)
parser.add_argument("path", type=Path, help="Path to the flake", default=Path("."))
parser.add_argument(
"path", type=Path, help="Path to the clan directory", default=Path(".")
)
def create_flake_command(args: argparse.Namespace) -> None:
create_clan(args.path, args.url)
parser.set_defaults(func=create_flake_command)