Files
disko/src/disko_lib/run_cmd.py
T
Felix Uhl a6944c6bea disko2: Make messages type-safe and modular
Using untyped dicts was not a safe way to pass arguments to error
message rendering. Also, having one giant match statement wouldn't scale
well. Using function pointers and kwargs, mypy can now properly check
whether all required arguments are passed to DiskoMessage().

As a bonus, it's now easy to use the "Go to definition" LSP command for
error codes, which wasn't possible before.
2024-11-03 22:35:06 +01:00

34 lines
784 B
Python

import subprocess
from disko_lib.messages.msgs import err_command_failed
from .logging import debug
from .result import DiskoError, DiskoResult, DiskoSuccess
def run(args: list[str]) -> DiskoResult[str]:
command = " ".join(args)
debug(f"Running: {command}")
result = subprocess.run(args, capture_output=True, text=True)
debug(
f"""
Ran: {command}
Exit code: {result.returncode}
Stdout: {result.stdout}
Stderr: {result.stderr}
"""
)
if result.returncode == 0:
return DiskoSuccess(result.stdout, "run command")
return DiskoError.single_message(
err_command_failed,
"run command",
command=command,
stderr=result.stderr,
exit_code=result.returncode,
)