stateless-uptime-kuma/stateless_uptime_kuma/cli.py

93 lines
1.9 KiB
Python
Raw Permalink Normal View History

2024-04-16 22:48:52 +02:00
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,
2024-04-21 17:35:57 +02:00
envvar="UPTIME_KUMA_PASSWORD",
)
def apply_json(
file,
scrape_http_keywords,
no_autocreate_tags,
keywords_fallback,
username,
password,
host,
):
2024-04-16 22:48:52 +02:00
"""
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:
2024-04-16 22:48:52 +02:00
logging.debug("Reading json")
data = json.load(file)
logging.debug("Parsing json")
tree = from_dict(api, data, not no_autocreate_tags)
2024-04-16 22:48:52 +02:00
if scrape_http_keywords:
hydrate_http_probes(tree)
logging.debug("Sync probes")
api.login(username, password)
2024-04-16 22:48:52 +02:00
Manager(api, tree).process()
if __name__ == "__main__":
2024-04-21 17:35:57 +02:00
cli()