Merge pull request 'Templates/cli: move display command into it own category' (#4222) from clan-templates into main

Reviewed-on: https://git.clan.lol/clan/clan-core/pulls/4222
This commit is contained in:
hsjobeki
2025-07-06 18:26:45 +00:00
6 changed files with 84 additions and 48 deletions

View File

@@ -154,6 +154,7 @@ nav:
- reference/cli/show.md - reference/cli/show.md
- reference/cli/ssh.md - reference/cli/ssh.md
- reference/cli/state.md - reference/cli/state.md
- reference/cli/templates.md
- reference/cli/vars.md - reference/cli/vars.md
- reference/cli/vms.md - reference/cli/vms.md
- NixOS Modules: - NixOS Modules:

View File

@@ -15,6 +15,7 @@ from . import (
clan, clan,
secrets, secrets,
select, select,
templates,
state, state,
vms, vms,
) )
@@ -195,6 +196,13 @@ For more detailed information, visit: {help_hyperlink("getting-started", "https:
clan.register_parser(parser_flake) clan.register_parser(parser_flake)
parser_templates = subparsers.add_parser(
"templates",
help="Subcommands to interact with templates",
formatter_class=argparse.RawTextHelpFormatter,
)
templates.register_parser(parser_templates)
parser_flash = subparsers.add_parser( parser_flash = subparsers.add_parser(
"flash", "flash",
help="Flashes your machine to an USB drive", help="Flashes your machine to an USB drive",

View File

@@ -4,7 +4,6 @@ import argparse
from clan_cli.clan.inspect import register_inspect_parser from clan_cli.clan.inspect import register_inspect_parser
from .create import register_create_parser from .create import register_create_parser
from .list import register_list_parser
# takes a (sub)parser and configures it # takes a (sub)parser and configures it
@@ -19,5 +18,3 @@ def register_parser(parser: argparse.ArgumentParser) -> None:
register_create_parser(create_parser) register_create_parser(create_parser)
inspect_parser = subparser.add_parser("inspect", help="Inspect a clan ") inspect_parser = subparser.add_parser("inspect", help="Inspect a clan ")
register_inspect_parser(inspect_parser) register_inspect_parser(inspect_parser)
list_parser = subparser.add_parser("list", help="List clan templates")
register_list_parser(list_parser)

View File

@@ -1,45 +0,0 @@
import argparse
import logging
from clan_lib.templates import list_templates
log = logging.getLogger(__name__)
def list_command(args: argparse.Namespace) -> None:
templates = list_templates(args.flake)
builtin_clan_templates = templates.builtins.get("clan", {})
print("Available templates")
print("├── <builtin>")
for i, (name, template) in enumerate(builtin_clan_templates.items()):
is_last_template = i == len(builtin_clan_templates.items()) - 1
if not is_last_template:
print(f"│ ├── {name}: {template.get('description', 'no description')}")
else:
print(f"│ └── {name}: {template.get('description', 'no description')}")
for i, (input_name, input_templates) in enumerate(templates.custom.items()):
custom_clan_templates = input_templates.get("clan", {})
is_last_input = i == len(templates.custom.items()) - 1
prefix = "" if not is_last_input else " "
if not is_last_input:
print(f"├── inputs.{input_name}:")
else:
print(f"└── inputs.{input_name}:")
for i, (name, template) in enumerate(custom_clan_templates.items()):
is_last_template = i == len(builtin_clan_templates.items()) - 1
if not is_last_template:
print(
f"{prefix} ├── {name}: {template.get('description', 'no description')}"
)
else:
print(
f"{prefix} └── {name}: {template.get('description', 'no description')}"
)
def register_list_parser(parser: argparse.ArgumentParser) -> None:
parser.set_defaults(func=list_command)

View File

@@ -0,0 +1,15 @@
# !/usr/bin/env python3
import argparse
from .list import register_list_parser
# takes a (sub)parser and configures it
def register_parser(parser: argparse.ArgumentParser) -> None:
subparser = parser.add_subparsers(
title="command",
description="the command to run",
help="the command to run",
required=True,
)
list_parser = subparser.add_parser("list", help="List avilable templates")
register_list_parser(list_parser)

View File

@@ -0,0 +1,60 @@
import argparse
import logging
from clan_lib.nix_models.clan import TemplateClanType
from clan_lib.templates import list_templates
log = logging.getLogger(__name__)
def list_command(args: argparse.Namespace) -> None:
templates = list_templates(args.flake)
# Display all templates
for i, (template_type, _builtin_template_set) in enumerate(
templates.builtins.items()
):
builtin_template_set: TemplateClanType | None = templates.builtins.get(
template_type, None
) # type: ignore
if not builtin_template_set:
continue
print(f"Avilable '{template_type}' templates")
print("├── <builtin>")
for i, (name, template) in enumerate(builtin_template_set.items()):
description = template.get("description", "no description")
is_last_template = i == len(builtin_template_set.items()) - 1
if not is_last_template:
print(f"│ ├── {name}: {description}")
else:
print(f"│ └── {name}: {description}")
for i, (input_name, input_templates) in enumerate(templates.custom.items()):
custom_templates: TemplateClanType | None = input_templates.get(
template_type, None
) # type: ignore
if not custom_templates:
continue
is_last_input = i == len(templates.custom.items()) - 1
prefix = "" if not is_last_input else " "
if not is_last_input:
print(f"├── inputs.{input_name}:")
else:
print(f"└── inputs.{input_name}:")
for i, (name, template) in enumerate(custom_templates.items()):
is_last_template = i == len(custom_templates.items()) - 1
if not is_last_template:
print(
f"{prefix} ├── {name}: {template.get('description', 'no description')}"
)
else:
print(
f"{prefix} └── {name}: {template.get('description', 'no description')}"
)
def register_list_parser(parser: argparse.ArgumentParser) -> None:
parser.set_defaults(func=list_command)