Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix list command names from importlib #189

Merged
merged 2 commits into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions pifpaf/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,16 @@ def _format_multiple_exceptions(e, debug=False):
DAEMONS = importlib.metadata.entry_points()['pifpaf.daemons']


def _daemons_names():
names = []
if hasattr(DAEMONS, 'names'):
names = DAEMONS.names
else:
for i in DAEMONS:
names.append(i.name)
return names


@click.group()
@click.option('--verbose/--quiet', help="Print mode details.")
@click.option('--debug', help="Show tracebacks on errors.", is_flag=True)
Expand Down Expand Up @@ -119,14 +129,14 @@ def main(ctx, verbose=False, debug=False, log_file=None,

@main.command(name="list")
def drivers_list():
for n in DAEMONS.keys():
for n in _daemons_names():
click.echo(n)


class RunGroup(click.MultiCommand):
@staticmethod
def list_commands(ctx):
return DAEMONS.keys()
return _daemons_names()

def get_command(self, ctx, name):
params = [click.Argument(["command"], nargs=-1)]
Expand Down
8 changes: 8 additions & 0 deletions pifpaf/tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,14 @@ def test_global_urls_variable_old_format(self):
b"\"memcached://localhost:11217;memcached://localhost:11218\";",
env[b"export PIFPAF_URLS"])

def test_list_command(self):
c = subprocess.Popen(["pifpaf", "list"],
bufsize=0,
stdout=subprocess.PIPE)
(stdout, stderr) = c.communicate()
self.assertEqual(0, c.wait())
self.assertIn(b'memcached', stdout)

def test_non_existing_command(self):
# Keep PATH to just the one set by tox to run pifpaf
self.useFixture(fixtures.EnvironmentVariable(
Expand Down
Loading