summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPavel Zhukov <33721692+LeaveMyYard@users.noreply.github.com>2023-12-05 11:24:20 +0200
committerGitHub <noreply@github.com>2023-12-05 11:24:20 +0200
commit9d7aa01595ba3a4d43d62ab125b11dbc67c42be4 (patch)
treeb99ba700be337c41413dc56fc0015958d88e60d5
parent34974bf955afa502c80f4f9095398966a605b91b (diff)
parent8ee297b76d832d65150c638ea294b7797e8ba267 (diff)
Merge pull request #162 from robusta-dev/console-width-parameter
Add --width parameter for the console
-rw-r--r--robusta_krr/core/models/config.py19
-rw-r--r--robusta_krr/main.py4
-rw-r--r--robusta_krr/utils/print.py11
-rw-r--r--tests/test_krr.py14
4 files changed, 22 insertions, 26 deletions
diff --git a/robusta_krr/core/models/config.py b/robusta_krr/core/models/config.py
index 3d6aabc..5a26c49 100644
--- a/robusta_krr/core/models/config.py
+++ b/robusta_krr/core/models/config.py
@@ -53,6 +53,7 @@ class Config(pd.BaseSettings):
format: str
strategy: str
log_to_stderr: bool
+ width: Optional[int] = pd.Field(None, ge=1)
# Outputs Settings
file_output: Optional[str] = pd.Field(None)
@@ -62,11 +63,11 @@ class Config(pd.BaseSettings):
# Internal
inside_cluster: bool = False
- console: Optional[Console] = None
+ _logging_console: Optional[Console] = pd.PrivateAttr(None)
+ _result_console: Optional[Console] = pd.PrivateAttr(None)
def __init__(self, **kwargs: Any) -> None:
super().__init__(**kwargs)
- self.console = Console(stderr=self.log_to_stderr)
@property
def Formatter(self) -> formatters.FormatterFunc:
@@ -112,6 +113,18 @@ class Config(pd.BaseSettings):
def context(self) -> Optional[str]:
return self.clusters[0] if self.clusters != "*" and self.clusters else None
+ @property
+ def logging_console(self) -> Console:
+ if getattr(self, "_logging_console") is None:
+ self._logging_console = Console(file=sys.stderr if self.log_to_stderr else sys.stdout, width=self.width)
+ return self._logging_console
+
+ @property
+ def result_console(self) -> Console:
+ if getattr(self, "_result_console") is None:
+ self._result_console = Console(file=sys.stdout, width=self.width)
+ return self._result_console
+
def load_kubeconfig(self) -> None:
try:
config.load_incluster_config()
@@ -130,7 +143,7 @@ class Config(pd.BaseSettings):
level="NOTSET",
format="%(message)s",
datefmt="[%X]",
- handlers=[RichHandler(console=Console(file=sys.stderr if settings.log_to_stderr else sys.stdout))],
+ handlers=[RichHandler(console=config.logging_console)],
)
logging.getLogger("").setLevel(logging.CRITICAL)
logger.setLevel(logging.DEBUG if config.verbose else logging.CRITICAL if config.quiet else logging.INFO)
diff --git a/robusta_krr/main.py b/robusta_krr/main.py
index 9710c47..8c19f1b 100644
--- a/robusta_krr/main.py
+++ b/robusta_krr/main.py
@@ -205,6 +205,9 @@ def load_commands() -> None:
log_to_stderr: bool = typer.Option(
False, "--logtostderr", help="Pass logs to stderr", rich_help_panel="Logging Settings"
),
+ width: Optional[int] = typer.Option(
+ None, "--width", help="Width of the output. Will use console width by default.", rich_help_panel="Logging Settings"
+ ),
file_output: Optional[str] = typer.Option(
None, "--fileoutput", help="Print the output to a file", rich_help_panel="Output Settings"
),
@@ -245,6 +248,7 @@ def load_commands() -> None:
memory_min_value=memory_min_value,
quiet=quiet,
log_to_stderr=log_to_stderr,
+ width=width,
file_output=file_output,
slack_output=slack_output,
strategy=_strategy_name,
diff --git a/robusta_krr/utils/print.py b/robusta_krr/utils/print.py
index 1963c3e..093cc44 100644
--- a/robusta_krr/utils/print.py
+++ b/robusta_krr/utils/print.py
@@ -1,21 +1,14 @@
-import sys
-
-from rich import print as r_print
-
from robusta_krr.core.models.config import settings
-py_print = print
-
def print(*objects, rich: bool = True, force: bool = False) -> None:
"""
A wrapper around `rich.print` that prints only if `settings.quiet` is False.
"""
- print_func = r_print if rich else py_print
- output = sys.stdout if force or not settings.log_to_stderr else sys.stderr
+ print_func = settings.logging_console.print if rich else print
if not settings.quiet or force:
- print_func(*objects, file=output) # type: ignore
+ print_func(*objects) # type: ignore
__all__ = ["print"]
diff --git a/tests/test_krr.py b/tests/test_krr.py
index 2a2fb94..fe44176 100644
--- a/tests/test_krr.py
+++ b/tests/test_krr.py
@@ -1,7 +1,4 @@
-import json
-
import pytest
-import yaml
from typer.testing import CliRunner
from robusta_krr.main import app, load_commands
@@ -37,14 +34,3 @@ def test_output_formats(format: str, output: str):
assert result.exit_code == 0, result.exc_info
except AssertionError as e:
raise e from result.exception
-
- try:
- if format == "json":
- json_output = json.loads(result.stdout)
- assert json_output, result.stdout
- assert len(json_output["scans"]) > 0, result.stdout
-
- if format == "yaml":
- assert yaml.safe_load(result.stdout), result.stdout
- except Exception as e:
- raise Exception(result.stdout) from e