Skip to content

Commit

Permalink
CLI. Implement config-tool command
Browse files Browse the repository at this point in the history
  • Loading branch information
smotornyuk committed Mar 26, 2019
1 parent 793f57f commit 79ed02e
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 1 deletion.
7 changes: 6 additions & 1 deletion ckan/cli/cli.py
Expand Up @@ -4,7 +4,11 @@

import click

from ckan.cli import click_config_option, db, load_config, search_index, server
from ckan.cli import (
click_config_option, db, load_config, search_index, server,
config_tool,
)

from ckan.config.middleware import make_app

log = logging.getLogger(__name__)
Expand All @@ -28,3 +32,4 @@ def ckan(ctx, config, *args, **kwargs):
ckan.add_command(server.run)
ckan.add_command(db.db)
ckan.add_command(search_index.search_index)
ckan.add_command(config_tool.config_tool)
77 changes: 77 additions & 0 deletions ckan/cli/config_tool.py
@@ -0,0 +1,77 @@
# encoding: utf-8

import logging

import click

from ckan.cli import error_shout
import ckan.lib.config_tool as ct

log = logging.getLogger(__name__)


class ConfigOption(click.ParamType):
name = u'config-option'

def convert(self, value, param, ctx):
if u'=' not in value:
self.fail(
u'An option does not have an equals sign. '
u'It should be \'key=value\'. If there are spaces '
u'you\'ll need to quote the option.\n'
)
return value


@click.command(
name=u'config-tool',
short_help=u'Tool for editing options in a CKAN config file.'
)
@click.option(
u'--section',
u'-s',
default=u'app:main',
help=u'Section of the config file'
)
@click.option(
u'--edit',
u'-e',
is_flag=True,
help=u'Checks the option already exists in the config file.'
)
@click.option(
u'--file',
u'-f',
u'merge_filepath',
help=u'Supply an options file to merge in.'
)
@click.argument(u'config_filepath', type=click.Path(exists=True))
@click.argument(u'options', nargs=-1, type=ConfigOption())
def config_tool(config_filepath, options, section, edit, merge_filepath):
u'''Tool for editing options in a CKAN config file
paster config-tool <default.ini> <key>=<value> [<key>=<value> ...]
paster config-tool <default.ini> -f <custom_options.ini>
Examples:
paster config-tool default.ini sqlalchemy.url=123 'ckan.site_title=ABC'
paster config-tool default.ini -s server:main -e port=8080
paster config-tool default.ini -f custom_options.ini
'''

if merge_filepath:
ct.config_edit_using_merge_file(
config_filepath, merge_filepath
)
if not (options or merge_filepath):
return error_shout(u'No options provided')
try:
ct.config_edit_using_option_strings(
config_filepath, options, section, edit=edit
)
except ct.ConfigToolError as e:
error_shout(e)

0 comments on commit 79ed02e

Please sign in to comment.