From 8a08c00f46bc82aa65e854756e55df73f33bf825 Mon Sep 17 00:00:00 2001 From: a-kenji Date: Mon, 27 May 2024 21:52:18 +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 staying debuggable. Stdout will be active when the `--debug` flag is passed to the cli. Fixes #1443 --- pkgs/clan-cli/clan_cli/__init__.py | 5 ----- pkgs/clan-cli/clan_cli/cmd.py | 20 ++++++++++++++++++++ pkgs/clan-cli/clan_cli/config/__init__.py | 6 +++--- pkgs/clan-cli/clan_cli/config/parsing.py | 4 ++-- pkgs/clan-cli/clan_cli/flakes/create.py | 14 +++++++------- pkgs/clan-cli/clan_cli/flakes/inspect.py | 4 ++-- pkgs/clan-cli/clan_cli/flash.py | 4 ++-- pkgs/clan-cli/clan_cli/git.py | 6 +++--- pkgs/clan-cli/clan_cli/machines/inventory.py | 4 ++-- pkgs/clan-cli/clan_cli/machines/list.py | 7 ++----- pkgs/clan-cli/clan_cli/machines/machines.py | 12 ++++++------ pkgs/clan-cli/clan_cli/nix.py | 8 ++++---- 12 files changed, 53 insertions(+), 41 deletions(-) diff --git a/pkgs/clan-cli/clan_cli/__init__.py b/pkgs/clan-cli/clan_cli/__init__.py index 10b9e95e..25d956bf 100644 --- a/pkgs/clan-cli/clan_cli/__init__.py +++ b/pkgs/clan-cli/clan_cli/__init__.py @@ -126,7 +126,6 @@ For more detailed information, visit: https://docs.clan.lol/getting-started/back parser_flake = subparsers.add_parser( "flakes", help="create a clan flake inside the current directory", - description="create a clan flake inside the current directory", epilog=( """ Examples: @@ -144,7 +143,6 @@ For more detailed information, visit: https://docs.clan.lol/getting-started parser_config = subparsers.add_parser( "config", help="set nixos configuration", - description="set nixos configuration", epilog=( """ """ @@ -177,7 +175,6 @@ For more detailed information, visit: https://docs.clan.lol/getting-started/depl parser_secrets = subparsers.add_parser( "secrets", help="manage secrets", - description="manage secrets", epilog=( """ This subcommand provides an interface to secret facts. @@ -201,7 +198,6 @@ For more detailed information, visit: https://docs.clan.lol/getting-started/secr parser_facts = subparsers.add_parser( "facts", help="manage facts", - description="manage facts", epilog=( """ @@ -238,7 +234,6 @@ For more detailed information, visit: https://docs.clan.lol/getting-started/secr parser_machine = subparsers.add_parser( "machines", help="manage machines and their configuration", - description="manage machines and their configuration", epilog=( """ This subcommand provides an interface to machines managed by clan. 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/config/__init__.py b/pkgs/clan-cli/clan_cli/config/__init__.py index b2e1585f..2c98e368 100644 --- a/pkgs/clan-cli/clan_cli/config/__init__.py +++ b/pkgs/clan-cli/clan_cli/config/__init__.py @@ -8,7 +8,7 @@ import sys from pathlib import Path from typing import Any, get_origin -from clan_cli.cmd import run +from clan_cli.cmd import run_no_stdout from clan_cli.dirs import machine_settings_file from clan_cli.errors import ClanError from clan_cli.git import commit_file @@ -116,7 +116,7 @@ def options_for_machine( f"{clan_dir}#nixosConfigurations.{machine_name}.config.clanCore.optionsNix" ) cmd = nix_eval(flags=flags) - proc = run( + proc = run_no_stdout( cmd, error_msg=f"Failed to read options for machine {machine_name}", ) @@ -136,7 +136,7 @@ def read_machine_option_value( f"{clan_dir}#nixosConfigurations.{machine_name}.config.{option}", ], ) - proc = run(cmd, error_msg=f"Failed to read option {option}") + proc = run_no_stdout(cmd, error_msg=f"Failed to read option {option}") value = json.loads(proc.stdout) # print the value so that the output can be copied and fed as an input. diff --git a/pkgs/clan-cli/clan_cli/config/parsing.py b/pkgs/clan-cli/clan_cli/config/parsing.py index df896138..f5174fd7 100644 --- a/pkgs/clan-cli/clan_cli/config/parsing.py +++ b/pkgs/clan-cli/clan_cli/config/parsing.py @@ -2,7 +2,7 @@ import json from pathlib import Path from typing import Any -from ..cmd import run +from ..cmd import run_no_stdout from ..errors import ClanError from ..nix import nix_eval @@ -32,7 +32,7 @@ def schema_from_module_file( """ # run the nix expression and parse the output as json cmd = nix_eval(["--expr", nix_expr]) - proc = run(cmd) + proc = run_no_stdout(cmd) return json.loads(proc.stdout) diff --git a/pkgs/clan-cli/clan_cli/flakes/create.py b/pkgs/clan-cli/clan_cli/flakes/create.py index d8aba488..1f8b31b0 100644 --- a/pkgs/clan-cli/clan_cli/flakes/create.py +++ b/pkgs/clan-cli/clan_cli/flakes/create.py @@ -2,7 +2,7 @@ import argparse from pathlib import Path -from ..cmd import CmdOut, run +from ..cmd import CmdOut, run_no_stdout from ..errors import ClanError from ..nix import nix_command, nix_shell @@ -23,28 +23,28 @@ def create_flake(directory: Path, url: str) -> dict[str, CmdOut]: url, ] ) - out = run(command, cwd=directory) + out = run_no_stdout(command, cwd=directory) command = nix_shell(["nixpkgs#git"], ["git", "init"]) - out = run(command, cwd=directory) + out = run_no_stdout(command, cwd=directory) response["git init"] = out command = nix_shell(["nixpkgs#git"], ["git", "add", "."]) - out = run(command, cwd=directory) + out = run_no_stdout(command, cwd=directory) response["git add"] = out command = nix_shell(["nixpkgs#git"], ["git", "config", "user.name", "clan-tool"]) - out = run(command, cwd=directory) + out = run_no_stdout(command, cwd=directory) response["git config"] = out command = nix_shell( ["nixpkgs#git"], ["git", "config", "user.email", "clan@example.com"] ) - out = run(command, cwd=directory) + out = run_no_stdout(command, cwd=directory) response["git config"] = out command = ["nix", "flake", "update"] - out = run(command, cwd=directory) + out = run_no_stdout(command, cwd=directory) response["flake update"] = out return response diff --git a/pkgs/clan-cli/clan_cli/flakes/inspect.py b/pkgs/clan-cli/clan_cli/flakes/inspect.py index 906a6630..d6d785bb 100644 --- a/pkgs/clan-cli/clan_cli/flakes/inspect.py +++ b/pkgs/clan-cli/clan_cli/flakes/inspect.py @@ -2,7 +2,7 @@ import argparse from dataclasses import dataclass from pathlib import Path -from ..cmd import run +from ..cmd import run_no_stdout from ..dirs import machine_gcroot from ..errors import ClanError from ..machines.list import list_machines @@ -30,7 +30,7 @@ class FlakeConfig: def run_cmd(cmd: list[str]) -> str: - proc = run(cmd) + proc = run_no_stdout(cmd) return proc.stdout.strip() diff --git a/pkgs/clan-cli/clan_cli/flash.py b/pkgs/clan-cli/clan_cli/flash.py index 18142582..2e1e12e3 100644 --- a/pkgs/clan-cli/clan_cli/flash.py +++ b/pkgs/clan-cli/clan_cli/flash.py @@ -12,7 +12,7 @@ from pathlib import Path from tempfile import TemporaryDirectory from typing import Any -from .cmd import Log, run +from .cmd import Log, run, run_no_stdout from .errors import ClanError from .facts.secret_modules import SecretStoreBase from .machines.machines import Machine @@ -60,7 +60,7 @@ def get_keymap_and_locale() -> dict[str, str]: keymap = "en" # Execute the `localectl status` command - result = run(["localectl", "status"]) + result = run_no_stdout(["localectl", "status"]) if result.returncode == 0: output = result.stdout diff --git a/pkgs/clan-cli/clan_cli/git.py b/pkgs/clan-cli/clan_cli/git.py index b812004f..f73b4297 100644 --- a/pkgs/clan-cli/clan_cli/git.py +++ b/pkgs/clan-cli/clan_cli/git.py @@ -4,7 +4,7 @@ from pathlib import Path from clan_cli.errors import ClanError from clan_cli.nix import nix_shell -from .cmd import Log, run +from .cmd import Log, run, run_no_stdout from .locked_open import locked_open @@ -78,7 +78,7 @@ def _commit_file_to_git( ["git", "-C", str(repo_dir), "diff", "--cached", "--exit-code"] + [str(file_path) for file_path in file_paths], ) - result = run(cmd, check=False, cwd=repo_dir) + result = run_no_stdout(cmd, check=False, cwd=repo_dir) # if there is no diff, return if result.returncode == 0: return @@ -97,6 +97,6 @@ def _commit_file_to_git( + [str(file_path) for file_path in file_paths], ) - run( + run_no_stdout( cmd, error_msg=f"Failed to commit {file_paths} to git repository {repo_dir}" ) diff --git a/pkgs/clan-cli/clan_cli/machines/inventory.py b/pkgs/clan-cli/clan_cli/machines/inventory.py index 5f900b75..39c93a11 100644 --- a/pkgs/clan-cli/clan_cli/machines/inventory.py +++ b/pkgs/clan-cli/clan_cli/machines/inventory.py @@ -1,7 +1,7 @@ import json from pathlib import Path -from ..cmd import run +from ..cmd import run_no_stdout from ..nix import nix_build, nix_config from .machines import Machine @@ -10,7 +10,7 @@ from .machines import Machine def get_all_machines(flake_dir: Path) -> list[Machine]: config = nix_config() system = config["system"] - json_path = run( + json_path = run_no_stdout( nix_build([f'{flake_dir}#clanInternals.all-machines-json."{system}"']) ).stdout 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..7765056b 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,15 +197,15 @@ class Machine: config_json.flush() file_info = json.loads( - run( + run_no_stdout( nix_eval( [ "--impure", "--expr", f'let x = (builtins.fetchTree {{ type = "file"; url = "file://{config_json.name}"; }}); in {{ narHash = x.narHash; path = x.outPath; }}', ] - ) - ).stdout.strip() + ), + ).stdout.strip(), ) args = [] @@ -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}") diff --git a/pkgs/clan-cli/clan_cli/nix.py b/pkgs/clan-cli/clan_cli/nix.py index a3ab814c..a8262a24 100644 --- a/pkgs/clan-cli/clan_cli/nix.py +++ b/pkgs/clan-cli/clan_cli/nix.py @@ -4,7 +4,7 @@ import tempfile from pathlib import Path from typing import Any -from .cmd import run +from .cmd import run_no_stdout from .dirs import nixpkgs_flake, nixpkgs_source @@ -55,12 +55,12 @@ def nix_build(flags: list[str], gcroot: Path | None = None) -> list[str]: def nix_add_to_gcroots(nix_path: Path, dest: Path) -> None: cmd = ["nix-store", "--realise", f"{nix_path}", "--add-root", f"{dest}"] - run(cmd) + run_no_stdout(cmd) def nix_config() -> dict[str, Any]: cmd = nix_command(["show-config", "--json"]) - proc = run(cmd) + proc = run_no_stdout(cmd) data = json.loads(proc.stdout) config = {} for key, value in data.items(): @@ -95,7 +95,7 @@ def nix_eval(flags: list[str]) -> list[str]: def nix_metadata(flake_url: str | Path) -> dict[str, Any]: cmd = nix_command(["flake", "metadata", "--json", f"{flake_url}"]) - proc = run(cmd) + proc = run_no_stdout(cmd) data = json.loads(proc.stdout) return data