From 0e9eba4900c104c365e1be24b1566e71f75a04c0 Mon Sep 17 00:00:00 2001 From: Brendan Haines Date: Sat, 14 Sep 2024 17:59:49 -0600 Subject: [PATCH] add dry-run --- goat_monitor/goat_monitor.py | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/goat_monitor/goat_monitor.py b/goat_monitor/goat_monitor.py index 91747fa..44015e1 100644 --- a/goat_monitor/goat_monitor.py +++ b/goat_monitor/goat_monitor.py @@ -34,7 +34,13 @@ from goat_monitor._version import __version__ # type: ignore default=False, help="Print version and exit", ) -def wrap(command: List[str], config: Path, retries: int, version): +@click.option( + "--dry-run", + is_flag=True, + default=False, + help="Run command without sending any notifications", +) +def wrap(command: List[str], config: Path, retries: int, version: bool, dry_run: bool): """Wrap an arbitrary command with gotify notifications""" if version: @@ -46,13 +52,14 @@ def wrap(command: List[str], config: Path, retries: int, version): settings = toml.load(f) # gotify configuration - url = settings["server"] - app_token = settings["app_token"] - with gotify.Gotify(base_url=url, app_token=app_token) as gotify_connection: - # this is just to test configuration. - # I don't know how long a gotify connection will stay up so rather than thinking about it - # I'm just creating a new one whenever I need to send a message - pass + if not dry_run: + url = settings["server"] + app_token = settings["app_token"] + with gotify.Gotify(base_url=url, app_token=app_token) as gotify_connection: + # this is just to test configuration. + # I don't know how long a gotify connection will stay up so rather than thinking about it + # I'm just creating a new one whenever I need to send a message + pass if retries == -1: retries = np.inf @@ -95,8 +102,9 @@ def wrap(command: List[str], config: Path, retries: int, version): + "\n".join(lines) ) - with gotify.Gotify(base_url=url, app_token=app_token) as gotify_connection: - gotify_connection.create_message(message=message, title=title) + if not dry_run: + with gotify.Gotify(base_url=url, app_token=app_token) as gotify_connection: + gotify_connection.create_message(message=message, title=title) if not result.returncode: # only retry on failure