diff --git a/src/disko/cli.py b/src/disko/cli.py index 3639894..e7db6a0 100644 --- a/src/disko/cli.py +++ b/src/disko/cli.py @@ -2,17 +2,14 @@ import argparse import json -from pathlib import Path -from typing import Any, Literal, assert_never +from typing import Any, Literal -from disko_lib.ansi import disko_dev_ansi -from disko_lib.eval_config import eval_disko_file, eval_flake +from disko.mode_dev import run_dev +from disko_lib.eval_config import eval_config from disko_lib.logging import LOGGER, debug, info -from disko_lib.messages import err_missing_arguments -from disko_lib.messages.msgs import err_missing_mode, err_too_many_arguments -from disko_lib.result import DiskoError, DiskoSuccess, DiskoResult, exit_on_error +from disko_lib.messages.msgs import err_missing_mode +from disko_lib.result import DiskoError, DiskoResult, exit_on_error from disko_lib.types.disk import generate_config -from disko_lib.types.device import disko_dev_lsblk Mode = Literal[ "destroy", @@ -49,31 +46,13 @@ MODE_DESCRIPTION: dict[Mode, str] = { def run_apply( *, mode: str, disko_file: str | None, flake: str | None, **_kwargs: dict[str, Any] ) -> DiskoResult[dict[str, Any]]: - # match would be nicer, but mypy doesn't understand type narrowing in tuples - if not disko_file and not flake: - return DiskoError.single_message(err_missing_arguments, "validate args") - if not disko_file and flake: - return eval_flake(flake) - if disko_file and not flake: - return eval_disko_file(Path(disko_file)) - - return DiskoError.single_message(err_too_many_arguments, "validate args") + return eval_config(disko_file=disko_file, flake=flake) def run_generate() -> DiskoResult[dict[str, Any]]: return generate_config() -def run_dev(args: argparse.Namespace) -> DiskoResult[None]: - match args.dev_command: - case "lsblk": - return disko_dev_lsblk() - case "ansi": - return DiskoSuccess(None, disko_dev_ansi()) - case _: - assert_never(args.dev_command) - - def run( args: argparse.Namespace, ) -> DiskoResult[None | dict[str, Any]]: @@ -115,6 +94,9 @@ def parse_args() -> argparse.Namespace: mode, help=MODE_DESCRIPTION[mode], ) + return parser + + def add_common_apply_args(parser: argparse.ArgumentParser) -> None: parser.add_argument( "disko_file", nargs="?", @@ -126,10 +108,11 @@ def parse_args() -> argparse.Namespace: "-f", help="Flake to fetch the disko configuration from", ) - return parser # Commands to apply an existing configuration apply_parsers = [create_apply_parser(mode) for mode in APPLY_MODES] + for parser in apply_parsers: + add_common_apply_args(parser) # Other commands generate_parser = mode_parsers.add_parser( @@ -144,6 +127,12 @@ def parse_args() -> argparse.Namespace: ).add_subparsers(dest="dev_command") dev_parsers.add_parser("lsblk", help="List block devices the way disko sees them") dev_parsers.add_parser("ansi", help="Print defined ansi color codes") + + dev_eval_parser = dev_parsers.add_parser( + "eval", help="Evaluate a disko configuration and print the result as JSON" + ) + add_common_apply_args(dev_eval_parser) + return root_parser.parse_args() diff --git a/src/disko/mode_dev.py b/src/disko/mode_dev.py new file mode 100644 index 0000000..9b541dc --- /dev/null +++ b/src/disko/mode_dev.py @@ -0,0 +1,52 @@ +import argparse +import json +from logging import info +from typing import Any, assert_never + +from disko_lib.ansi import Colors +from disko_lib.eval_config import eval_config +from disko_lib.result import DiskoError, DiskoSuccess, DiskoResult +from disko_lib.types.device import run_lsblk + + +def run_dev_lsblk() -> DiskoResult[None]: + output = run_lsblk() + if isinstance(output, DiskoError): + return output + + print(output.value) + return DiskoSuccess(None, "run disko dev lsblk") + + +def run_dev_ansi() -> DiskoResult[None]: + import inspect + + for name, value in inspect.getmembers(Colors): + if value != "_" and not name.startswith("_") and name != "RESET": + print("{:>30} {}".format(name, value + name + Colors.RESET)) + + return DiskoSuccess(None, "run disko dev ansi") + + +def run_dev_eval( + *, disko_file: str | None, flake: str | None, **_: Any +) -> DiskoResult[None]: + result = eval_config(disko_file=disko_file, flake=flake) + + if isinstance(result, DiskoError): + return result + + print(json.dumps(result.value, indent=2)) + return DiskoSuccess(None, "run disko dev eval") + + +def run_dev(args: argparse.Namespace) -> DiskoResult[None]: + match args.dev_command: + case "lsblk": + return run_dev_lsblk() + case "ansi": + return run_dev_ansi() + case "eval": + return run_dev_eval(**vars(args)) + case _: + assert_never(args.dev_command) diff --git a/src/disko_lib/ansi.py b/src/disko_lib/ansi.py index e031f10..f39448d 100644 --- a/src/disko_lib/ansi.py +++ b/src/disko_lib/ansi.py @@ -173,13 +173,3 @@ class Colors: kernel32 = __import__("ctypes").windll.kernel32 kernel32.SetConsoleMode(kernel32.GetStdHandle(-11), 7) del kernel32 - - -def disko_dev_ansi() -> str: - import inspect - - for name, value in inspect.getmembers(Colors): - if value != "_" and not name.startswith("_") and name != "RESET": - print("{:>30} {}".format(name, value + name + Colors.RESET)) - - return "run disko dev ansi" diff --git a/src/disko_lib/eval_config.py b/src/disko_lib/eval_config.py index dbfaae7..1251f3c 100644 --- a/src/disko_lib/eval_config.py +++ b/src/disko_lib/eval_config.py @@ -7,6 +7,8 @@ from disko_lib.messages.msgs import ( err_eval_config_failed, err_file_not_found, err_flake_uri_no_attr, + err_missing_arguments, + err_too_many_arguments, ) from .run_cmd import run @@ -28,7 +30,7 @@ assert ( ), f"Can't find `eval-config.nix`, expected it next to {__file__}" -def eval_config(args: dict[str, str]) -> DiskoResult[dict[str, Any]]: +def _eval_config(args: dict[str, str]) -> DiskoResult[dict[str, Any]]: args_as_json = json.dumps(args) result = run( @@ -43,13 +45,12 @@ def eval_config(args: dict[str, str]) -> DiskoResult[dict[str, Any]]: args=args, stderr=result.messages[0].details["stderr"], ) - return result # We trust the output of `nix eval` to be valid JSON return DiskoSuccess(json.loads(result.value), "evaluate disko config") -def eval_disko_file(config_file: Path) -> DiskoResult[dict[str, Any]]: +def _eval_disko_file(config_file: Path) -> DiskoResult[dict[str, Any]]: abs_path = config_file.absolute() if not abs_path.exists(): @@ -59,10 +60,10 @@ def eval_disko_file(config_file: Path) -> DiskoResult[dict[str, Any]]: path=abs_path, ) - return eval_config({"diskoFile": str(abs_path)}) + return _eval_config({"diskoFile": str(abs_path)}) -def eval_flake(flake_uri: str) -> DiskoResult[dict[str, Any]]: +def _eval_flake(flake_uri: str) -> DiskoResult[dict[str, Any]]: # arg parser should not allow empty strings assert len(flake_uri) > 0 @@ -83,4 +84,18 @@ def eval_flake(flake_uri: str) -> DiskoResult[dict[str, Any]]: if flake_path.exists(): flake = str(flake_path.absolute()) - return eval_config({"flake": flake, "flakeAttr": flake_attr}) + return _eval_config({"flake": flake, "flakeAttr": flake_attr}) + + +def eval_config( + *, disko_file: str | None, flake: str | None +) -> DiskoResult[dict[str, Any]]: + # match would be nicer, but mypy doesn't understand type narrowing in tuples + if not disko_file and not flake: + return DiskoError.single_message(err_missing_arguments, "validate args") + if not disko_file and flake: + return _eval_flake(flake) + if disko_file and not flake: + return _eval_disko_file(Path(disko_file)) + + return DiskoError.single_message(err_too_many_arguments, "validate args") diff --git a/src/disko_lib/types/device.py b/src/disko_lib/types/device.py index db3a15e..d36b819 100644 --- a/src/disko_lib/types/device.py +++ b/src/disko_lib/types/device.py @@ -110,13 +110,13 @@ class BlockDevice: ) -def _run_lsblk() -> DiskoResult[str]: +def run_lsblk() -> DiskoResult[str]: return run(["lsblk", "--json", "--tree", "--output", ",".join(LSBLK_OUTPUT_FIELDS)]) def list_block_devices(lsblk_output: str = "") -> DiskoResult[list[BlockDevice]]: if not lsblk_output: - lsblk_result = _run_lsblk() + lsblk_result = run_lsblk() if isinstance(lsblk_result, DiskoError): return lsblk_result @@ -129,12 +129,3 @@ def list_block_devices(lsblk_output: str = "") -> DiskoResult[list[BlockDevice]] blockdevices = [BlockDevice.from_json_dict(dev) for dev in lsblk_json] return DiskoSuccess(blockdevices, "list block devices") - - -def disko_dev_lsblk() -> DiskoResult[None]: - output = _run_lsblk() - if isinstance(output, DiskoError): - return output - - print(output.value) - return DiskoSuccess(None, "run disko dev lsblk")