diff --git a/setup.py b/setup.py --- a/setup.py +++ b/setup.py @@ -58,7 +58,7 @@ loader.npm=swh.loader.package.npm:register loader.pypi=swh.loader.package.pypi:register [swh.cli.subcommands] - loader=swh.loader.cli:run + loader=swh.loader.cli:loader ''', classifiers=[ "Programming Language :: Python :: 3", diff --git a/swh/loader/cli.py b/swh/loader/cli.py --- a/swh/loader/cli.py +++ b/swh/loader/cli.py @@ -3,6 +3,7 @@ # License: GNU General Public License version 3, or any later version # See top-level LICENSE file for more information +import inspect import logging import click @@ -48,21 +49,42 @@ return loader_cls(**kwargs) -@click.command(name='run', context_settings=CONTEXT_SETTINGS) -@click.option('--type', '-t', help='Loader to run', - type=click.Choice(SUPPORTED_LOADERS)) -@click.option('--url', '-u', default=None, - help="Origin url to load") -@click.argument('options', nargs=-1) +@click.group(name='loader', context_settings=CONTEXT_SETTINGS) @click.pass_context -def run(ctx, type, url, options): +def loader(ctx): """Loader cli tools - Load an origin from its url with loader - """ + pass + + +@loader.command(name='run', context_settings=CONTEXT_SETTINGS) +@click.argument('type', + type=click.Choice(SUPPORTED_LOADERS)) +@click.argument('url') +@click.argument('options', nargs=-1) +@click.pass_context +def run(ctx, type, url, options): + """Ingest with loader the origin located at """ (_, kw) = parse_options(options) logger.debug(f'kw: {kw}') loader = get_loader(type, url=url, **kw) result = loader.load() click.echo(result) + + +@loader.command(name='list', context_settings=CONTEXT_SETTINGS) +@click.argument('type', default='all', + type=click.Choice(['all'] + SUPPORTED_LOADERS)) +@click.pass_context +def list(ctx, type): + """List supported loaders and optionally their arguments""" + if type == 'all': + loaders = ', '.join(SUPPORTED_LOADERS) + click.echo(f'Supported loaders: {loaders}') + else: + registry_entry = LOADERS[type].load()() + loader_cls = registry_entry['loader'] + doc = inspect.getdoc(loader_cls).strip() + signature = inspect.signature(loader_cls) + click.echo(f"Loader: {doc}\nsignature: {signature}") diff --git a/swh/loader/tests/test_cli.py b/swh/loader/tests/test_cli.py --- a/swh/loader/tests/test_cli.py +++ b/swh/loader/tests/test_cli.py @@ -6,7 +6,7 @@ import pytest -from swh.loader.cli import run, get_loader, SUPPORTED_LOADERS +from swh.loader.cli import run, list, get_loader, SUPPORTED_LOADERS from swh.loader.package.loader import PackageLoader from click.testing import CliRunner @@ -52,18 +52,13 @@ assert isinstance(loader, PackageLoader) -help_msg = """Usage: run [OPTIONS] [OPTIONS]... +run_help_msg = """Usage: run [OPTIONS] [archive|debian|deposit|npm|pypi] URL [OPTIONS]... - Loader cli tools - - Load an origin from its url with loader + Ingest with loader the origin located at Options: - -t, --type [archive|debian|deposit|npm|pypi] - Loader to run - -u, --url TEXT Origin url to load - -h, --help Show this message and exit. -""" + -h, --help Show this message and exit. +""" # noqa def test_run_help(swh_config): @@ -72,8 +67,9 @@ """ runner = CliRunner() result = runner.invoke(run, ['-h']) + assert result.exit_code == 0 - assert result.output.startswith(help_msg) + assert result.output.startswith(run_help_msg) def test_run_pypi(mocker, swh_config): @@ -82,9 +78,25 @@ """ mock_loader = mocker.patch('swh.loader.package.pypi.loader.PyPILoader') runner = CliRunner() - result = runner.invoke(run, [ - '--type', 'pypi', - '--url', 'https://some-url' - ]) + result = runner.invoke(run, ['pypi', 'https://some-url']) assert result.exit_code == 0 mock_loader.assert_called_once_with(url='https://some-url') # constructor + + +list_help_msg = """Usage: list [OPTIONS] [[all|archive|debian|deposit|npm|pypi]] + + List supported loaders and optionally their arguments + +Options: + -h, --help Show this message and exit. +""" # noqa + + +def test_list_help(mocker, swh_config): + """Triggering a load should be ok + + """ + runner = CliRunner() + result = runner.invoke(list, ['--help']) + assert result.exit_code == 0 + assert result.output.startswith(list_help_msg)