Skip to content

Commit

Permalink
Port the --debug argument to use a callback mechanism as --url does
Browse files Browse the repository at this point in the history
Signed-off-by: Pierre-Yves Chibon <[email protected]>
  • Loading branch information
pypingou committed Jul 30, 2018
1 parent d7f490c commit 5729a49
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 20 deletions.
65 changes: 47 additions & 18 deletions bodhi/client/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,37 @@ def _warn_if_url_and_staging_set(ctx, param, value):
return value


def _set_logging_debug(ctx, param, value):
"""
Set up the logging level to "debug".
This allows us to print more information on the user's screen and thus helps following what is
going on.
Args:
ctx (click.core.Context): The Click context. Unused.
param (click.core.Option): The option being handled. Unused.
value (unicode): The value of the --debug flag.
Returns:
unicode: The value of the --debug flag.
"""
if value:
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
log.addHandler(ch)
log.setLevel(logging.DEBUG)
return value


url_option = click.option('--url', envvar='BODHI_URL', default=bindings.BASE_URL,
help=('URL of a Bodhi server. Ignored if --staging is set. Can be set '
'with BODHI_URL environment variable'),
callback=_warn_if_url_and_staging_set)
staging_option = click.option('--staging', help='Use the staging bodhi instance',
is_flag=True, default=False)

debug_option = click.option('--debug', help='Display debugging information.',
is_flag=True, default=False,
callback=_set_logging_debug)

new_edit_options = [
click.option('--user'),
Expand Down Expand Up @@ -98,7 +122,8 @@ def _warn_if_url_and_staging_set(ctx, param, value):
click.option('--user'),
click.option('--password', hide_input=True),
staging_option,
url_option]
url_option,
debug_option]


# Basic options for pagination of query result
Expand Down Expand Up @@ -137,7 +162,8 @@ def _warn_if_url_and_staging_set(ctx, param, value):
'archived']),
help='The state of the release'),
staging_option,
url_option]
url_option,
debug_option]


def add_options(options):
Expand Down Expand Up @@ -226,17 +252,11 @@ def _save_override(url, user, password, staging, edit=False, **kwargs):

@click.group()
@click.version_option(message='%(version)s')
@click.option('--debug', is_flag=True, default=False, help='Display debugging information.')
def cli(debug):
def cli():
# Docs that show in the --help
"""Command line tool for interacting with Bodhi."""
# Developer Docs
"""Create the main CLI group."""

ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
log.addHandler(ch)
log.setLevel(logging.DEBUG)
pass # pragma: no cover


Expand All @@ -254,7 +274,8 @@ def composes():
@staging_option
@click.option('-v', '--verbose', is_flag=True, default=False, help='Display more information.')
@url_option
def list_composes(url, staging, verbose):
@debug_option
def list_composes(url, staging, verbose, debug):
# User docs for the CLI
"""
List composes.
Expand Down Expand Up @@ -288,7 +309,8 @@ def updates():
@click.option('--file', help='A text file containing all the update details')
@handle_errors
@url_option
def new(user, password, url, **kwargs):
@debug_option
def new(user, password, url, debug, **kwargs):
# User Docs that show in the --help
"""
Create a new update.
Expand All @@ -305,7 +327,6 @@ def new(user, password, url, **kwargs):
True.
kwargs (dict): Other keyword arguments passed to us by click.
"""

client = bindings.BodhiClient(base_url=url, username=user, password=password,
staging=kwargs['staging'])

Expand Down Expand Up @@ -356,8 +377,9 @@ def _validate_edit_update(ctx, param, value):
@add_options(new_edit_options)
@click.argument('update', callback=_validate_edit_update)
@url_option
@debug_option
@handle_errors
def edit(user, password, url, **kwargs):
def edit(user, password, url, debug, **kwargs):
# User Docs that show in the --help
"""
Edit an existing update.
Expand Down Expand Up @@ -442,9 +464,10 @@ def edit(user, password, url, **kwargs):
@click.option('--mine', is_flag=True, help='Show only your updates')
@staging_option
@url_option
@debug_option
@add_options(pagination_options)
@handle_errors
def query(url, mine=False, rows=None, **kwargs):
def query(url, mine=False, rows=None, debug=False, **kwargs):
# User Docs that show in the --help
"""Query updates on Bodhi.
Expand All @@ -461,10 +484,10 @@ def query(url, mine=False, rows=None, **kwargs):
url (unicode): The URL of a Bodhi server to create the update on. Ignored if staging is
True.
mine (Boolean): If the --mine flag was set
debug (Boolean): If the --debug flag was set
kwargs (dict): Other keyword arguments passed to us by click.
"""
client = bindings.BodhiClient(base_url=url, staging=kwargs['staging'])

