goat_monitor/goat_monitor.py

67 lines
1.6 KiB
Python
Raw Normal View History

# %% 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.pass_context
# @click.option(
# "--config",
# default="~/.config/goat_monitor.toml",
# type=click.Path(exists=False, writable=True, path_type=Path),
# help="Specify a .toml configuration file location",
# )
# def init(config: Path):
# with open(config, "w") as f:
# cfg_data = dict()
# toml.dump(cfg_data, f)
def create_config(ctx):
print("init called")
print(ctx)
@click.group(invoke_without_command=True)
@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",
)
@click.option("--init", is_flag=True, default=False, help="Create a configuration file")
@click.pass_context
def wrap(ctx, command: List[str], config: Path, init: bool): # , config: Path):
"""Wrap an arbitrary command with gotify notifications"""
if init:
print("calling init")
create_config(command)
else:
# 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="")
return result.returncode
# %% main
if __name__ == "__main__":
wrap()