52 lines
1.2 KiB
Python
52 lines
1.2 KiB
Python
# %% imports
|
|
import subprocess
|
|
from pathlib import Path
|
|
from typing import List
|
|
|
|
import click
|
|
import gotify
|
|
import toml
|
|
|
|
__version__ = "v0.0.0"
|
|
|
|
|
|
# %% commands
|
|
@click.command()
|
|
@click.argument("command", nargs=-1, required=False, type=str)
|
|
@click.option(
|
|
"--config",
|
|
default="~/.config/goat_monitor.toml",
|
|
type=click.Path(path_type=Path),
|
|
help="Use a .toml configuration file",
|
|
)
|
|
def wrap(command: List[str], config: Path):
|
|
"""Wrap an arbitrary command with gotify notifications"""
|
|
|
|
# read settings first
|
|
with open(config, "r") as f:
|
|
settings = toml.load(f)
|
|
|
|
url = settings["server"]
|
|
|
|
with gotify.Gotify(base_url=url) as gotify_connection:
|
|
# run the command
|
|
result = subprocess.run(
|
|
command,
|
|
shell=True,
|
|
stderr=subprocess.STDOUT,
|
|
stdout=subprocess.PIPE,
|
|
encoding="utf-8",
|
|
)
|
|
|
|
# TODO: print lines real time as the subprocess runs
|
|
print(result.stdout, end="")
|
|
|
|
gotify_connection.create_message(message=f'Command "{" ".join(command)}" finished {""}', title="")
|
|
|
|
return result.returncode
|
|
|
|
|
|
# %% main
|
|
if __name__ == "__main__":
|
|
wrap()
|