aboutsummaryrefslogtreecommitdiff
path: root/labbot/__main__.py
diff options
context:
space:
mode:
authorJan200101 <sentrycraft123@gmail.com>2022-03-29 11:35:51 +0200
committerJan200101 <sentrycraft123@gmail.com>2022-03-29 11:38:27 +0200
commit0ffe0731e6aca624695dd528761d2e459256ee67 (patch)
tree124b14d70b237289b1104aca76a0386557cebf7c /labbot/__main__.py
downloadlab-bot-0ffe0731e6aca624695dd528761d2e459256ee67.tar.gz
lab-bot-0ffe0731e6aca624695dd528761d2e459256ee67.zip
Initial commit0.0.0
Diffstat (limited to 'labbot/__main__.py')
-rw-r--r--labbot/__main__.py79
1 files changed, 79 insertions, 0 deletions
diff --git a/labbot/__main__.py b/labbot/__main__.py
new file mode 100644
index 0000000..5e0a3ae
--- /dev/null
+++ b/labbot/__main__.py
@@ -0,0 +1,79 @@
+import click
+from typing import List
+from importlib import import_module
+
+from labbot.config import read_config, write_config
+from labbot.bot import Bot
+
+@click.group()
+def main():
+ pass
+
+@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]")
+def setup(**data):
+ instance_name = data.pop("name", "").replace(" ", "_").lower()
+
+ if not read_config(instance_name):
+ write_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")
+
+@main.command(help="Configure an existing instance")
+@click.argument('name')
+@click.option("--access_token", required=False)
+@click.option("--secret", required=False)
+def config(name, **data):
+
+ data = {k:v for k,v in data.items() if v}
+
+ conf = read_config(name)
+ if conf:
+ if data:
+ conf.update(data)
+ write_config(name, conf)
+ click.echo("configured")
+ else:
+ click.echo(f"nothing to change")
+ else:
+ click.echo(f"{name} is not an instance")
+ 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.argument('name')
+def run(name, port):
+ conf = read_config(name)
+
+ if not conf:
+ click.echo(f"{name} is not an instance")
+ return
+
+ instance = Bot(
+ secret=conf["secret"],
+ access_token=conf["access_token"]
+ )
+
+ load_addons(instance, [
+ "merge-label"
+ ])
+
+ click.echo(f"Started {name}")
+
+ instance.run(
+ port=port
+ )
+
+
+
+if __name__ == "__main__":
+ main() \ No newline at end of file