Skip to content

Commit

Permalink
Use LocaleResolver in monrun_checks_keeper
Browse files Browse the repository at this point in the history
  • Loading branch information
dstaroff committed Aug 3, 2023
1 parent ee7be48 commit 4ed44b8
Showing 1 changed file with 48 additions and 12 deletions.
60 changes: 48 additions & 12 deletions src/ch_tools/monrun_checks_keeper/main.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import logging
import os
from functools import wraps
from typing import Optional

import click
from click import group, option, pass_context
import cloup

from ch_tools import __version__
from ch_tools.common.cli.context_settings import CONTEXT_SETTINGS
from ch_tools.common.cli.locale_resolver import LocaleResolver
from ch_tools.common.result import Status
from ch_tools.monrun_checks_keeper.keeper_commands import (
alive_command,
Expand All @@ -21,13 +25,30 @@

LOG_FILE = "/var/log/keeper-monitoring/keeper-monitoring.log"

# pylint: disable=too-many-ancestors


class KeeperChecks(cloup.Group):
def add_command(
self,
cmd: click.Command,
name: Optional[str] = None,
section: Optional[cloup.Section] = None,
fallback_to_default_section: bool = True,
) -> None:
if cmd.callback is None:
super().add_command(
cmd,
name=name,
section=section,
fallback_to_default_section=fallback_to_default_section,
)
return

class KeeperChecks(click.Group):
def add_command(self, cmd, name=None):
cmd_callback = cmd.callback

@wraps(cmd_callback)
@click.pass_context
@cloup.pass_context
def wrapper(ctx, *a, **kw):
os.makedirs(os.path.dirname(LOG_FILE), exist_ok=True)
logging.basicConfig(
Expand Down Expand Up @@ -58,28 +79,39 @@ def wrapper(ctx, *a, **kw):
status.report()

cmd.callback = wrapper
super().add_command(cmd, name=name)
super().add_command(
cmd,
name=name,
section=section,
fallback_to_default_section=fallback_to_default_section,
)


@group(cls=KeeperChecks, context_settings={"help_option_names": ["-h", "--help"]})
@option("-r", "--retries", "retries", type=int, default=3, help="Number of retries")
@option(
@cloup.group(
cls=KeeperChecks,
context_settings=CONTEXT_SETTINGS,
)
@cloup.option(
"-r", "--retries", "retries", type=int, default=3, help="Number of retries"
)
@cloup.option(
"-t",
"--timeout",
"timeout",
type=float,
default=0.5,
help="Connection timeout (in seconds)",
)
@option(
@cloup.option(
"-n",
"--no-verify-ssl-certs",
"no_verify_ssl_certs",
is_flag=True,
default=False,
help="Allow unverified SSL certificates, e.g. self-signed ones",
)
@pass_context
@cloup.version_option(__version__)
@cloup.pass_context
def cli(ctx, retries, timeout, no_verify_ssl_certs):
ctx.obj = dict(
retries=retries, timeout=timeout, no_verify_ssl_certs=no_verify_ssl_certs
Expand Down Expand Up @@ -108,5 +140,9 @@ def main():
"""
Program entry point.
"""
# pylint: disable=no-value-for-parameter
cli()
LocaleResolver.resolve()
cli.main()


if __name__ == "__main__":
main()

0 comments on commit 4ed44b8

Please sign in to comment.