Skip to content

Commit

Permalink
Fix AttributeError in log-errors check (#85)
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex-Burmak authored Dec 1, 2023
1 parent 47fef0a commit 37f2c1b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
14 changes: 14 additions & 0 deletions ch_tools/common/cli/parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,20 @@ def convert(self, value, param, ctx):
return _preprocess_value(value)


class RegexpParamType(click.ParamType):
"""
Command-line parameter type for regular expressions.
"""

name = "regexp"

def convert(self, value, param, ctx):
try:
return re.compile(value)
except re.error:
raise click.BadParameter("Value should be a valid regular expression.")


class TimeSpanParamType(click.ParamType):
"""
Command-line parameter type for times span values.
Expand Down
10 changes: 2 additions & 8 deletions ch_tools/monrun_checks/ch_log_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import click
from file_read_backwards import FileReadBackwards

from ch_tools.common.cli.parameters import RegexpParamType
from ch_tools.common.result import Result

REGEXP = re.compile(
Expand All @@ -12,13 +13,6 @@
DEFAULT_EXCLUDE = r"e\.displayText\(\) = No message received"


def validate_exclude(ctx, param, value): # pylint: disable=unused-argument
try:
return re.compile(value if value is not None else DEFAULT_EXCLUDE)
except re.error:
raise click.BadParameter("Value should be a valid regular expression.")


@click.command("log-errors")
@click.option(
"-c", "--critical", "crit", type=int, default=60, help="Critical threshold."
Expand All @@ -36,8 +30,8 @@ def validate_exclude(ctx, param, value): # pylint: disable=unused-argument
"-e",
"--exclude",
"exclude",
type=RegexpParamType(),
default=DEFAULT_EXCLUDE,
callback=validate_exclude,
help="Excluded error.",
)
@click.option(
Expand Down

0 comments on commit 37f2c1b

Please sign in to comment.