Merge pull request 'clan-cli: simplify cli helper' (#1726) from DavHau/clan-core:DavHau-dave into main
This commit is contained in:
@@ -8,8 +8,7 @@ from clan_app import main
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class Cli:
|
||||
def run(self, args: list[str]) -> None:
|
||||
cmd = shlex.join(["clan", *args])
|
||||
log.debug(f"$ {cmd} \nCaller: {get_caller()}")
|
||||
main(args)
|
||||
def run(args: list[str]) -> None:
|
||||
cmd = shlex.join(["clan", *args])
|
||||
log.debug(f"$ {cmd} \nCaller: {get_caller()}")
|
||||
main(args)
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
import pytest
|
||||
from helpers.cli import Cli
|
||||
from helpers import cli
|
||||
|
||||
|
||||
def test_help(capfd: pytest.CaptureFixture) -> None:
|
||||
cli = Cli()
|
||||
with pytest.raises(SystemExit):
|
||||
cli.run(["clan-app", "--help"])
|
||||
|
||||
@@ -8,12 +8,11 @@ from clan_cli.custom_logger import get_caller
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class Cli:
|
||||
def run(self, args: list[str]) -> argparse.Namespace:
|
||||
parser = create_parser(prog="clan")
|
||||
parsed = parser.parse_args(args)
|
||||
cmd = shlex.join(["clan", *args])
|
||||
log.debug(f"$ {cmd} \nCaller: {get_caller()}")
|
||||
if hasattr(parsed, "func"):
|
||||
parsed.func(parsed)
|
||||
return parsed
|
||||
def run(args: list[str]) -> argparse.Namespace:
|
||||
parser = create_parser(prog="clan")
|
||||
parsed = parser.parse_args(args)
|
||||
cmd = shlex.join(["clan", *args])
|
||||
log.debug(f"$ {cmd} \nCaller: {get_caller()}")
|
||||
if hasattr(parsed, "func"):
|
||||
parsed.func(parsed)
|
||||
return parsed
|
||||
|
||||
@@ -1,14 +1,12 @@
|
||||
import pytest
|
||||
from fixtures_flakes import FlakeForTest
|
||||
from helpers.cli import Cli
|
||||
from helpers import cli
|
||||
|
||||
|
||||
@pytest.mark.impure
|
||||
def test_backups(
|
||||
test_flake_with_core: FlakeForTest,
|
||||
) -> None:
|
||||
cli = Cli()
|
||||
|
||||
cli.run(
|
||||
[
|
||||
"backups",
|
||||
|
||||
@@ -1,9 +1,8 @@
|
||||
import pytest
|
||||
from helpers.cli import Cli
|
||||
from helpers import cli
|
||||
|
||||
|
||||
def test_help(capsys: pytest.CaptureFixture) -> None:
|
||||
cli = Cli()
|
||||
with pytest.raises(SystemExit):
|
||||
cli.run(["--help"])
|
||||
captured = capsys.readouterr()
|
||||
|
||||
@@ -2,7 +2,7 @@ from pathlib import Path
|
||||
|
||||
import pytest
|
||||
from fixtures_flakes import FlakeForTest
|
||||
from helpers.cli import Cli
|
||||
from helpers import cli
|
||||
|
||||
from clan_cli import config
|
||||
from clan_cli.config import parsing
|
||||
@@ -13,12 +13,8 @@ example_options = f"{Path(config.__file__).parent}/jsonschema/options.json"
|
||||
|
||||
def test_configure_machine(
|
||||
test_flake: FlakeForTest,
|
||||
temporary_home: Path,
|
||||
capsys: pytest.CaptureFixture,
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
) -> None:
|
||||
cli = Cli()
|
||||
|
||||
# clear the output buffer
|
||||
capsys.readouterr()
|
||||
# read a option value
|
||||
|
||||
@@ -3,12 +3,7 @@ import subprocess
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
from helpers.cli import Cli
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def cli() -> Cli:
|
||||
return Cli()
|
||||
from helpers import cli
|
||||
|
||||
|
||||
@pytest.mark.impure
|
||||
@@ -16,7 +11,6 @@ def test_create_flake(
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
capsys: pytest.CaptureFixture,
|
||||
temporary_home: Path,
|
||||
cli: Cli,
|
||||
clan_core: Path,
|
||||
) -> None:
|
||||
flake_dir = temporary_home / "test-flake"
|
||||
@@ -56,7 +50,6 @@ def test_ui_template(
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
capsys: pytest.CaptureFixture,
|
||||
temporary_home: Path,
|
||||
cli: Cli,
|
||||
clan_core: Path,
|
||||
) -> None:
|
||||
flake_dir = temporary_home / "test-flake"
|
||||
|
||||
@@ -2,7 +2,7 @@ from typing import TYPE_CHECKING
|
||||
|
||||
import pytest
|
||||
from fixtures_flakes import FlakeForTest
|
||||
from helpers.cli import Cli
|
||||
from helpers import cli
|
||||
|
||||
if TYPE_CHECKING:
|
||||
pass
|
||||
@@ -12,7 +12,6 @@ if TYPE_CHECKING:
|
||||
def test_flakes_inspect(
|
||||
test_flake_with_core: FlakeForTest, capsys: pytest.CaptureFixture
|
||||
) -> None:
|
||||
cli = Cli()
|
||||
cli.run(
|
||||
[
|
||||
"flakes",
|
||||
|
||||
@@ -3,7 +3,7 @@ from typing import TYPE_CHECKING
|
||||
|
||||
import pytest
|
||||
from fixtures_flakes import FlakeForTest
|
||||
from helpers.cli import Cli
|
||||
from helpers import cli
|
||||
from pytest import CaptureFixture
|
||||
|
||||
from clan_cli.dirs import user_history_file
|
||||
@@ -17,7 +17,6 @@ if TYPE_CHECKING:
|
||||
def test_history_add(
|
||||
test_flake_with_core: FlakeForTest,
|
||||
) -> None:
|
||||
cli = Cli()
|
||||
cmd = [
|
||||
"history",
|
||||
"add",
|
||||
@@ -36,7 +35,6 @@ def test_history_list(
|
||||
capsys: CaptureFixture,
|
||||
test_flake_with_core: FlakeForTest,
|
||||
) -> None:
|
||||
cli = Cli()
|
||||
cmd = [
|
||||
"history",
|
||||
"list",
|
||||
|
||||
@@ -3,7 +3,7 @@ from typing import TYPE_CHECKING
|
||||
|
||||
import pytest
|
||||
from fixtures_flakes import FlakeForTest
|
||||
from helpers.cli import Cli
|
||||
from helpers import cli
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from age_keys import KeyPair
|
||||
@@ -16,8 +16,6 @@ def test_import_sops(
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
age_keys: list["KeyPair"],
|
||||
) -> None:
|
||||
cli = Cli()
|
||||
|
||||
monkeypatch.setenv("SOPS_AGE_KEY", age_keys[1].privkey)
|
||||
cli.run(
|
||||
[
|
||||
|
||||
@@ -1,13 +1,12 @@
|
||||
import pytest
|
||||
from fixtures_flakes import FlakeForTest
|
||||
from helpers.cli import Cli
|
||||
from helpers import cli
|
||||
|
||||
|
||||
@pytest.mark.impure
|
||||
def test_machine_subcommands(
|
||||
test_flake_with_core: FlakeForTest, capsys: pytest.CaptureFixture
|
||||
) -> None:
|
||||
cli = Cli()
|
||||
cli.run(
|
||||
["machines", "create", "--flake", str(test_flake_with_core.path), "machine1"]
|
||||
)
|
||||
|
||||
@@ -13,7 +13,7 @@ from clan_cli.nix import nix_eval, run_no_stdout
|
||||
if TYPE_CHECKING:
|
||||
from age_keys import KeyPair
|
||||
|
||||
from helpers.cli import Cli
|
||||
from helpers import cli
|
||||
|
||||
from clan_cli.machines.facts import machine_get_fact
|
||||
|
||||
@@ -39,7 +39,6 @@ def test_add_module_to_inventory(
|
||||
monkeypatch.chdir(test_flake_with_core.path)
|
||||
monkeypatch.setenv("SOPS_AGE_KEY", age_keys[0].privkey)
|
||||
|
||||
cli = Cli()
|
||||
cli.run(
|
||||
[
|
||||
"secrets",
|
||||
|
||||
@@ -6,7 +6,7 @@ from typing import TYPE_CHECKING
|
||||
|
||||
import pytest
|
||||
from fixtures_flakes import FlakeForTest
|
||||
from helpers.cli import Cli
|
||||
from helpers import cli
|
||||
|
||||
from clan_cli.errors import ClanError
|
||||
|
||||
@@ -22,7 +22,6 @@ def _test_identities(
|
||||
capsys: pytest.CaptureFixture,
|
||||
age_keys: list["KeyPair"],
|
||||
) -> None:
|
||||
cli = Cli()
|
||||
sops_folder = test_flake.path / "sops"
|
||||
|
||||
cli.run(
|
||||
@@ -111,7 +110,6 @@ def test_machines(
|
||||
def test_groups(
|
||||
test_flake: FlakeForTest, capsys: pytest.CaptureFixture, age_keys: list["KeyPair"]
|
||||
) -> None:
|
||||
cli = Cli()
|
||||
capsys.readouterr() # empty the buffer
|
||||
cli.run(["secrets", "groups", "list", "--flake", str(test_flake.path)])
|
||||
assert capsys.readouterr().out == ""
|
||||
@@ -249,7 +247,6 @@ def test_secrets(
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
age_keys: list["KeyPair"],
|
||||
) -> None:
|
||||
cli = Cli()
|
||||
capsys.readouterr() # empty the buffer
|
||||
cli.run(["secrets", "list", "--flake", str(test_flake.path)])
|
||||
assert capsys.readouterr().out == ""
|
||||
|
||||
@@ -3,7 +3,7 @@ from typing import TYPE_CHECKING
|
||||
|
||||
import pytest
|
||||
from fixtures_flakes import FlakeForTest
|
||||
from helpers.cli import Cli
|
||||
from helpers import cli
|
||||
from helpers.validator import is_valid_age_key, is_valid_ssh_key
|
||||
|
||||
from clan_cli.clan_uri import FlakeId
|
||||
@@ -24,7 +24,6 @@ def test_generate_secret(
|
||||
) -> None:
|
||||
monkeypatch.chdir(test_flake_with_core.path)
|
||||
monkeypatch.setenv("SOPS_AGE_KEY", age_keys[0].privkey)
|
||||
cli = Cli()
|
||||
cli.run(
|
||||
[
|
||||
"secrets",
|
||||
|
||||
@@ -3,7 +3,7 @@ from pathlib import Path
|
||||
|
||||
import pytest
|
||||
from fixtures_flakes import FlakeForTest
|
||||
from helpers.cli import Cli
|
||||
from helpers import cli
|
||||
from helpers.validator import is_valid_ssh_key
|
||||
|
||||
from clan_cli.clan_uri import FlakeId
|
||||
@@ -37,7 +37,6 @@ def test_upload_secret(
|
||||
%no-protection
|
||||
"""
|
||||
)
|
||||
cli = Cli()
|
||||
subprocess.run(
|
||||
nix_shell(
|
||||
["nixpkgs#gnupg"], ["gpg", "--batch", "--gen-key", str(gpg_key_spec)]
|
||||
|
||||
@@ -2,7 +2,7 @@ from typing import TYPE_CHECKING
|
||||
|
||||
import pytest
|
||||
from fixtures_flakes import FlakeForTest
|
||||
from helpers.cli import Cli
|
||||
from helpers import cli
|
||||
|
||||
from clan_cli.ssh import HostGroup
|
||||
|
||||
@@ -20,7 +20,6 @@ def test_secrets_upload(
|
||||
monkeypatch.chdir(test_flake_with_core.path)
|
||||
monkeypatch.setenv("SOPS_AGE_KEY", age_keys[0].privkey)
|
||||
|
||||
cli = Cli()
|
||||
cli.run(
|
||||
[
|
||||
"secrets",
|
||||
|
||||
@@ -3,7 +3,7 @@ from typing import TYPE_CHECKING
|
||||
|
||||
import pytest
|
||||
from fixtures_flakes import generate_flake
|
||||
from helpers.cli import Cli
|
||||
from helpers import cli
|
||||
from root import CLAN_CORE
|
||||
|
||||
if TYPE_CHECKING:
|
||||
@@ -41,7 +41,6 @@ def test_generate_secret(
|
||||
),
|
||||
)
|
||||
monkeypatch.chdir(flake.path)
|
||||
cli = Cli()
|
||||
cmd = ["vars", "generate", "--flake", str(flake.path), "my_machine"]
|
||||
cli.run(cmd)
|
||||
assert (
|
||||
|
||||
@@ -8,7 +8,7 @@ from typing import TYPE_CHECKING
|
||||
|
||||
import pytest
|
||||
from fixtures_flakes import FlakeForTest, generate_flake
|
||||
from helpers.cli import Cli
|
||||
from helpers import cli
|
||||
from root import CLAN_CORE
|
||||
|
||||
from clan_cli.dirs import vm_state_dir
|
||||
@@ -25,7 +25,7 @@ def run_vm_in_thread(machine_name: str) -> None:
|
||||
# runs machine and prints exceptions
|
||||
def run() -> None:
|
||||
try:
|
||||
Cli().run(["vms", "run", machine_name])
|
||||
cli.run(["vms", "run", machine_name])
|
||||
except Exception:
|
||||
# print exception details
|
||||
print(traceback.format_exc(), file=sys.stderr)
|
||||
@@ -85,7 +85,6 @@ def qga_connect(state_dir: Path) -> QgaSession:
|
||||
def test_inspect(
|
||||
test_flake_with_core: FlakeForTest, capsys: pytest.CaptureFixture
|
||||
) -> None:
|
||||
cli = Cli()
|
||||
cli.run(["vms", "inspect", "--flake", str(test_flake_with_core.path), "vm1"])
|
||||
out = capsys.readouterr() # empty the buffer
|
||||
assert "Cores" in out.out
|
||||
@@ -100,7 +99,6 @@ def test_run(
|
||||
) -> None:
|
||||
monkeypatch.chdir(test_flake_with_core.path)
|
||||
monkeypatch.setenv("SOPS_AGE_KEY", age_keys[0].privkey)
|
||||
cli = Cli()
|
||||
cli.run(
|
||||
[
|
||||
"secrets",
|
||||
|
||||
Reference in New Issue
Block a user