changeset 7406:7784a1212471

cli: convert 'gearbox make-config' into 'kallithea-cli config-create' ... and update documentation.
author Thomas De Schampheleire <thomas.de_schampheleire@nokia.com>
date Sun, 18 Nov 2018 20:02:17 +0100
parents e4b9a1d1fea1
children 0e0dbb16fcbf
files docs/contributing.rst docs/dev/dbmigrations.rst docs/installation_win.rst docs/installation_win_old.rst docs/setup.rst docs/upgrade.rst kallithea/bin/kallithea_cli.py kallithea/bin/kallithea_cli_config.py kallithea/lib/paster_commands/make_config.py setup.py
diffstat 10 files changed, 101 insertions(+), 120 deletions(-) [+]
line wrap: on
line diff
--- a/docs/contributing.rst	Sun Nov 18 20:02:17 2018 +0100
+++ b/docs/contributing.rst	Sun Nov 18 20:02:17 2018 +0100
@@ -39,7 +39,7 @@
         pip install --upgrade -r dev_requirements.txt
         npm install     # install dependencies - both tools and data
         npm run less    # for generating css from less
-        gearbox make-config my.ini
+        kallithea-cli config-create my.ini
         gearbox setup-db -c my.ini --user=user --email=user@example.com --password=password --repos=/tmp
         gearbox serve -c my.ini --reload &
         firefox http://127.0.0.1:5000/
--- a/docs/dev/dbmigrations.rst	Sun Nov 18 20:02:17 2018 +0100
+++ b/docs/dev/dbmigrations.rst	Sun Nov 18 20:02:17 2018 +0100
@@ -18,7 +18,7 @@
 2. Create a separate throwaway configuration for iterating on the actual
    database changes::
 
-    gearbox make-config temp.ini
+    kallithea-cli config-create temp.ini
 
    Edit the file to change database settings. SQLite is typically fine,
    but make sure to change the path to e.g. ``temp.db``, to avoid
--- a/docs/installation_win.rst	Sun Nov 18 20:02:17 2018 +0100
+++ b/docs/installation_win.rst	Sun Nov 18 20:02:17 2018 +0100
@@ -166,7 +166,7 @@
 one). When ready, type::
 
   cd C:\Kallithea\Bin
-  gearbox make-config my.ini
+  kallithea-cli config-create my.ini
 
 Then you must edit my.ini to fit your needs (IP address, IP
 port, mail settings, database, etc.). `NotePad++`__ or a similar text
--- a/docs/installation_win_old.rst	Sun Nov 18 20:02:17 2018 +0100
+++ b/docs/installation_win_old.rst	Sun Nov 18 20:02:17 2018 +0100
@@ -204,7 +204,7 @@
 "activate" one). When ready, just type::
 
   cd C:\Kallithea\Bin
-  gearbox make-config my.ini
+  kallithea-cli config-create my.ini
 
 Then, you must edit my.ini to fit your needs (network address and
 port, mail settings, database, whatever). I recommend using NotePad++
--- a/docs/setup.rst	Sun Nov 18 20:02:17 2018 +0100
+++ b/docs/setup.rst	Sun Nov 18 20:02:17 2018 +0100
@@ -27,14 +27,14 @@
 First, you will need to create a Kallithea configuration file. Run the
 following command to do so::
 
-    gearbox make-config my.ini
+    kallithea-cli config-create my.ini
 
 This will create the file ``my.ini`` in the current directory. This
 configuration file contains the various settings for Kallithea, e.g.
 proxy port, email settings, usage of static files, cache, Celery
 settings, and logging. Extra settings can be specified like::
 
-    gearbox make-config my.ini host=8.8.8.8 "[handler_console]" formatter=color_formatter
+    kallithea-cli config-create my.ini host=8.8.8.8 "[handler_console]" formatter=color_formatter
 
 Next, you need to create the databases used by Kallithea. It is recommended to
 use PostgreSQL or SQLite (default). If you choose a database other than the
--- a/docs/upgrade.rst	Sun Nov 18 20:02:17 2018 +0100
+++ b/docs/upgrade.rst	Sun Nov 18 20:02:17 2018 +0100
@@ -105,7 +105,7 @@
 
 Run the following command to create a new configuration (``.ini``) file::
 
-    gearbox make-config new.ini
+    kallithea-cli config-create new.ini
 
 Then compare it with your old config file and see what changed.
 
--- a/kallithea/bin/kallithea_cli.py	Sun Nov 18 20:02:17 2018 +0100
+++ b/kallithea/bin/kallithea_cli.py	Sun Nov 18 20:02:17 2018 +0100
@@ -14,3 +14,6 @@
 
 # 'cli' is the main entry point for 'kallithea-cli', specified in setup.py as entry_points console_scripts
 from kallithea.bin.kallithea_cli_base import cli
