Skip to content

Commit

Permalink
[#5108] Alloe extensions to define Click commands
Browse files Browse the repository at this point in the history
Currently, there is no way for extension to add commands
to the main ckan one

This changes implements the possibility for extensions
to do it.
  • Loading branch information
calexandr committed Dec 3, 2019
1 parent fbab8f5 commit dd4f1c1
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
17 changes: 16 additions & 1 deletion ckan/cli/cli.py
Expand Up @@ -2,6 +2,7 @@

import logging

import ckan.plugins as p
import click
from ckan.cli import config_tool
from ckan.cli import (
Expand Down Expand Up @@ -31,19 +32,33 @@
log = logging.getLogger(__name__)


class IClick(click.Group):
def get_command(self, ctx, name):
cmd = self.commands.get(name)
if not cmd:
ckan.invoke(ctx)
cmd = self.commands.get(name)
return cmd


class CkanCommand(object):

def __init__(self, conf=None):
self.config = load_config(conf)
self.app = make_app(self.config.global_conf, **self.config.local_conf)


@click.group()
@click.group(
invoke_without_command=True,
cls=IClick
)
@click.help_option(u'-h', u'--help')
@click_config_option
@click.pass_context
def ckan(ctx, config, *args, **kwargs):
ctx.obj = CkanCommand(config)
for plugin in p.PluginImplementations(p.ICLICommands):
ckan.add_command(plugin.get_commands())


ckan.add_command(jobs.jobs)
Expand Down
12 changes: 12 additions & 0 deletions ckan/plugins/interfaces.py
Expand Up @@ -39,6 +39,7 @@
u'IBlueprint',
u'IPermissionLabels',
u'IForkObserver',
u'ICLICommands',
]


Expand Down Expand Up @@ -1779,3 +1780,14 @@ def before_fork(self):
u'''
Called shortly before the CKAN process is forked.
'''


class ICLICommands(Interface):
u'''
Allow extensions to define click commands.
'''
def get_commands(self):
u'''
Return command function object
to be registered by the click.add_command.
'''

0 comments on commit dd4f1c1

Please sign in to comment.