stateless-uptime-kuma/stateless_uptime_kuma/cli.py

71 lines
1.7 KiB
Python
Raw 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,
)
def apply_json(file, scrape_http_keywords, no_autocreate_tags, keywords_fallback):
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}"
)
2024-04-16 22:48:52 +02:00
with UptimeKumaApi("http://localhost:3001") as api:
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("admin", "123456789a")
2024-04-16 22:48:52 +02:00
Manager(api, tree).process()
if __name__ == "__main__":
cli()