if mine:
client.init_username()
kwargs['user'] = client.username
Expand All @@ -479,6 +502,7 @@ def query(url, mine=False, rows=None, **kwargs):
@click.option('--password', hide_input=True)
@staging_option
@url_option
@debug_option
@handle_errors
def request(update, state, user, password, url, **kwargs):
# User Docs that show in the --help
Expand Down Expand Up @@ -524,6 +548,7 @@ def request(update, state, user, password, url, **kwargs):
@click.option('--password', hide_input=True)
@staging_option
@url_option
@debug_option
@handle_errors
def comment(update, text, karma, user, password, url, **kwargs):
# User Docs that show in the --help
Expand Down Expand Up @@ -564,6 +589,7 @@ def comment(update, text, karma, user, password, url, **kwargs):
@click.option('--updateid', help='Download update(s) by ID(s) (comma-separated list)')
@click.option('--builds', help='Download update(s) by build NVR(s) (comma-separated list)')
@url_option
@debug_option
@handle_errors
def download(url, **kwargs):
# User Docs that show in the --help
Expand Down Expand Up @@ -662,6 +688,7 @@ def _get_notes(**kwargs):
"requirements, specify --test=all")
@staging_option
@url_option
@debug_option
@handle_errors
def waive(update, show, test, comment, url, **kwargs):
# User Docs that show in the --help
Expand Down Expand Up @@ -747,6 +774,7 @@ def overrides():
@click.option('--builds', default=None,
help='Query by comma-separated build id(s)')
@url_option
@debug_option
@add_options(pagination_options)
@handle_errors
def query_buildroot_overrides(url, user=None, mine=False, packages=None,
Expand Down Expand Up @@ -930,7 +958,7 @@ def releases():
@releases.command(name='create')
@handle_errors
@add_options(release_options)
def create_release(user, password, url, **kwargs):
def create_release(user, password, url, debug, **kwargs):
"""Create a release."""
client = bindings.BodhiClient(base_url=url, username=user, password=password,
staging=kwargs['staging'])
Expand All @@ -943,7 +971,7 @@ def create_release(user, password, url, **kwargs):
@handle_errors
@add_options(release_options)
@click.option('--new-name', help='New release name (eg: F20)')
def edit_release(user, password, url, **kwargs):
def edit_release(user, password, url, debug, **kwargs):
"""Edit an existing release."""
client = bindings.BodhiClient(base_url=url, username=user, password=password,
staging=kwargs['staging'])
Expand Down Expand Up @@ -981,6 +1009,7 @@ def edit_release(user, password, url, **kwargs):
@handle_errors
@click.argument('name')
@url_option
@debug_option
@staging_option
def info_release(name, url, **kwargs):
"""Retrieve and print info about a named release."""
Expand Down
4 changes: 2 additions & 2 deletions bodhi/tests/client/test___init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,8 +335,8 @@ def test_severity_and_debug_flags(self, send_request):
runner = testing.CliRunner()

result = runner.invoke(
client.cli,
['--debug', 'updates', 'new', '--user', 'bowlofeggs', '--password', 's3kr3t',
client.new,
['--debug', '--user', 'bowlofeggs', '--password', 's3kr3t',
'--autokarma', 'bodhi-2.2.4-1.el7', '--severity', 'urgent', '--notes',
'No description.'])

Expand Down

0 comments on commit 5729a49

Please sign in to comment.