From db88e631481243ab181faf26868959c5cb093320 Mon Sep 17 00:00:00 2001 From: Johannes Kirschbauer Date: Wed, 5 Jun 2024 09:45:47 +0200 Subject: [PATCH] clan-cli: create flake refactor to create clan --- pkgs/clan-cli/clan_cli/flakes/create.py | 60 +++++++++++++++++-------- 1 file changed, 42 insertions(+), 18 deletions(-) diff --git a/pkgs/clan-cli/clan_cli/flakes/create.py b/pkgs/clan-cli/clan_cli/flakes/create.py index d8aba488a..e698a5ef7 100644 --- a/pkgs/clan-cli/clan_cli/flakes/create.py +++ b/pkgs/clan-cli/clan_cli/flakes/create.py @@ -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)