re-enable config subcommand if CLAN_OPTIONS_FILE is set

This commit is contained in:
Jörg Thalheim
2023-08-24 17:53:33 +02:00
parent dcbf1613fb
commit 6adfadd8b9
2 changed files with 12 additions and 20 deletions

View File

@@ -1,11 +1,10 @@
import argparse
import os
import sys
from types import ModuleType
from typing import Optional
from . import admin, machines, secrets, webui
# from . import admin, config, secrets, update, webui
from . import admin, config, machines, secrets, webui
from .errors import ClanError
from .ssh import cli as ssh_cli
@@ -24,8 +23,9 @@ def create_parser(prog: Optional[str] = None) -> argparse.ArgumentParser:
admin.register_parser(parser_admin)
# DISABLED: this currently crashes if a flake does not define .#clanOptions
# parser_config = subparsers.add_parser("config", help="set nixos configuration")
# config.register_parser(parser_config)
if os.environ.get("CLAN_OPTIONS_FILE") is not None:
parser_config = subparsers.add_parser("config", help="set nixos configuration")
config.register_parser(parser_config)
parser_ssh = subparsers.add_parser("ssh", help="ssh to a remote machine")
ssh_cli.register_parser(parser_ssh)

View File

@@ -1,11 +1,10 @@
import argparse
import json
import sys
import tempfile
from pathlib import Path
from typing import Any
import pytest
from cli import Cli
from clan_cli import config
from clan_cli.config import parsing
@@ -15,7 +14,7 @@ example_options = f"{Path(config.__file__).parent}/jsonschema/options.json"
# use pytest.parametrize
@pytest.mark.parametrize(
"argv,expected",
"args,expected",
[
(["name", "DavHau"], {"name": "DavHau"}),
(
@@ -27,25 +26,18 @@ example_options = f"{Path(config.__file__).parent}/jsonschema/options.json"
],
)
def test_set_some_option(
argv: list[str],
args: list[str],
expected: dict[str, Any],
monkeypatch: pytest.MonkeyPatch,
) -> None:
# monkeypatch sys.argv
monkeypatch.setenv("CLAN_OPTIONS_FILE", example_options)
# create temporary file for out_file
with tempfile.NamedTemporaryFile() as out_file:
with open(out_file.name, "w") as f:
json.dump({}, f)
monkeypatch.setattr(
sys, "argv", ["", "--quiet", "--settings-file", out_file.name] + argv
)
parser = argparse.ArgumentParser()
config._register_parser(
parser=parser,
options=json.loads(Path(example_options).read_text()),
)
args = parser.parse_args()
args.func(args)
cli = Cli()
cli.run(["config", "--quiet", "--settings-file", out_file.name] + args)
json_out = json.loads(open(out_file.name).read())
assert json_out == expected