Compare commits

..

No commits in common. "5891409fdb75ae9061e74a5ef9a53937da354ecf" and "e02737abd13bde9c12a5f1aa526d0e8de2a58416" have entirely different histories.

View File

@ -34,13 +34,7 @@ from goat_monitor._version import __version__ # type: ignore
default=False,
help="Print version and exit",
)
@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):
def wrap(command: List[str], config: Path, retries: int, version):
"""Wrap an arbitrary command with gotify notifications"""
if version:
@ -52,7 +46,6 @@ def wrap(command: List[str], config: Path, retries: int, version: bool, dry_run:
settings = toml.load(f)
# gotify configuration
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:
@ -69,25 +62,20 @@ def wrap(command: List[str], config: Path, retries: int, version: bool, dry_run:
attempt = 0
while attempt <= retries:
# run the command
with subprocess.Popen(
result = subprocess.run(
" ".join(command),
shell=True,
stderr=subprocess.STDOUT,
stdout=subprocess.PIPE,
) as proc:
stdout = ""
while True:
c = proc.stdout.read(1)
if c == b"":
break
s = c.decode("utf-8")
stdout += s
print(s, end="", flush=True)
return_code = proc.wait()
encoding="utf-8",
)
if return_code:
# TODO: print lines real time as the subprocess runs
print(result.stdout, end="")
if result.returncode:
# failed
title = f"Command failed with exit code {return_code}"
title = f"Command failed with exit code {result.returncode}"
if attempt < retries:
title += f" - Retrying (attempt {attempt+1}/{retries})"
else:
@ -97,7 +85,7 @@ def wrap(command: List[str], config: Path, retries: int, version: bool, dry_run:
title = "Command succeeded"
MAX_LINES = 20
lines = stdout.splitlines()
lines = result.stdout.splitlines()
if len(lines) > MAX_LINES:
lines = lines[-MAX_LINES:]
message = (
@ -107,17 +95,16 @@ def wrap(command: List[str], config: Path, retries: int, version: bool, dry_run:
+ "\n".join(lines)
)
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 return_code:
if not result.returncode:
# only retry on failure
break
attempt += 1
sys.exit(return_code)
sys.exit(result.returncode)
# %% main