clan_cli: add select command

This commit is contained in:
lassulus
2025-02-04 05:10:36 +01:00
committed by kenji
parent 6755aa2c70
commit 3811aef9b2
4 changed files with 42 additions and 1 deletions

View File

@@ -121,6 +121,7 @@ nav:
- reference/cli/flash.md - reference/cli/flash.md
- reference/cli/history.md - reference/cli/history.md
- reference/cli/machines.md - reference/cli/machines.md
- reference/cli/select.md
- reference/cli/secrets.md - reference/cli/secrets.md
- reference/cli/show.md - reference/cli/show.md
- reference/cli/ssh.md - reference/cli/ssh.md

View File

@@ -18,6 +18,7 @@ from . import (
clan, clan,
history, history,
secrets, secrets,
select,
state, state,
vms, vms,
) )
@@ -373,6 +374,29 @@ For more detailed information, visit: {help_hyperlink("deploy", "https://docs.cl
) )
history.register_parser(parser_history) history.register_parser(parser_history)
parser_select = subparsers.add_parser(
"select",
help="Select nixos values from the flake",
description="Select nixos values from the flake",
epilog=(
"""
This subcommand provides an interface nix values defined in the flake.
Examples:
$ clan select nixosConfigurations.*.config.networking.hostName
List hostnames of all nixos configurations as JSON.
$ clan select nixosConfigurations.{jon,alice}.config.clan.core.vars.generators.*.name
List all vars generators for jon and alice.
# clan select nixosConfigurations.jon.config.envirnonment.systemPackages.1
List the first system package for jon.
"""
),
)
select.register_parser(parser_select)
parser_state = subparsers.add_parser( parser_state = subparsers.add_parser(
"state", "state",
help="Query state information about machines", help="Query state information about machines",

View File

@@ -307,6 +307,5 @@ class Flake:
def select(self, selector: str) -> Any: def select(self, selector: str) -> Any:
if not self.cache.is_cached(selector): if not self.cache.is_cached(selector):
log.info(f"Cache miss for {selector}") log.info(f"Cache miss for {selector}")
print(f"Cache miss for {selector}")
self.prepare_cache([selector]) self.prepare_cache([selector])
return self.cache.select(selector) return self.cache.select(selector)

View File

@@ -0,0 +1,17 @@
import argparse
import json
from clan_cli.flake import Flake
def select_command(args: argparse.Namespace) -> None:
flake = Flake(args.flake.path)
print(json.dumps(flake.select(args.selector), indent=4))
def register_parser(parser: argparse.ArgumentParser) -> None:
parser.set_defaults(func=select_command)
parser.add_argument(
"selector",
help="select from a flake",
)