stateless-uptime-kuma/stateless_uptime_kuma/cli.py
2024-04-21 17:35:57 +02:00

92 lines
1.9 KiB
Python

import json
import logging
import sys
import click
import click_log
from uptime_kuma_api import UptimeKumaApi
from .hydratation import hydrate_http_probes
from .tree_gen import from_dict
from .uptime_kuma import Manager
logger = logging.getLogger(__name__)
click_log.basic_config()
@click.group()
def cli():
pass
@cli.command()
@click_log.simple_verbosity_option()
@click.option(
"--file",
"-f",
help="File to import probes data from",
type=click.File("r"),
default=sys.stdin,
)
@click.option(
"--scrape-http-keywords",
"-s",
is_flag=True,
help="Scrape keywords for http probe",
default=False,
)
@click.option(
"--keywords-fallback/--no-keywords-fallback",
help="When scrapping keywords, fallback to http if no keywords found",
default=True,
)
@click.option(
"--no-autocreate-tags",
"-t",
is_flag=True,
help="Don't automatically create tags if not in tags section of input",
default=False,
)
@click.option(
"--username",
prompt=True,
)
@click.option(
"--host",
prompt=True,
)
@click.option(
"--password",
prompt=True,
hide_input=True,
envvar="UPTIME_KUMA_PASSWORD",
)
def apply_json(
file,
scrape_http_keywords,
no_autocreate_tags,
keywords_fallback,
username,
password,
host,
):
"""
Apply json probes
"""
logger.debug(
f"Flags value:\n - scrape_http_keywords: {scrape_http_keywords}\n - no_autocreate_tags: {no_autocreate_tags}\n - keywords_fallback: {keywords_fallback}"
)
with UptimeKumaApi(host) as api:
logging.debug("Reading json")
data = json.load(file)
logging.debug("Parsing json")
tree = from_dict(api, data, not no_autocreate_tags)
if scrape_http_keywords:
hydrate_http_probes(tree)
logging.debug("Sync probes")
api.login(username, password)
Manager(api, tree).process()
if __name__ == "__main__":
cli()