Skip to content

Commit

Permalink
Do not crash on unsuccessfull CLI results
Browse files Browse the repository at this point in the history
  • Loading branch information
smotornyuk committed Nov 29, 2018
1 parent 78a4022 commit e6a172f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 14 deletions.
7 changes: 6 additions & 1 deletion ckan/cli/__init__.py
Expand Up @@ -9,12 +9,17 @@
log = logging.getLogger(__name__)


def error_shout(exception):
click.secho(str(exception), fg='red', err=True)


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


def load_config(config=None):
Expand Down
28 changes: 17 additions & 11 deletions ckan/cli/db.py
Expand Up @@ -3,7 +3,8 @@
import logging

import click
from werkzeug.serving import run_simple

from ckan.cli import error_shout

log = logging.getLogger(__name__)

Expand All @@ -13,16 +14,17 @@ def db():
pass


@db.command(u'init', short_help=u'Initialaze the database')
@db.command(u'init', short_help=u'Initialize the database')
def initdb():
u'''Initialising the database'''
log.info(u"Initialize the Database")
try:
import ckan.model as model
model.repo.init_db()
except Exception as e:
click.secho(e, err=True)
print(u'Initialising DB: SUCCESS')
error_shout(e)
else:
click.secho(u'Initialising DB: SUCCESS', fg=u'green', bold=True)


PROMPT_MSG = u'This will delete all of your data!\nDo you want to continue?'
Expand All @@ -36,8 +38,9 @@ def cleandb():
import ckan.model as model
model.repo.clean_db()
except Exception as e:
click.secho(e, err=True)
click.secho(u'Cleaning DB: SUCCESS', color=u"green", bold=True)
error_shout(e)
else:
click.secho(u'Cleaning DB: SUCCESS', fg=u'green', bold=True)


@db.command(u'upgrade', short_help=u'Upgrade the database')
Expand All @@ -48,8 +51,9 @@ def updatedb(version=None):
import ckan.model as model
model.repo.upgrade_db(version)
except Exception as e:
click.secho(e, err=True)
click.secho(u'Upgrading DB: SUCCESS', fg=u'green', bold=True)
error_shout(e)
else:
click.secho(u'Upgrading DB: SUCCESS', fg=u'green', bold=True)


@db.command(u'version', short_help=u'Returns current version of data schema')
Expand All @@ -60,7 +64,9 @@ def version():
from ckan.model import Session
ver = Session.execute(u'select version from '
u'migrate_version;').fetchall()
click.secho(u"Latest data schema version: {0}".format(ver[0][0]),
fg=u"green", bold=True)
click.secho(
u"Latest data schema version: {0}".format(ver[0][0]),
bold=True
)
except Exception as e:
click.secho(e, err=True)
error_shout(e)
5 changes: 3 additions & 2 deletions ckan/cli/search_index.py
Expand Up @@ -4,7 +4,8 @@

import click
import sqlalchemy as sa
from werkzeug.serving import run_simple

from ckan.cli import error_shout


@click.group(name=u'search-index', short_help=u'Search index commands')
Expand Down Expand Up @@ -40,7 +41,7 @@ def rebuild(ctx, verbose, force, refresh, only_missing, quiet, commit_each):
defer_commit=(not commit_each),
quiet=quiet)
except Exception as e:
click.echo(e, err=True)
error_shout(e)
if not commit_each:
commit()

Expand Down

0 comments on commit e6a172f

Please sign in to comment.