cmd.run: add needs_user_terminal to unbreak sudo

This commit is contained in:
DavHau
2024-10-23 17:37:31 +07:00
parent 102ca452b0
commit 28026a9b92
3 changed files with 12 additions and 4 deletions

View File

@@ -134,6 +134,7 @@ def run(
log: Log = Log.STDERR, log: Log = Log.STDERR,
check: bool = True, check: bool = True,
error_msg: str | None = None, error_msg: str | None = None,
needs_user_terminal: bool = False,
) -> CmdOut: ) -> CmdOut:
if cwd is None: if cwd is None:
cwd = Path.cwd() cwd = Path.cwd()
@@ -153,9 +154,11 @@ def run(
env=env, env=env,
stdout=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.PIPE, stderr=subprocess.PIPE,
start_new_session=True, start_new_session=not needs_user_terminal,
) as process, ) as process,
terminate_process_group(process), terminate_process_group(process)
if not needs_user_terminal
else contextlib.suppress(), # NOQA: B022
): ):
stdout_buf, stderr_buf = handle_output(process, log) stdout_buf, stderr_buf = handle_output(process, log)

View File

@@ -30,7 +30,7 @@ def pause_automounting(devices: list[Path]) -> Generator[None, None, None]:
str_devs = [str(dev) for dev in devices] str_devs = [str(dev) for dev in devices]
cmd = ["sudo", str(inhibit_path), "enable", *str_devs] cmd = ["sudo", str(inhibit_path), "enable", *str_devs]
result = run(cmd, log=Log.BOTH, check=False) result = run(cmd, log=Log.BOTH, check=False, needs_user_terminal=True)
if result.returncode != 0: if result.returncode != 0:
log.error("Failed to inhibit automounting") log.error("Failed to inhibit automounting")
yield None yield None

View File

@@ -152,4 +152,9 @@ def flash_machine(
["nixpkgs#disko"], ["nixpkgs#disko"],
disko_install, disko_install,
) )
run(cmd, log=Log.BOTH, error_msg=f"Failed to flash {machine}") run(
cmd,
log=Log.BOTH,
error_msg=f"Failed to flash {machine}",
needs_user_terminal=True,
)