cli: default template url should come from the package itself rather than our gitea

This allow easier testing and also forks.
This commit is contained in:
Jörg Thalheim
2024-07-21 22:06:26 +02:00
parent b5f6200148
commit f04ed457db
6 changed files with 68 additions and 40 deletions

View File

@@ -9,12 +9,10 @@ from clan_cli.arg_actions import AppendOptionAction
from clan_cli.inventory import Meta, load_inventory, save_inventory
from ..cmd import CmdOut, run
from ..dirs import clan_templates
from ..errors import ClanError
from ..nix import nix_command, nix_shell
default_template_url: str = "git+https://git.clan.lol/clan/clan-core"
minimal_template_url: str = "git+https://git.clan.lol/clan/clan-core#templates.minimal"
@dataclass
class CreateClanResponse:
@@ -33,7 +31,7 @@ class CreateOptions:
# Metadata can be shown with `clan show`
meta: Meta | None = None
# URL to the template to use. Defaults to the "minimal" template
template_url: str = minimal_template_url
template: str = "minimal"
def git_command(directory: Path, *args: str) -> list[str]:
@@ -43,7 +41,7 @@ def git_command(directory: Path, *args: str) -> list[str]:
@API.register
def create_clan(options: CreateOptions) -> CreateClanResponse:
directory = Path(options.directory).resolve()
template_url = options.template_url
template_url = f"{clan_templates()}#{options.template}"
if not directory.exists():
directory.mkdir()
else:
@@ -112,10 +110,11 @@ def create_clan(options: CreateOptions) -> CreateClanResponse:
def register_create_parser(parser: argparse.ArgumentParser) -> None:
parser.add_argument(
"--url",
"--template",
type=str,
help="url to the clan template",
default=default_template_url,
choices=["default", "minimal"],
help="Clan template name",
default="default",
)
parser.add_argument(
@@ -135,7 +134,7 @@ def register_create_parser(parser: argparse.ArgumentParser) -> None:
create_clan(
CreateOptions(
directory=args.path,
template_url=args.url,
template=args.template,
)
)

View File

@@ -4,6 +4,8 @@ import sys
import urllib
from pathlib import Path
from .errors import ClanError
log = logging.getLogger(__name__)
@@ -41,6 +43,18 @@ def find_toplevel(top_level_files: list[str]) -> Path | None:
return None
def clan_templates() -> Path:
template_path = module_root().parent.parent / "templates"
if template_path.exists():
return template_path
else:
template_path = module_root() / "templates"
if not template_path.exists():
msg = f"BUG! clan core not found at {template_path}. This is an issue with packaging the cli"
raise ClanError(msg)
return template_path
def user_config_dir() -> Path:
if sys.platform == "win32":
return Path(os.getenv("APPDATA", os.path.expanduser("~\\AppData\\Roaming\\")))

View File

@@ -63,6 +63,7 @@ let
rm $out/clan_cli/config/jsonschema
ln -sf ${nixpkgs'} $out/clan_cli/nixpkgs
cp -r ${../../lib/jsonschema} $out/clan_cli/config/jsonschema
cp -r ${../../templates} $out/clan_cli/templates
${classgen}/bin/classgen ${inventory-schema}/schema.json $out/clan_cli/inventory/classes.py
'';

View File

@@ -18,7 +18,14 @@ Repository = "https://git.clan.lol/clan/clan-core"
exclude = ["clan_cli.nixpkgs*", "result"]
[tool.setuptools.package-data]
clan_cli = ["py.typed", "config/jsonschema/*", "webui/assets/**/*", "vms/mimetypes/**/*", "**/allowed-programs.json"]
clan_cli = [
"**/allowed-programs.json",
"config/jsonschema/*",
"py.typed",
"templates/**/*",
"vms/mimetypes/**/*",
"webui/assets/**/*",
]
[tool.pytest.ini_options]
testpaths = "tests"

View File

@@ -1,35 +1,26 @@
{ self, inputs, ... }:
{
flake.templates = {
new-clan = {
description = "Initialize a new clan flake";
path = ./new-clan;
};
default = self.templates.new-clan;
minimal = {
description = "for clans managed via (G)UI";
path = ./minimal;
};
};
flake.checks.x86_64-linux.template-minimal =
let
path = self.templates.minimal.path;
initialized = inputs.nixpkgs.legacyPackages.x86_64-linux.runCommand "minimal-clan-flake" { } ''
mkdir $out
cp -r ${path}/* $out
mkdir -p $out/machines/foo
echo '{ "nixpkgs": { "hostPlatform": "x86_64-linux" } }' > $out/machines/foo/settings.json
'';
evaled = (import "${initialized}/flake.nix").outputs {
self = evaled // {
outPath = initialized;
flake = (import ./flake.nix).outputs {} // {
checks.x86_64-linux.template-minimal =
let
path = self.templates.minimal.path;
initialized = inputs.nixpkgs.legacyPackages.x86_64-linux.runCommand "minimal-clan-flake" { } ''
mkdir $out
cp -r ${path}/* $out
mkdir -p $out/machines/foo
echo '{ "nixpkgs": { "hostPlatform": "x86_64-linux" } }' > $out/machines/foo/settings.json
'';
evaled = (import "${initialized}/flake.nix").outputs {
self = evaled // {
outPath = initialized;
};
clan-core = self;
};
clan-core = self;
in
{
type = "derivation";
name = "minimal-clan-flake-check";
inherit (evaled.nixosConfigurations.foo.config.system.build.vm) drvPath outPath;
};
in
{
type = "derivation";
name = "minimal-clan-flake-check";
inherit (evaled.nixosConfigurations.foo.config.system.build.vm) drvPath outPath;
};
};
}

16
templates/flake.nix Normal file
View File

@@ -0,0 +1,16 @@
{
outputs =
{ ... }:
{
templates = {
default = {
description = "Initialize a new clan flake";
path = ./new-clan;
};
minimal = {
description = "for clans managed via (G)UI";
path = ./minimal;
};
};
};
}