[PATCH 3 of 3 PoC] cli: convert 'gearbox make-config' to 'kallithea config create'

Thomas De Schampheleire patrickdepinguin at gmail.com
Mon Sep 24 20:45:24 UTC 2018


# HG changeset patch
# User Thomas De Schampheleire <thomas.de_schampheleire at nokia.com>
# Date 1537821464 -7200
#      Mon Sep 24 22:37:44 2018 +0200
# Node ID 3c33619af70545eb9b3cbba585181094df67afbb
# Parent  b807b1e90e61d2c8c16c5e3ec5978ef7dd7a2d31
cli: convert 'gearbox make-config' to 'kallithea config create'

Notes regarding the original implementation:
- show_defaults was trying to obtain a custom
value from mako_variable_values, which would never happen because
the argument parser would not allow passing custom key-value pairs in
combination with '--show-defaults'.

- variables TMPL_FILE and here were no longer used

TODO: documentation update

diff --git a/kallithea/bin/kallithea_cli.py b/kallithea/bin/kallithea_cli.py
--- a/kallithea/bin/kallithea_cli.py
+++ b/kallithea/bin/kallithea_cli.py
@@ -14,6 +14,7 @@
 
 import click
 
+from kallithea.bin.kallithea_cli_config import config
 from kallithea.bin.kallithea_cli_frontend import frontend
 
 @click.group()
@@ -21,4 +22,5 @@ def cli():
     """Various commands to set up a Kallithea instance."""
     pass
 
+cli.add_command(config)
 cli.add_command(frontend)
diff --git a/kallithea/lib/paster_commands/make_config.py b/kallithea/bin/kallithea_cli_config.py
rename from kallithea/lib/paster_commands/make_config.py
rename to kallithea/bin/kallithea_cli_config.py
--- a/kallithea/lib/paster_commands/make_config.py
+++ b/kallithea/bin/kallithea_cli_config.py
@@ -11,38 +11,40 @@
 #
 # You should have received a copy of the GNU General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
-"""
-kallithea.lib.paster_commands.make_config
-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
-make-config gearbox command for Kallithea
-
-:license: GPLv3, see LICENSE.md for more details.
-"""
-
-
+import click
 import os
-import sys
 import uuid
-import argparse
 from collections import defaultdict
 
 import mako.exceptions
 
-TMPL = 'template.ini.mako'
-here = os.path.dirname(os.path.abspath(__file__))
-
-from kallithea.lib.paster_commands.common import BasePasterCommand
 from kallithea.lib import inifile
 
+ at click.group()
+def config():
+    pass
 
-class Command(BasePasterCommand):
-    """Kallithea: Create a new config file
+def show_defaults(ctx, param, value):
+    if not value or ctx.resilient_parsing:
+        return
+
+    for key, value in inifile.default_variables.items():
+        click.echo('%s=%s' % (key, value))
 
-    make-config is part of a two-phase installation process (the
-    second phase is setup-app). make-config creates a bare configuration
-    file (possibly filling in defaults from the extra
-    variables you give).
+    ctx.exit()
+
+ at config.command()
+ at click.option('--show-defaults', callback=show_defaults,
+              is_flag=True, expose_value=False, is_eager=True,
+              help='Show the default values that can be overridden')
+ at click.argument('config_file', type=click.Path())
+ at click.argument('key_value_pairs', nargs=-1)
+def create(config_file, key_value_pairs):
+    """Create a new configuration file
+
+    This command creates a bare configuration file (possibly filling in
+    defaults from the extra variables you give).
 
     The first key=value arguments are used to customize the Mako variables that
     are shown with --show-defaults. The following settings will be
@@ -50,39 +52,11 @@ class Command(BasePasterCommand):
     is specified and change where the following values go.
     """
 
-    takes_config_file = False # at least not an existing one ...
-
-    def take_action(self, args):
-        _run(args)
-
-    def get_parser(self, prog_name):
-        parser = super(Command, self).get_parser(prog_name)
-
-        parser.add_argument('config_file', nargs='?',
-            help='application config file to write')
-
-        parser.add_argument('custom', nargs=argparse.REMAINDER,
-            help='custom values for the config file')
-
-        parser.add_argument('--show-defaults', action='store_true',
-            help="Show the default values that can be overridden")
-
-        return parser
-
-
-def _run(args):
-    if args.config_file is None:
-        if not args.show_defaults:
-            raise ValueError("Missing argument: config_file")
-    else:
-        if args.show_defaults:
-            raise ValueError("Can't specify both config_file and --show_defaults")
-
     mako_variable_values = {}
     ini_settings = defaultdict(dict)
 
     section_name = None
-    for parameter in args.custom:
+    for parameter in key_value_pairs:
         parts = parameter.split('=', 1)
         if len(parts) == 1 and parameter.startswith('[') and parameter.endswith(']'):
             section_name = parameter
@@ -97,20 +71,14 @@ def _run(args):
         else:
             raise ValueError("Invalid name=value parameter %r" % parameter)
 
-    if args.show_defaults:
-        for key, value in inifile.default_variables.items():
-            value = mako_variable_values.get(key, value)
-            print '%s=%s' % (key, value)
-        sys.exit(0)
-
     # use default that cannot be replaced
     mako_variable_values.update({
         'uuid': lambda: uuid.uuid4().hex,
     })
     try:
-        config_file = os.path.abspath(args.config_file)
-        inifile.create(config_file, mako_variable_values, ini_settings)
-        print 'Wrote new config file in %s' % config_file
+        config_file_abs = os.path.abspath(config_file)
+        inifile.create(config_file_abs, mako_variable_values, ini_settings)
+        click.echo('Wrote new config file in %s' % config_file_abs)
 
     except Exception:
-        print mako.exceptions.text_error_template().render()
+        click.echo(mako.exceptions.text_error_template().render())
diff --git a/setup.py b/setup.py
--- a/setup.py
+++ b/setup.py
@@ -163,7 +163,6 @@ setuptools.setup(
     cleanup-repos=kallithea.lib.paster_commands.cleanup:Command
     install-iis=kallithea.lib.paster_commands.install_iis:Command
     ishell=kallithea.lib.paster_commands.ishell:Command
-    make-config=kallithea.lib.paster_commands.make_config:Command
     make-index=kallithea.lib.paster_commands.make_index:Command
     make-rcext=kallithea.lib.paster_commands.make_rcextensions:Command
     repo-scan=kallithea.lib.paster_commands.repo_scan:Command


More information about the kallithea-general mailing list