Merge pull request 'chore(api/create_clan): remove unused reponse class' (#3907) from api-narrowing into main

Reviewed-on: https://git.clan.lol/clan/clan-core/pulls/3907
This commit is contained in:
hsjobeki
2025-06-09 19:00:27 +00:00
4 changed files with 27 additions and 35 deletions

View File

@@ -7,6 +7,7 @@ from clan_lib.cmd import run
from clan_lib.dirs import machine_gcroot from clan_lib.dirs import machine_gcroot
from clan_lib.errors import ClanError from clan_lib.errors import ClanError
from clan_lib.flake import Flake from clan_lib.flake import Flake
from clan_lib.machines.actions import list_machines
from clan_lib.machines.machines import Machine from clan_lib.machines.machines import Machine
from clan_lib.nix import ( from clan_lib.nix import (
nix_add_to_gcroots, nix_add_to_gcroots,
@@ -16,7 +17,6 @@ from clan_lib.nix import (
nix_metadata, nix_metadata,
) )
from clan_cli.machines.list import list_full_machines
from clan_cli.vms.inspect import VmConfig, inspect_vm from clan_cli.vms.inspect import VmConfig, inspect_vm
@@ -58,7 +58,7 @@ def inspect_flake(flake_url: str | Path, machine_name: str) -> FlakeConfig:
system = config["system"] system = config["system"]
# Check if the machine exists # Check if the machine exists
machines: dict[str, Machine] = list_full_machines(Flake(str(flake_url))) machines = list_machines(Flake(str(flake_url)))
if machine_name not in machines: if machine_name not in machines:
msg = f"Machine {machine_name} not found in {flake_url}. Available machines: {', '.join(machines)}" msg = f"Machine {machine_name} not found in {flake_url}. Available machines: {', '.join(machines)}"
raise ClanError(msg) raise ClanError(msg)

View File

@@ -3,7 +3,7 @@ from dataclasses import dataclass
from pathlib import Path from pathlib import Path
from clan_lib.api import API from clan_lib.api import API
from clan_lib.cmd import CmdOut, RunOpts, run from clan_lib.cmd import RunOpts, run
from clan_lib.errors import ClanError from clan_lib.errors import ClanError
from clan_lib.flake import Flake from clan_lib.flake import Flake
from clan_lib.nix import nix_command, nix_metadata, nix_shell from clan_lib.nix import nix_command, nix_metadata, nix_shell
@@ -18,15 +18,6 @@ from clan_lib.templates import (
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
@dataclass
class CreateClanResponse:
flake_update: CmdOut | None = None
git_init: CmdOut | None = None
git_add: CmdOut | None = None
git_config_username: CmdOut | None = None
git_config_email: CmdOut | None = None
@dataclass @dataclass
class CreateOptions: class CreateOptions:
dest: Path dest: Path
@@ -43,7 +34,7 @@ def git_command(directory: Path, *args: str) -> list[str]:
@API.register @API.register
def create_clan(opts: CreateOptions) -> CreateClanResponse: def create_clan(opts: CreateOptions) -> None:
dest = opts.dest.resolve() dest = opts.dest.resolve()
if opts.src_flake is not None: if opts.src_flake is not None:
@@ -75,36 +66,28 @@ def create_clan(opts: CreateOptions) -> CreateClanResponse:
copy_from_nixstore(src, dest) copy_from_nixstore(src, dest)
response = CreateClanResponse()
if opts.setup_git: if opts.setup_git:
response.git_init = run(git_command(dest, "init")) run(git_command(dest, "init"))
response.git_add = run(git_command(dest, "add", ".")) run(git_command(dest, "add", "."))
# check if username is set # check if username is set
has_username = run( has_username = run(
git_command(dest, "config", "user.name"), RunOpts(check=False) git_command(dest, "config", "user.name"), RunOpts(check=False)
) )
response.git_config_username = None
if has_username.returncode != 0: if has_username.returncode != 0:
response.git_config_username = run( run(git_command(dest, "config", "user.name", "clan-tool"))
git_command(dest, "config", "user.name", "clan-tool")
)
has_username = run( has_username = run(
git_command(dest, "config", "user.email"), RunOpts(check=False) git_command(dest, "config", "user.email"), RunOpts(check=False)
) )
if has_username.returncode != 0: if has_username.returncode != 0:
response.git_config_email = run( run(git_command(dest, "config", "user.email", "clan@example.com"))
git_command(dest, "config", "user.email", "clan@example.com")
)
if opts.update_clan: if opts.update_clan:
flake_update = run(nix_command(["flake", "update"]), RunOpts(cwd=dest)) run(nix_command(["flake", "update"]), RunOpts(cwd=dest))
response.flake_update = flake_update
if opts.initial: if opts.initial:
inventory_store = InventoryStore(flake=Flake(str(opts.dest))) inventory_store = InventoryStore(flake=Flake(str(opts.dest)))
inventory_store.write(opts.initial, message="Init inventory") inventory_store.write(opts.initial, message="Init inventory")
return response return

View File

@@ -568,7 +568,6 @@ class Flake:
""" """
identifier: str identifier: str
inputs_from: str | None = None
hash: str | None = None hash: str | None = None
store_path: str | None = None store_path: str | None = None
@@ -634,9 +633,6 @@ class Flake:
self.identifier, self.identifier,
] ]
if self.inputs_from:
cmd += ["--inputs-from", self.inputs_from]
flake_prefetch = run(nix_command(cmd)) flake_prefetch = run(nix_command(cmd))
flake_metadata = json.loads(flake_prefetch.stdout) flake_metadata = json.loads(flake_prefetch.stdout)
self.store_path = flake_metadata["storePath"] self.store_path = flake_metadata["storePath"]

View File

@@ -7,6 +7,9 @@ from clan_lib.cmd import run
from clan_lib.dirs import clan_templates from clan_lib.dirs import clan_templates
from clan_lib.errors import ClanCmdError, ClanError from clan_lib.errors import ClanCmdError, ClanError
from clan_lib.flake import Flake from clan_lib.flake import Flake
from clan_lib.nix import (
nix_command,
)
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
@@ -202,16 +205,26 @@ def list_templates(
return result return result
def realize_nix_path(clan_dir: Flake, nix_path: str) -> None: def realize_nix_path(flake: Flake, nix_store_path: str) -> None:
""" """
Downloads / realizes a nix path into the nix store Downloads / realizes a nix path into the nix store
""" """
if Path(nix_path).exists(): if Path(nix_store_path).exists():
return return
flake = Flake(identifier=nix_path, inputs_from=clan_dir.identifier) cmd = [
flake.invalidate_cache() "flake",
"prefetch",
"--inputs-from",
flake.identifier,
"--option",
"flake-registry",
"",
nix_store_path,
]
run(nix_command(cmd))
def get_template( def get_template(