mirror of
https://github.com/nix-community/disko.git
synced 2026-11-30 21:13:28 +01:00
disko2: Refactor dev mode, add disko dev eval
This commit is contained in:
1 parent
c5da562d0d
commit
fa44467f82
5 files changed
+92
-55
No files matched your search
@@ -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"
|
||||
@@ -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")
|
||||
@@ -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")
|
||||
Reference in new issue
Block a user