clan-cli: create flake refactor to create clan
This commit is contained in:
@@ -1,66 +1,90 @@
|
|||||||
# !/usr/bin/env python3
|
# !/usr/bin/env python3
|
||||||
import argparse
|
import argparse
|
||||||
|
from dataclasses import dataclass
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
|
from clan_cli.api import API
|
||||||
|
|
||||||
from ..cmd import CmdOut, run
|
from ..cmd import CmdOut, run
|
||||||
from ..errors import ClanError
|
from ..errors import ClanError
|
||||||
from ..nix import nix_command, nix_shell
|
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():
|
if not directory.exists():
|
||||||
directory.mkdir()
|
directory.mkdir()
|
||||||
else:
|
else:
|
||||||
raise ClanError(f"Flake at '{directory}' already exists")
|
raise ClanError(
|
||||||
response = {}
|
location=f"{directory.resolve()}",
|
||||||
|
msg="Cannot create clan",
|
||||||
|
description="Directory already exists",
|
||||||
|
)
|
||||||
|
|
||||||
|
cmd_responses = {}
|
||||||
command = nix_command(
|
command = nix_command(
|
||||||
[
|
[
|
||||||
"flake",
|
"flake",
|
||||||
"init",
|
"init",
|
||||||
"-t",
|
"-t",
|
||||||
url,
|
template_url,
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
out = run(command, cwd=directory)
|
out = run(command, cwd=directory)
|
||||||
|
|
||||||
command = nix_shell(["nixpkgs#git"], ["git", "init"])
|
command = nix_shell(["nixpkgs#git"], ["git", "init"])
|
||||||
out = run(command, cwd=directory)
|
out = run(command, cwd=directory)
|
||||||
response["git init"] = out
|
cmd_responses["git init"] = out
|
||||||
|
|
||||||
command = nix_shell(["nixpkgs#git"], ["git", "add", "."])
|
command = nix_shell(["nixpkgs#git"], ["git", "add", "."])
|
||||||
out = run(command, cwd=directory)
|
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"])
|
command = nix_shell(["nixpkgs#git"], ["git", "config", "user.name", "clan-tool"])
|
||||||
out = run(command, cwd=directory)
|
out = run(command, cwd=directory)
|
||||||
response["git config"] = out
|
cmd_responses["git config"] = out
|
||||||
|
|
||||||
command = nix_shell(
|
command = nix_shell(
|
||||||
["nixpkgs#git"], ["git", "config", "user.email", "clan@example.com"]
|
["nixpkgs#git"], ["git", "config", "user.email", "clan@example.com"]
|
||||||
)
|
)
|
||||||
out = run(command, cwd=directory)
|
out = run(command, cwd=directory)
|
||||||
response["git config"] = out
|
cmd_responses["git config"] = out
|
||||||
|
|
||||||
command = ["nix", "flake", "update"]
|
command = ["nix", "flake", "update"]
|
||||||
out = run(command, cwd=directory)
|
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
|
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:
|
def register_create_parser(parser: argparse.ArgumentParser) -> None:
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"--url",
|
"--url",
|
||||||
type=str,
|
type=str,
|
||||||
help="url for the flake",
|
help="url to the clan template",
|
||||||
default=DEFAULT_URL,
|
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)
|
parser.set_defaults(func=create_flake_command)
|
||||||
|
|||||||
Reference in New Issue
Block a user