Merge "Added EGroup click group which can handle errors"

This commit is contained in:
Jenkins 2016-01-21 14:34:17 +00:00 committed by Gerrit Code Review
commit 0f28333157
3 changed files with 45 additions and 12 deletions

View File

@ -38,3 +38,15 @@ class AliasedGroup(click.Group):
elif len(matches) == 1: elif len(matches) == 1:
return click.Group.get_command(self, ctx, matches[0]) return click.Group.get_command(self, ctx, matches[0])
ctx.fail('Too many matches: %s' % ', '.join(sorted(matches))) ctx.fail('Too many matches: %s' % ', '.join(sorted(matches)))
class EGroup(click.Group):
error_wrapper_enabled = False
error_wrapper = None
def add_command(self, cmd, name=None):
if self.error_wrapper:
cmd.callback = self.error_wrapper(cmd.callback)
return super(EGroup, self).add_command(cmd, name)

View File

@ -19,6 +19,7 @@ On create "golden" resource should be moved to special place
import collections import collections
import json import json
import os
import click import click
from fabric import api as fabric_api from fabric import api as fabric_api
@ -58,8 +59,11 @@ def show_emitter_connections(res):
@click.group(cls=base.AliasedGroup) @click.group(cls=base.AliasedGroup)
def main(): @click.option('--debug/--no-debug', default=False)
pass def main(debug):
debug = debug or os.getenv("SOLAR_CLI_DEBUG")
if not debug:
base.EGroup.error_wrapper_enabled = True
def init_actions(): def init_actions():

View File

@ -13,14 +13,35 @@
# under the License. # under the License.
import click import click
from functools import wraps
import os import os
import yaml import yaml
from solar.core.resource.repository import Repository from solar.core.resource.repository import Repository
from solar.core.resource.repository import RepositoryExists from solar.core.resource.repository import RepositoryException
from solar.cli.base import EGroup
@click.group(help="Manages Solar repositories") class RepoGroup(EGroup):
def error_wrapper(self, f):
@wraps(f)
def _in(*args, **kwargs):
try:
return f(*args, **kwargs)
except OSError as e:
if self.error_wrapper_enabled:
raise click.ClickException(str(e))
raise
except RepositoryException as e:
if self.error_wrapper_enabled:
raise click.ClickException(str(e))
raise
return _in
@click.group(help="Manages Solar repositories", cls=RepoGroup)
def repository(): def repository():
pass pass
@ -56,14 +77,10 @@ def _import(name, source, link):
if name is None: if name is None:
name = os.path.split(source)[-1] name = os.path.split(source)[-1]
repo = Repository(name) repo = Repository(name)
try: repo.create(source, link)
repo.create(source, link) cnt = len(list(repo.iter_contents()))
except RepositoryExists as e: click.echo(
click.echo(click.style(str(e), fg='red')) "Created new repository with {} resources".format(cnt))
else:
cnt = len(list(repo.iter_contents()))
click.echo(
"Created new repository with {} resources".format(cnt))
@repository.command(help="Updates existing repository with new content") @repository.command(help="Updates existing repository with new content")