+
+# import commands (they will add themselves to the 'cli' object)
+import kallithea.bin.kallithea_cli_config
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/kallithea/bin/kallithea_cli_config.py	Sun Nov 18 20:02:17 2018 +0100
@@ -0,0 +1,91 @@
+# -*- coding: utf-8 -*-
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+import click
+import kallithea.bin.kallithea_cli_base as cli_base
+
+import os
+import uuid
+from collections import defaultdict
+
+import mako.exceptions
+
+from kallithea.lib import inifile
+
+def show_defaults(ctx, param, value):
+    # Following construct is taken from the Click documentation:
+    # https://click.palletsprojects.com/en/7.x/options/#callbacks-and-eager-options
+    # "The resilient_parsing flag is applied to the context if Click wants to
+    # parse the command line without any destructive behavior that would change
+    # the execution flow. In this case, because we would exit the program, we
+    # instead do nothing."
+    if not value or ctx.resilient_parsing:
+        return
+
+    for key, value in inifile.default_variables.items():
+        click.echo('%s=%s' % (key, value))
+
+    ctx.exit()
+
+@cli_base.register_command()
+@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')
+@click.argument('config_file', type=click.Path(dir_okay=False, writable=True), required=True)
+@click.argument('key_value_pairs', nargs=-1)
+def config_create(config_file, key_value_pairs):
+    """Create a new configuration file.
+
+    This command creates a default configuration file, possibly adding/updating
+    settings you specify.
+
+    The primary high level configuration keys and their default values are
+    shown with --show-defaults . Custom values for these keys can be specified
+    on the command line as key=value arguments.
+
+    Additional key=value arguments will be patched/inserted in the [app:main]
+    section ... until another section name specifies where any following values
+    should go.
+    """
+
+    mako_variable_values = {}
+    ini_settings = defaultdict(dict)
+
+    section_name = None
+    for parameter in key_value_pairs:
+        parts = parameter.split('=', 1)
+        if len(parts) == 1 and parameter.startswith('[') and parameter.endswith(']'):
+            section_name = parameter
+        elif len(parts) == 2:
+            key, value = parts
+            if section_name is None and key in inifile.default_variables:
+                mako_variable_values[key] = value
+            else:
+                if section_name is None:
+                    section_name = '[app:main]'
+                ini_settings[section_name][key] = value
+        else:
+            raise ValueError("Invalid name=value parameter %r" % parameter)
+
+    # use default that cannot be replaced
+    mako_variable_values.update({
+        'uuid': lambda: uuid.uuid4().hex,
+    })
+    try:
+        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:
+        click.echo(mako.exceptions.text_error_template().render())
--- a/kallithea/lib/paster_commands/make_config.py	Sun Nov 18 20:02:17 2018 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,112 +0,0 @@
-# -*- coding: utf-8 -*-
-# This program is free software: you can redistribute it and/or modify
-# it under the terms of the GNU General Public License as published by
-# the Free Software Foundation, either version 3 of the License, or
-# (at your option) any later version.
-#
-# This program is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-# GNU General Public License for more details.
-#
-# 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 os
-import sys
-import uuid
-import argparse
-from collections import defaultdict
-
-import mako.exceptions
-
-from kallithea.lib.paster_commands.common import BasePasterCommand
-from kallithea.lib import inifile
-
-
-class Command(BasePasterCommand):
-    """Kallithea: Create a new config file
-
-    make-config is the first part of the two step setup process. This first
-    step creates a customized .ini configuration file. The next step is to run
-    setup-db to populate the database that is referenced in the configuration
-    file.
-
-    The primary high level configuration keys and their default values are
-    shown with --show-defaults . Custom values can be specified on the command
-    line as key=value arguments when creating a config file.
-
-    Additional key=value arguments will be patched/inserted in the [app:main]
-    section ... until another section name specifies where any following values
-    should 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='"key=value" for customizing 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.show_defaults:
-        if args.config_file is not None:
-            raise ValueError("Can't specify both config file and --show-defaults")
-        for key, value in inifile.default_variables.items():
-            print '%s=%s' % (key, value)
-        sys.exit(0)
-    if args.config_file is None:
-        raise ValueError("Missing argument: config file")
-
-    mako_variable_values = {}
-    ini_settings = defaultdict(dict)
-
-    section_name = None
-    for parameter in args.custom:
-        parts = parameter.split('=', 1)
-        if len(parts) == 1 and parameter.startswith('[') and parameter.endswith(']'):
-            section_name = parameter
-        elif len(parts) == 2:
-            key, value = parts
-            if section_name is None and key in inifile.default_variables:
-                mako_variable_values[key] = value
-            else:
-                if section_name is None:
-                    section_name = '[app:main]'
-                ini_settings[section_name][key] = value
-        else:
-            raise ValueError("Invalid name=value parameter %r" % parameter)
-
-    # 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
-
-    except Exception:
-        print mako.exceptions.text_error_template().render()
--- a/setup.py	Sun Nov 18 20:02:17 2018 +0100
+++ b/setup.py	Sun Nov 18 20:02:17 2018 +0100
@@ -164,7 +164,6 @@
     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