feat(cli): Added CLI command for handling plugins
* Install / Uninstall plugins * List plugins
This commit is contained in:
parent
76f660c160
commit
46ecfcd62a
|
@ -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')}"
|
||||
)
|
Loading…
Reference in New Issue