Merge "Added EGroup click group which can handle errors"
This commit is contained in:
commit
0f28333157
@ -38,3 +38,15 @@ class AliasedGroup(click.Group):
|
||||
elif len(matches) == 1:
|
||||
return click.Group.get_command(self, ctx, matches[0])
|
||||
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)
|
||||
|
@ -19,6 +19,7 @@ On create "golden" resource should be moved to special place
|
||||
|
||||
import collections
|
||||
import json
|
||||
import os
|
||||
|
||||
import click
|
||||
from fabric import api as fabric_api
|
||||
@ -58,8 +59,11 @@ def show_emitter_connections(res):
|
||||
|
||||
|
||||
@click.group(cls=base.AliasedGroup)
|
||||
def main():
|
||||
pass
|
||||
@click.option('--debug/--no-debug', default=False)
|
||||
def main(debug):
|
||||
debug = debug or os.getenv("SOLAR_CLI_DEBUG")
|
||||
if not debug:
|
||||
base.EGroup.error_wrapper_enabled = True
|
||||
|
||||
|
||||
def init_actions():
|
||||
|
@ -13,14 +13,35 @@
|
||||
# under the License.
|
||||
|
||||
import click
|
||||
from functools import wraps
|
||||
import os
|
||||
import yaml
|
||||
|
||||
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():
|
||||
pass
|
||||
|
||||
@ -56,14 +77,10 @@ def _import(name, source, link):
|
||||
if name is None:
|
||||
name = os.path.split(source)[-1]
|
||||
repo = Repository(name)
|
||||
try:
|
||||
repo.create(source, link)
|
||||
except RepositoryExists as e:
|
||||
click.echo(click.style(str(e), fg='red'))
|
||||
else:
|
||||
cnt = len(list(repo.iter_contents()))
|
||||
click.echo(
|
||||
"Created new repository with {} resources".format(cnt))
|
||||
repo.create(source, link)
|
||||
cnt = len(list(repo.iter_contents()))
|
||||
click.echo(
|
||||
"Created new repository with {} resources".format(cnt))
|
||||
|
||||
|
||||
@repository.command(help="Updates existing repository with new content")
|
||||
|
Loading…
x
Reference in New Issue
Block a user