diff options
author | Jan200101 <sentrycraft123@gmail.com> | 2022-05-04 15:04:46 +0200 |
---|---|---|
committer | Jan200101 <sentrycraft123@gmail.com> | 2022-05-04 15:04:46 +0200 |
commit | 1ec64ccb178a4164ac204925c20c559d96508a9b (patch) | |
tree | a11105d202afa7fb54273ab9d74985bfe7098faa /labbot/__main__.py | |
parent | 27b8241380d5f02fdcf00a832c281767ef258d9f (diff) | |
download | lab-bot-1ec64ccb178a4164ac204925c20c559d96508a9b.tar.gz lab-bot-1ec64ccb178a4164ac204925c20c559d96508a9b.zip |
add logging, offload addons into config, allow external addons
Diffstat (limited to 'labbot/__main__.py')
-rw-r--r-- | labbot/__main__.py | 61 |
1 files changed, 36 insertions, 25 deletions
diff --git a/labbot/__main__.py b/labbot/__main__.py index 9f0e3ba..57ffbcd 100644 --- a/labbot/__main__.py +++ b/labbot/__main__.py @@ -1,9 +1,16 @@ import click +import logging from typing import List from importlib import import_module -from labbot.config import read_config, write_config -from labbot.bot import Bot +import labbot.bot +import labbot.config +import labbot.logger + +DEFAULT_ADDONS = [ + "merge-label", + "approve-merge" +] @click.group() def main(): @@ -11,13 +18,15 @@ def main(): @main.command(help="Create a new instance") @click.option("--name", prompt=True, help="Name the instance will be given") -@click.option("--access_token", prompt=True, hide_input=True, help="Access Token to interact with the API") -@click.option("--secret", prompt=True, default="", help="Secret to receive webhook requests [can be empty]") +@click.option("--access_token", prompt="Access Token for the Bot account", hide_input=True, help="Access Token to interact with the API") +@click.option("--secret", prompt="Webhook Secret (can be empty)", default="", help="Secret to receive webhook requests (can be empty)") def setup(**data): instance_name = data.pop("name", "").replace(" ", "_").lower() + data["addons"] = DEFAULT_ADDONS + data["addon_path"] = "" - if not read_config(instance_name): - write_config(instance_name, data) + if not labbot.config.read_instance_config(instance_name): + labbot.config.write_instance_config(instance_name, data) click.echo(f"You can start your instance by running `lab-bot run {instance_name}`") else: click.echo(f"an instance with the name {instance_name} already exists") @@ -30,11 +39,11 @@ def config(name, **data): data = {k:v for k,v in data.items() if v} - conf = read_config(name) + conf = labbot.config.read_instance_config(name) if conf: if data: conf.update(data) - write_config(name, conf) + labbot.config.write_instance_config(name, conf) click.echo("configured") else: click.echo(f"nothing to change") @@ -43,38 +52,40 @@ def config(name, **data): pass -def load_addons(instance: Bot, addons: List[str]): - for addon in addons: - import_module(f"labbot.addons.{addon}").setup(instance) - click.echo(f"Imported {addon}") - @main.command(help="Run an instance") -@click.option("--port", default=8080) +@click.option("--port", default=8080, show_default=True, help="change the webhook port") +@click.option("--debug", is_flag=True, default=False, help="enable debug logging") @click.argument('name') -def run(name, port): - conf = read_config(name) +def run(name, port, debug: bool): + conf = labbot.config.read_instance_config(name) if not conf: click.echo(f"{name} is not an instance") return - instance = Bot( + if debug: + logger_level = logging.DEBUG + else: + logger_level = logging.INFO + + labbot.logger.init(logger_level) + + instance = labbot.bot.Bot( + name=name, + config=conf, secret=conf["secret"], access_token=conf["access_token"] ) - load_addons(instance, [ - "merge-label", - "approve-merge" - ]) - - click.echo(f"Started {name}") - instance.run( port=port ) - +@main.command(name="list", help="List all available instances") +def list_instances(): + print("Available Instances:") + for ins in labbot.config.list_instances(): + print(f"- {ins}") if __name__ == "__main__": main()
\ No newline at end of file |