pkgs/clan: Add clan validation to vars

Add clan validation to vars and facts subcommmands
This commit is contained in:
a-kenji
2025-07-15 14:01:41 +02:00
parent 143fbb929f
commit 8178c41c7b
14 changed files with 114 additions and 9 deletions

View File

@@ -1,6 +1,7 @@
import argparse
import logging
from clan_lib.flake import require_flake
from clan_lib.machines.machines import Machine
from clan_cli.completions import add_dynamic_completer, complete_machines
@@ -37,9 +38,10 @@ def check_secrets(machine: Machine, service: None | str = None) -> bool:
def check_command(args: argparse.Namespace) -> None:
flake = require_flake(args.flake)
machine = Machine(
name=args.machine,
flake=args.flake,
flake=flake,
)
check_secrets(machine, service=args.service)

View File

@@ -0,0 +1,15 @@
from pathlib import Path
import pytest
from clan_lib.errors import ClanError
from clan_cli.tests.helpers import cli
def test_check_command_no_flake(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
monkeypatch.chdir(tmp_path)
with pytest.raises(ClanError):
cli.run(["facts", "check", "machine1"])

View File

@@ -2,6 +2,7 @@ import argparse
import json
import logging
from clan_lib.flake import require_flake
from clan_lib.machines.machines import Machine
from clan_cli.completions import add_dynamic_completer, complete_machines
@@ -10,7 +11,8 @@ log = logging.getLogger(__name__)
def get_command(args: argparse.Namespace) -> None:
machine = Machine(name=args.machine, flake=args.flake)
flake = require_flake(args.flake)
machine = Machine(name=args.machine, flake=flake)
# the raw_facts are bytestrings making them not json serializable
raw_facts = machine.public_facts_store.get_all()

View File

@@ -0,0 +1,13 @@
from pathlib import Path
import pytest
from clan_lib.errors import ClanError
from clan_cli.tests.helpers import cli
def test_list_command_no_flake(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.chdir(tmp_path)
with pytest.raises(ClanError):
cli.run(["facts", "list", "machine1"])

View File

@@ -3,6 +3,7 @@ import logging
from pathlib import Path
from tempfile import TemporaryDirectory
from clan_lib.flake import require_flake
from clan_lib.machines.machines import Machine
from clan_lib.ssh.remote import Remote
@@ -25,7 +26,8 @@ def upload_secrets(machine: Machine, host: Remote) -> None:
def upload_command(args: argparse.Namespace) -> None:
machine = Machine(name=args.machine, flake=args.flake)
flake = require_flake(args.flake)
machine = Machine(name=args.machine, flake=flake)
with machine.target_host().ssh_control_master() as host:
upload_secrets(machine, host)

View File

@@ -0,0 +1,15 @@
from pathlib import Path
import pytest
from clan_lib.errors import ClanError
from clan_cli.tests.helpers import cli
def test_upload_command_no_flake(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
monkeypatch.chdir(tmp_path)
with pytest.raises(ClanError):
cli.run(["facts", "upload", "machine1"])

View File

@@ -3,6 +3,7 @@ import logging
from clan_cli.completions import add_dynamic_completer, complete_machines
from clan_lib.errors import ClanError
from clan_lib.flake import require_flake
from clan_lib.machines.machines import Machine
log = logging.getLogger(__name__)
@@ -29,9 +30,10 @@ def fix_vars(machine: Machine, generator_name: None | str = None) -> None:
def fix_command(args: argparse.Namespace) -> None:
flake = require_flake(args.flake)
machine = Machine(
name=args.machine,
flake=args.flake,
flake=flake,
)
fix_vars(machine, generator_name=args.generator)

View File

@@ -0,0 +1,12 @@
from pathlib import Path
import pytest
from clan_cli.tests.helpers import cli
from clan_lib.errors import ClanError
def test_fix_command_no_flake(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.chdir(tmp_path)
with pytest.raises(ClanError):
cli.run(["vars", "fix", "machine1"])

View File

@@ -4,7 +4,7 @@ import sys
from clan_cli.completions import add_dynamic_completer, complete_machines
from clan_lib.errors import ClanError
from clan_lib.flake import Flake
from clan_lib.flake import Flake, require_flake
from .generate import Var
from .list import get_machine_vars
@@ -52,10 +52,11 @@ def get_command(machine_name: str, var_id: str, flake: Flake) -> None:
def _get_command(
args: argparse.Namespace,
) -> None:
flake = require_flake(args.flake)
get_command(
machine_name=args.machine,
var_id=args.var_id,
flake=args.flake,
flake=flake,
)

View File

@@ -0,0 +1,12 @@
from pathlib import Path
import pytest
from clan_cli.tests.helpers import cli
from clan_lib.errors import ClanError
def test_get_command_no_flake(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.chdir(tmp_path)
with pytest.raises(ClanError):
cli.run(["vars", "get", "machine1", "var1"])

View File

@@ -2,7 +2,7 @@ import argparse
import logging
from clan_cli.completions import add_dynamic_completer, complete_machines
from clan_lib.flake import Flake
from clan_lib.flake import Flake, require_flake
from clan_lib.machines.machines import Machine
from .generate import Var
@@ -37,7 +37,8 @@ def stringify_all_vars(machine: Machine) -> str:
def list_command(args: argparse.Namespace) -> None:
machine = Machine(name=args.machine, flake=args.flake)
flake = require_flake(args.flake)
machine = Machine(name=args.machine, flake=flake)
print(stringify_all_vars(machine))

View File

@@ -0,0 +1,12 @@
from pathlib import Path
import pytest
from clan_cli.tests.helpers import cli
from clan_lib.errors import ClanError
def test_list_command_no_flake(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.chdir(tmp_path)
with pytest.raises(ClanError):
cli.run(["vars", "list", "machine1"])

View File

@@ -3,6 +3,7 @@ import logging
from pathlib import Path
from clan_cli.completions import add_dynamic_completer, complete_machines
from clan_lib.flake import require_flake
from clan_lib.machines.machines import Machine
from clan_lib.ssh.remote import Remote
@@ -22,7 +23,8 @@ def populate_secret_vars(machine: Machine, directory: Path) -> None:
def upload_command(args: argparse.Namespace) -> None:
machine = Machine(name=args.machine, flake=args.flake)
flake = require_flake(args.flake)
machine = Machine(name=args.machine, flake=flake)
directory = None
if args.directory:
directory = Path(args.directory)

View File

@@ -0,0 +1,14 @@
from pathlib import Path
import pytest
from clan_cli.tests.helpers import cli
from clan_lib.errors import ClanError
def test_upload_command_no_flake(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
monkeypatch.chdir(tmp_path)
with pytest.raises(ClanError):
cli.run(["vars", "upload", "machine1"])