From c1e2bc9ea978ea0a1b2bc4887b9259919e6f2244 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Thalheim?= Date: Tue, 7 May 2024 13:16:33 +0200 Subject: [PATCH] make config command read-only --- pkgs/clan-cli/clan_cli/__init__.py | 4 +- pkgs/clan-cli/clan_cli/config/__init__.py | 18 ++++---- pkgs/clan-cli/tests/test_config.py | 56 +---------------------- pkgs/clan-cli/tests/test_create_flake.py | 13 ------ 4 files changed, 13 insertions(+), 78 deletions(-) diff --git a/pkgs/clan-cli/clan_cli/__init__.py b/pkgs/clan-cli/clan_cli/__init__.py index 7ce560558..7f2b9ba3e 100644 --- a/pkgs/clan-cli/clan_cli/__init__.py +++ b/pkgs/clan-cli/clan_cli/__init__.py @@ -156,8 +156,8 @@ For more detailed information, visit: https://docs.clan.lol/getting-started parser_config = subparsers.add_parser( "config", - help="set nixos configuration", - description="set nixos configuration", + help="read a nixos configuration option", + description="read a nixos configuration option", epilog=( """ """ diff --git a/pkgs/clan-cli/clan_cli/config/__init__.py b/pkgs/clan-cli/clan_cli/config/__init__.py index b2e1585f4..de16f4047 100644 --- a/pkgs/clan-cli/clan_cli/config/__init__.py +++ b/pkgs/clan-cli/clan_cli/config/__init__.py @@ -150,6 +150,15 @@ def read_machine_option_value( return out +def get_option(args: argparse.Namespace) -> None: + print( + read_machine_option_value( + args.flake, args.machine, args.option, args.show_trace + ) + ) + + +# Currently writing is disabled def get_or_set_option(args: argparse.Namespace) -> None: if args.value == []: print( @@ -307,7 +316,7 @@ def register_parser( ) # inject callback function to process the input later - parser.set_defaults(func=get_or_set_option) + parser.set_defaults(func=get_option) parser.add_argument( "--machine", "-m", @@ -345,13 +354,6 @@ def register_parser( type=str, ) - parser.add_argument( - "value", - # force this arg to be set - nargs="*", - help="option value to set (if omitted, the current value is printed)", - ) - def main(argv: list[str] | None = None) -> None: if argv is None: diff --git a/pkgs/clan-cli/tests/test_config.py b/pkgs/clan-cli/tests/test_config.py index b99570892..985b15c16 100644 --- a/pkgs/clan-cli/tests/test_config.py +++ b/pkgs/clan-cli/tests/test_config.py @@ -1,7 +1,4 @@ -import json -import tempfile from pathlib import Path -from typing import Any import pytest from cli import Cli @@ -14,46 +11,6 @@ from clan_cli.errors import ClanError example_options = f"{Path(config.__file__).parent}/jsonschema/options.json" -# use pytest.parametrize -@pytest.mark.parametrize( - "args,expected", - [ - (["name", "DavHau"], {"name": "DavHau"}), - ( - ["kernelModules", "foo", "bar", "baz"], - {"kernelModules": ["foo", "bar", "baz"]}, - ), - (["services.opt", "test"], {"services": {"opt": "test"}}), - (["userIds.DavHau", "42"], {"userIds": {"DavHau": 42}}), - ], -) -def test_set_some_option( - args: list[str], - expected: dict[str, Any], - test_flake: FlakeForTest, -) -> None: - # create temporary file for out_file - with tempfile.NamedTemporaryFile() as out_file: - with open(out_file.name, "w") as f: - json.dump({}, f) - cli = Cli() - cli.run( - [ - "config", - "--flake", - str(test_flake.path), - "--quiet", - "--options-file", - example_options, - "--settings-file", - out_file.name, - *args, - ] - ) - json_out = json.loads(open(out_file.name).read()) - assert json_out == expected - - def test_configure_machine( test_flake: FlakeForTest, temporary_home: Path, @@ -62,17 +19,6 @@ def test_configure_machine( ) -> None: cli = Cli() - cli.run( - [ - "config", - "--flake", - str(test_flake.path), - "-m", - "machine1", - "clan.jitsi.enable", - "true", - ] - ) # clear the output buffer capsys.readouterr() # read a option value @@ -88,7 +34,7 @@ def test_configure_machine( ) # read the output - assert capsys.readouterr().out == "true\n" + assert capsys.readouterr().out == "false\n" def test_walk_jsonschema_all_types() -> None: diff --git a/pkgs/clan-cli/tests/test_create_flake.py b/pkgs/clan-cli/tests/test_create_flake.py index 89338430f..2fa7a88a8 100644 --- a/pkgs/clan-cli/tests/test_create_flake.py +++ b/pkgs/clan-cli/tests/test_create_flake.py @@ -47,16 +47,3 @@ def test_create_flake( flake_outputs["nixosConfigurations"]["machine1"] except KeyError: pytest.fail("nixosConfigurations.machine1 not found in flake outputs") - # configure machine1 - capsys.readouterr() - cli.run(["config", "--machine", "machine1", "services.openssh.enable", ""]) - capsys.readouterr() - cli.run( - [ - "config", - "--machine", - "machine1", - "services.openssh.enable", - "true", - ] - )