From 638d4adcd70750bf4823c398dec4ef5156505e7e Mon Sep 17 00:00:00 2001 From: ael-code Date: Mon, 7 Mar 2016 13:09:52 +0100 Subject: [PATCH] cli: expose utility function die() to all commands --- cli/__init__.py | 2 +- cli/libreant.py | 6 +++--- cli/libreant_db.py | 14 +++++--------- cli/libreant_users.py | 11 +++-------- 4 files changed, 12 insertions(+), 21 deletions(-) diff --git a/cli/__init__.py b/cli/__init__.py index 130f32e..0ff5d18 100644 --- a/cli/__init__.py +++ b/cli/__init__.py @@ -3,7 +3,7 @@ def die(msg, exit_code=1, fg='red'): - click.secho('ERROR: ' + msg, fg=fg, err=True) + click.secho(msg, fg=fg, err=True) exit(exit_code) diff --git a/cli/libreant.py b/cli/libreant.py index 93362f6..71616a8 100644 --- a/cli/libreant.py +++ b/cli/libreant.py @@ -2,7 +2,7 @@ import logging import json -from . import load_cfg +from . import load_cfg, die from conf.defaults import get_def_conf, get_help from utils.loggers import initLoggers from webant.webant import main @@ -58,8 +58,8 @@ def libreant(settings, debug, port, address, fsdb_path, es_indexname, es_hosts, if conf.get('DEBUG', False): raise else: - click.secho(str(e), fg='yellow', err=True) - exit(1) + die(str(e)) + if __name__ == '__main__': libreant() diff --git a/cli/libreant_db.py b/cli/libreant_db.py index ee81db0..08f9017 100644 --- a/cli/libreant_db.py +++ b/cli/libreant_db.py @@ -4,7 +4,7 @@ import os import mimetypes -from . import load_cfg +from . import load_cfg, die from archivant import Archivant from archivant.exceptions import NotFoundException from conf.defaults import get_def_conf, get_help @@ -47,8 +47,7 @@ def libreant_db(debug, settings, fsdb_path, es_indexname, es_hosts): if conf.get('DEBUG', False): raise else: - click.secho(str(e), fg='yellow', err=True) - exit(1) + die(str(e)) @libreant_db.command(name="export-volume", help="export a volume") @@ -58,8 +57,7 @@ def export_volume(volumeid, pretty): try: volume = arc.get_volume(volumeid) except NotFoundException as e: - click.secho(str(e), fg="yellow", err=True) - exit(4) + die(str(e), fg="yellow", exit_code=4) indent = 3 if pretty else None ouput = json.dumps(volume, indent=indent) @@ -72,8 +70,7 @@ def delete_volume(volumeid): try: arc.delete_volume(volumeid) except NotFoundException as e: - click.secho(str(e), fg="yellow", err=True) - exit(4) + die(str(e), fg="yellow", exit_code=4) @libreant_db.command(help="search volumes by query") @@ -83,8 +80,7 @@ def search(query, pretty): results = arc._db.user_search(query)['hits']['hits'] results = map(arc.normalize_volume, results) if not results: - click.secho("No results found for '{}'".format(query), fg="yellow", err=True) - exit(4) + die("No results found for '{}'".format(query), fg="yellow", exit_code=4) indent = 3 if pretty else None output = json.dumps(results, indent=indent) click.echo(output) diff --git a/cli/libreant_users.py b/cli/libreant_users.py index ab5e5fe..773e6d5 100644 --- a/cli/libreant_users.py +++ b/cli/libreant_users.py @@ -5,7 +5,7 @@ import users.api import users -from . import load_cfg +from . import load_cfg, die from conf.defaults import get_def_conf, get_help from utils.loggers import initLoggers @@ -19,11 +19,6 @@ def pretty_json_dumps(*args, **kargs): return json.dumps(*args, **kargs) -def die(msg, exit_code=1): - click.secho('ERROR: ' + msg, err=True, fg='red') - exit(exit_code) - - @click.group(name="libreant-users", help="manage libreant users") @click.version_option() @click.option('-s', '--settings', type=click.Path(exists=True, readable=True), help='file from wich load settings') @@ -42,7 +37,7 @@ def libreant_users(debug, settings, users_db, pretty): cliConf['USERS_DATABASE'] = users_db conf.update(cliConf) if conf['USERS_DATABASE'] is None: - die('--users-db not set') + die('Error: --users-db not set') if pretty: global json_dumps json_dumps = pretty_json_dumps @@ -58,7 +53,7 @@ def libreant_users(debug, settings, users_db, pretty): if conf['DEBUG']: raise else: - die(str(e)) + die('Error: ' + str(e)) class ExistingUserType(click.ParamType):