Skip to content

Commit

Permalink
Merge pull request #162 from robusta-dev/console-width-parameter
Browse files Browse the repository at this point in the history
Add --width parameter for the console
  • Loading branch information
LeaveMyYard authored Dec 5, 2023
2 parents 34974bf + 8ee297b commit 9d7aa01
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 26 deletions.
19 changes: 16 additions & 3 deletions robusta_krr/core/models/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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:
Expand Down Expand Up @@ -112,6 +113,18 @@ def validate_format(cls, v: str) -> str:
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()
Expand All @@ -130,7 +143,7 @@ def set_config(config: Config) -> None:
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)
Expand Down
4 changes: 4 additions & 0 deletions robusta_krr/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,9 @@ def run_strategy(
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"
),
Expand Down Expand Up @@ -245,6 +248,7 @@ def run_strategy(
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,
Expand Down
11 changes: 2 additions & 9 deletions robusta_krr/utils/print.py
Original file line number Diff line number Diff line change
@@ -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"]
14 changes: 0 additions & 14 deletions tests/test_krr.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import json

import pytest
import yaml
from typer.testing import CliRunner

from robusta_krr.main import app, load_commands
Expand Down Expand Up @@ -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

0 comments on commit 9d7aa01

Please sign in to comment.