Skip to content

Commit

Permalink
Rearange the cli commands, into one folder, separated by subcommands
Browse files Browse the repository at this point in the history
  • Loading branch information
tino097 committed Sep 3, 2018
1 parent 792be10 commit 27160c1
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 1 deletion.
52 changes: 52 additions & 0 deletions ckan/cli/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# encoding: utf-8

import os

import click
from flask import Flask, current_app
from flask.cli import AppGroup, with_appcontext
from werkzeug.serving import run_simple

from ckan.common import config
from ckan.config.environment import load_environment
from ckan.config.middleware import make_app


click_config_option = click.option(
'-c',
'--config',
default=None,
metavar='CONFIG',
help=u'Config file to use (default: development.ini)')


def load_config(config=None):

from paste.deploy import appconfig
from paste.script.util.logging_config import fileConfig

if config:
filename = os.path.abspath(config)
config_source = u'-c parameter'
elif os.environ.get(u'CKAN_INI'):
filename = os.environ.get(u'CKAN_INI')
config_source = u'$CKAN_INI'
else:
default_filename = u'development.ini'
filename = os.path.join(os.getcwd(), default_filename)
if not os.path.exists(filename):
# give really clear error message for this common situation
msg = u'ERROR: You need to specify the CKAN config (.ini) '\
u'file path.'\
u'\nUse the --config parameter or set environment ' \
u'variable CKAN_INI or have {}\nin the current directory.' \
.format(default_filename)
exit(msg)

if not os.path.exists(filename):
msg = u'Config file not found: %s' % filename
msg += u'\n(Given by: %s)' % config_source
exit(msg)

fileConfig(filename)
return appconfig(u'config:' + filename)
15 changes: 15 additions & 0 deletions ckan/cli/cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# encoding: utf-8

import os

import click


@click.group()
@click.help_option(u'-h', u'--help')
def ckan(*args, **kwargs):
pass


from ckan.cli.server.server import run
ckan.add_command(run)
Empty file added ckan/cli/server/__init__.py
Empty file.
27 changes: 27 additions & 0 deletions ckan/cli/server/server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# encoding: utf-8

import os

import click
from flask import Flask, current_app
from flask.cli import AppGroup, with_appcontext
from werkzeug.serving import run_simple

from ckan.common import config
from ckan.config.environment import load_environment

from ckan.config.middleware import make_app
from ckan.cli import load_config, click_config_option
from ckan.cli.cli import ckan

@ckan.command(u'run', short_help=u'Start development server')
@click.help_option(u'-h', u'--help')
@click_config_option
@click.option(u'-H', u'--host', default=u'localhost', help=u'Set host')
@click.option(u'-p', u'--port', default=5000, help=u'Set port')
@click.option(u'-r', u'--reloader', default=True, help=u'Use reloader')
def run(config, host, port, reloader):
u'''Runs development server'''
conf = load_config(config)
app = make_app(conf.global_conf, **conf.local_conf)
run_simple(host, port, app, use_reloader=reloader, use_evalex=True)
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def parse_version(s):
'jobs = ckan.lib.cli:JobsCommand',
],
'console_scripts': [
'ckan = ckan.lib.flask_cli:main',
'ckan = ckan.cli.cli:ckan',
'ckan-admin = bin.ckan_admin:Command',
],
'paste.paster_create_template': [
Expand Down

0 comments on commit 27160c1

Please sign in to comment.