-
-
Notifications
You must be signed in to change notification settings - Fork 644
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
register_commands()
plugin hook to register extra CLI commands
#1449
Comments
The closest plugin hook to this right now is publish_subcommand - which looks like this: @hookimpl
def publish_subcommand(publish):
@publish.command()
@add_common_publish_arguments_and_options
@click.option(
"-k",
"--api_key",
help="API key for talking to my hosting provider",
)
def my_hosting_provider(...): But there are also several plugin hooks with |
There's also the option for plugins to muck around with existing registered commands - this could get a bit untidy if multiple plugins try to do it, but being able to replace |
Terminology question: is it correct to call these subcommands or should they be commands? |
Here's the answer to that:
Since it's adding extra things that show up in |
I'm going to call the new hook |
register_commands()
plugin hook to register extra CLI commands
The first example plugin I'm going to build for this will be |
Considering this piece of code: Lines 122 to 142 in a1a33bb
I think the hook itself gets called with a single argument, I can't pass it |
I don't think I can get this new hook to support the handy --plugins-dir= mechanism for loading plugins from Python files as opposed to registering them with setuptools - that mechanism is itself implemented inside of code called by |
I'll probably have to use this mechanism for the tests then: https://til.simonwillison.net/pytest/registering-plugins-in-tests |
Partial prototype of from datasette import hookimpl
import click
@hookimpl
def register_commands(cli):
from datasette.cli import sqlite_extensions
@cli.command()
@click.argument("files", type=click.Path(exists=True), nargs=-1)
@sqlite_extensions
def verify(files, sqlite_extensions):
"Verify that files can be opened by Datasette"
for file in files:
print(file) I had to move the |
Writing the test for this is proving difficult, because the Trying to work around that with |
I need to push this out as an alpha so I can release a demo plugin that uses it. |
The
datasette
CLI tool currently has 7 subcommands:serve
,inspect
,install
,package
,plugins
,publish
anduninstall
.A plugin hook could allow plugins to register extra subcommands.
I've avoided this for quite a while because I didn't have good use-cases for it - but the existence of the
datasette install xxx
command for installing packages into the correct virtual environment means that actually there's a good reason to do this: it would allow plugins to provide additional command-line mechanisms without the user having to understand how virtual environments work in order to install those commands into the same environment as the rest of Datasette.The text was updated successfully, but these errors were encountered: