Skip to content

Commit

Permalink
cli: expose utility function die() to all commands
Browse files Browse the repository at this point in the history
  • Loading branch information
ael-code committed Mar 19, 2016
1 parent e4cafb1 commit 638d4ad
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 21 deletions.
2 changes: 1 addition & 1 deletion cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down
6 changes: 3 additions & 3 deletions cli/libreant.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
14 changes: 5 additions & 9 deletions cli/libreant_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")
Expand All @@ -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)
Expand All @@ -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")
Expand All @@ -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)
Expand Down
11 changes: 3 additions & 8 deletions cli/libreant_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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')
Expand All @@ -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
Expand All @@ -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):
Expand Down

0 comments on commit 638d4ad

Please sign in to comment.