From eee99730d1ebce93c72a0610e6f650a0badbc12b Mon Sep 17 00:00:00 2001 From: a-kenji Date: Tue, 28 May 2024 11:13:55 +0200 Subject: [PATCH] clan: add `run_no_stdout` function suppressing stdout Add the `run_no_stdout` function suppressing stdout by default.This keeps the noise down on most commands, while still stayingdebuggable.Stdout will be active when the `--debug` flag is passed to the cli. Fixes #1443 --- pkgs/clan-cli/clan_cli/cmd.py | 20 ++++++++++++++++++++ pkgs/clan-cli/clan_cli/machines/list.py | 7 ++----- pkgs/clan-cli/clan_cli/machines/machines.py | 8 ++++---- 3 files changed, 26 insertions(+), 9 deletions(-) diff --git a/pkgs/clan-cli/clan_cli/cmd.py b/pkgs/clan-cli/clan_cli/cmd.py index 09f71ffb..d209981d 100644 --- a/pkgs/clan-cli/clan_cli/cmd.py +++ b/pkgs/clan-cli/clan_cli/cmd.py @@ -140,3 +140,23 @@ def run( raise ClanCmdError(cmd_out) return cmd_out + + +def run_no_stdout( + cmd: list[str], + *, + env: dict[str, str] | None = None, + cwd: Path = Path.cwd(), + log: Log = Log.STDERR, + check: bool = True, + error_msg: str | None = None, +) -> CmdOut: + """ + Like run, but automatically suppresses stdout, if not in DEBUG log level. + If in DEBUG log level the stdout of commands will be shown. + """ + if logging.getLogger(__name__.split(".")[0]).isEnabledFor(logging.DEBUG): + return run(cmd, env=env, log=log, check=check, error_msg=error_msg) + else: + log = Log.NONE + return run(cmd, env=env, log=log, check=check, error_msg=error_msg) diff --git a/pkgs/clan-cli/clan_cli/machines/list.py b/pkgs/clan-cli/clan_cli/machines/list.py index 7378a001..7964e120 100644 --- a/pkgs/clan-cli/clan_cli/machines/list.py +++ b/pkgs/clan-cli/clan_cli/machines/list.py @@ -6,7 +6,7 @@ from pathlib import Path from clan_cli.api import API -from ..cmd import Log, run +from ..cmd import run_no_stdout from ..nix import nix_config, nix_eval log = logging.getLogger(__name__) @@ -34,10 +34,7 @@ def list_machines(flake_url: str | Path, debug: bool) -> dict[str, MachineInfo]: ] ) - if not debug: - proc = run(cmd, log=Log.NONE) - else: - proc = run(cmd) + proc = run_no_stdout(cmd) res = proc.stdout.strip() machines_dict = json.loads(res) diff --git a/pkgs/clan-cli/clan_cli/machines/machines.py b/pkgs/clan-cli/clan_cli/machines/machines.py index 9c8bf265..7907b01e 100644 --- a/pkgs/clan-cli/clan_cli/machines/machines.py +++ b/pkgs/clan-cli/clan_cli/machines/machines.py @@ -10,7 +10,7 @@ from clan_cli.clan_uri import ClanURI, MachineData from clan_cli.dirs import vm_state_dir from clan_cli.qemu.qmp import QEMUMonitorProtocol -from ..cmd import run +from ..cmd import run_no_stdout from ..errors import ClanError from ..nix import nix_build, nix_config, nix_eval, nix_metadata from ..ssh import Host, parse_deployment_address @@ -197,7 +197,7 @@ class Machine: config_json.flush() file_info = json.loads( - run( + run_no_stdout( nix_eval( [ "--impure", @@ -247,10 +247,10 @@ class Machine: ] if method == "eval": - output = run(nix_eval(args)).stdout.strip() + output = run_no_stdout(nix_eval(args)).stdout.strip() return output elif method == "build": - outpath = run(nix_build(args)).stdout.strip() + outpath = run_no_stdout(nix_build(args)).stdout.strip() return Path(outpath) else: raise ValueError(f"Unknown method {method}") -- 2.45.2