From f0751a3cc5f9e00e2c077c96e26e5f56dbbed5d1 Mon Sep 17 00:00:00 2001 From: Ferdinand Thiessen Date: Thu, 23 Dec 2021 02:49:19 +0100 Subject: [PATCH] feat(cli): Added CLI command for handling plugins * Install / Uninstall plugins * List plugins --- flaschengeist/cli/plugin_cmd.py | 83 +++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 flaschengeist/cli/plugin_cmd.py diff --git a/flaschengeist/cli/plugin_cmd.py b/flaschengeist/cli/plugin_cmd.py new file mode 100644 index 0000000..3cad5f7 --- /dev/null +++ b/flaschengeist/cli/plugin_cmd.py @@ -0,0 +1,83 @@ +import click +from click.decorators import pass_context +from flask import current_app +from flask.cli import with_appcontext +from flaschengeist.utils.plugin import get_plugins, plugin_version + + +@click.group() +def plugin(): + pass + + +@plugin.command() +@click.argument("plugin", nargs=-1, type=str) +@click.option("--all", help="Install all enabled plugins", is_flag=True) +@with_appcontext +@pass_context +def install(ctx, plugin, all): + """Install one or more plugins""" + if not all and len(plugin) == 0: + ctx.fail("At least one plugin must be specified, or use `--all` flag.") + if all: + plugins = current_app.config["FG_PLUGINS"].values() + else: + try: + plugins = [current_app.config["FG_PLUGINS"][p] for p in plugin] + except KeyError as e: + ctx.fail(f"Invalid plugin ID, could not find >{e.args[0]}<") + for p in plugins: + name = p.id.split(".")[-1] + click.echo(f"Installing {name}{'.'*(20-len(name))}", nl=False) + p.install() + click.secho(" ok", fg="green") + + +@plugin.command() +@click.argument("plugin", nargs=-1, type=str) +@with_appcontext +@pass_context +def uninstall(ctx: click.Context, plugin): + """Uninstall one or more plugins""" + + if len(plugin) == 0: + ctx.fail("At least one plugin must be specified") + try: + plugins = [current_app.config["FG_PLUGINS"][p] for p in plugin] + except KeyError as e: + ctx.fail(f"Invalid plugin ID, could not find >{e.args[0]}<") + if ( + click.prompt( + "You are going to uninstall:\n\n" + f"\t{', '.join([p.id.split('.')[-1] for p in plugins])}\n\n" + "Are you sure?", + default="n", + show_choices=True, + type=click.Choice(["y", "N"], False), + ).lower() + != "y" + ): + ctx.exit() + for p in plugins: + name = p.id.split(".")[-1] + click.echo(f"Uninstalling {name}{'.'*(20-len(name))}", nl=False) + p.uninstall() + click.secho(" ok", fg="green") + + +@plugin.command() +@click.option("--enabled", "-e", help="List only enabled plugins", is_flag=True) +@with_appcontext +def ls(enabled): + if enabled: + plugins = current_app.config["FG_PLUGINS"].values() + else: + plugins = get_plugins() + + print(f"{' '*13}{'name': <20}|{'version': >10}") + print("-" * 46) + for plugin in plugins: + print( + f"{plugin.id: <33}|{plugin_version(plugin): >12}" + f"{click.style(' (enabled)', fg='green') if plugin.id in current_app.config['FG_PLUGINS'] else click.style(' (disabled)', fg='red')}" + )