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

cli: Slight improvement of the 'swh backend list' command

parent 36749765
No related branches found
No related tags found
No related merge requests found
Pipeline #14680 passed
#!/usr/bin/env python3
# Copyright (C) 2024 The Software Heritage developers
# Copyright (C) 2024-2025 The Software Heritage developers
# See the AUTHORS file at the top-level directory of this distribution
# License: GNU General Public License version 3, or any later version
# See top-level LICENSE file for more information
......@@ -53,13 +53,15 @@ def bk_list(ctx, package, cls):
"""
from swh.core.config import get_swh_backend_module, list_swh_backends
G = lambda o: click.style(o, fg="green", bold=True) # noqa: E731
Y = lambda o: click.style(o, fg="yellow", bold=True) # noqa: E731
R = lambda o: click.style(o, fg="red", bold=True) # noqa: E731
if cls is None:
items = []
for backend in list_swh_backends(package):
_, BackendCls = get_swh_backend_module(package, backend)
msg = BackendCls.__doc__
if msg is None:
msg = ""
msg: str = BackendCls.__doc__ or BackendCls.__init__.__doc__ or ""
msg = msg.strip()
if "\n" in msg:
firstline = msg.splitlines()[0]
......@@ -67,12 +69,7 @@ def bk_list(ctx, package, cls):
firstline = msg
items.append((backend, firstline, msg))
if not items:
click.secho(
f"No backend found for package '{package}'",
fg="red",
bold=True,
err=True,
)
click.secho(R(f"No backend found for package '{package}'"), err=True)
raise click.Abort()
max_name = max(len(name) for name, _, _ in items)
......@@ -81,14 +78,7 @@ def bk_list(ctx, package, cls):
except OSError:
width = 78
for name, firstline, msg in items:
click.echo(
click.style(
f"{name:<{max_name + 1}}",
fg="green",
bold=True,
),
nl=False,
)
click.echo(G(f"{name:<{max_name + 1}}"), nl=False)
firstline = firstline[: width - max_name - 1]
click.echo(firstline)
else:
......@@ -105,19 +95,8 @@ def bk_list(ctx, package, cls):
err=True,
)
raise click.Abort()
click.echo(f"{G(package)}:{Y(cls)}")
click.echo(
click.style(
package,
fg="green",
bold=True,
)
+ ":"
+ click.style(
cls,
fg="yellow",
bold=True,
)
+ "\n",
)
click.echo(f" {G('class')}: {Y(BackendCls.__name__)}")
click.echo()
click.echo(BackendCls.__doc__.strip())
......@@ -36,7 +36,15 @@ def test_backend_list_cls_ok(swhmain, mock_get_entry_points):
runner = CliRunner()
result = runner.invoke(swhmain, ["backend", "list", "test", "backend1"])
assert_result(result)
assert result.output.strip() == "test:backend1\n\nA mockup backend for tests"
assert (
result.output.strip()
== """\
test:backend1
class: MockBackend
A mockup backend for tests\
"""
)
def test_backend_list_cls_no_package(swhmain, mock_get_entry_points):
......
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