Skip to content
Snippets Groups Projects
Commit 08788aa3 authored by David Douard's avatar David Douard
Browse files

Do not automagically add click cmd from swh.cli.subcommands

the idea is now to be a bit more explicit, and expect thoses commands
to have the main `swh` click group as parent.

Then the swh.cli.subcommands entrypoint hooks only have to load the
declared cli modules.

So we now expect to:

- use swh.core.cli.swh as parent group when declaring
  sub-groups/commands in a swh package.
- the swh.cli.subcommand entrypoint to only refer the module rather than
  the click group/command, ie. have:

  '''
    [swh.cli.subcommands]
    name = swh.path.to.cli_module
  '''

  instead of:

  '''
    [swh.cli.subcommands]
    name = swh.path.to.cli_module:click_command
  '''

A backward compatilibility mech is provided: if the loaded entrypoint
object is indeed a click command, auto add it to the main swh group.
parent 56d505f3
No related branches found
No related tags found
No related merge requests found
......@@ -67,8 +67,7 @@ setup(
swh=swh.core.cli:main
swh-db-init=swh.core.cli.db:db_init
[swh.cli.subcommands]
db=swh.core.cli.db:db
db-init=swh.core.cli.db:db_init
db=swh.core.cli.db
[pytest11]
pytest_swh_core = swh.core.pytest_plugin
""",
......
......@@ -5,6 +5,7 @@
import logging
import logging.config
import warnings
import click
import pkg_resources
......@@ -117,7 +118,17 @@ def main():
for entry_point in pkg_resources.iter_entry_points("swh.cli.subcommands"):
try:
cmd = entry_point.load()
swh.add_command(cmd, name=entry_point.name)
if isinstance(cmd, click.BaseCommand):
# for BW compat, auto add click commands
warnings.warn(
f"{entry_point.name}: automagic addition of click commands "
f"to the main swh group is deprecated",
DeprecationWarning,
)
swh.add_command(cmd, name=entry_point.name)
# otherwise it's expected to be a module which has been loaded
# it's the responsibility of the click commands/groups in this
# module to transitively have the main swh group as parent.
except Exception as e:
logger.warning("Could not load subcommand %s: %s", entry_point.name, str(e))
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment