Skip to content

Commit

Permalink
225740 Add pluggable system to the CLI tool
Browse files Browse the repository at this point in the history
  • Loading branch information
d3rky committed May 28, 2024
1 parent 004b31e commit 24464a1
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 1 deletion.
19 changes: 19 additions & 0 deletions swo/mpt/cli/core/plugins.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from importlib.metadata import EntryPoint, entry_points

from typer import Typer

PLUGINS_PACKAGE = "swo.mpt.cli.plugins"


def list_entrypoints() -> list[EntryPoint]: # pragma: no cover
entrypoints = entry_points().select(group=PLUGINS_PACKAGE)
if not entrypoints:
return []

return entrypoints


def load_plugins(app: Typer):
for entrypoint in list_entrypoints():
plugin = entrypoint.load()
app.add_typer(plugin.app, name=entrypoint.name)
Empty file added swo/mpt/cli/plugins/__init__.py
Empty file.
6 changes: 5 additions & 1 deletion swo/mpt/cli/swocli.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from swo.mpt.cli.core.accounts import app as accounts_app
from swo.mpt.cli.core.alias_group import AliasTyperGroup
from swo.mpt.cli.core.console import console, show_banner
from swo.mpt.cli.core.plugins import load_plugins
from swo.mpt.cli.core.pricelists import app as pricelists_app
from swo.mpt.cli.core.products import app as products_app

Expand All @@ -15,7 +16,7 @@


try:
VERSION = version("swo-marketplace-cli")
VERSION = version("mpt-cli")
except PackageNotFoundError: # pragma: no cover
VERSION = "unknown"

Expand Down Expand Up @@ -43,5 +44,8 @@ def main(
show_banner()


load_plugins(app)


if __name__ == "__main__":
app()
Empty file added tests/plugins/__init__.py
Empty file.
1 change: 1 addition & 0 deletions tests/plugins/test_plugin/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .app import app # noqa
13 changes: 13 additions & 0 deletions tests/plugins/test_plugin/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import typer
from swo.mpt.cli.core.console import console

app = typer.Typer()


@app.command()
def test():
console.print("I'm a test plugin")


if __name__ == "__main__":
app()
27 changes: 27 additions & 0 deletions tests/test_swo/test_mpt/test_cli/test_core/test_plugins.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from importlib.metadata import EntryPoint

from swo.mpt.cli.core.plugins import load_plugins
from swo.mpt.cli.swocli import app
from typer.testing import CliRunner

from tests.plugins.test_plugin import app as plugin_app

runner = CliRunner()


def test_load_plugins(mocker):
mocker.patch.object(
EntryPoint,
"load",
side_effect=lambda: mocker.MagicMock(app=plugin_app),
)
mocker.patch(
"swo.mpt.cli.core.plugins.list_entrypoints",
return_value=[EntryPoint("tests", "tests.plugins.test_plugin", None)],
)

load_plugins(app)
result = runner.invoke(app, ["tests", "test"])

assert result.exit_code == 0, result.stdout
assert "I'm a test plugin" in result.stdout

0 comments on commit 24464a1

Please sign in to comment.