Skip to content

Commit

Permalink
Fix [exclude-]fields in ansible content list
Browse files Browse the repository at this point in the history
Deprecated the plural --[exclude-]fields parameters and made the
default value compatible with the generic --[exclude-]field options.

fixes #602
  • Loading branch information
mdellweg committed Nov 15, 2022
1 parent bd521c1 commit 3583c49
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGES/602.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Deprecated `--fields` and `--exclude-fields` on `pulp ansible content list` in favor of `--[exclude-]field`.
52 changes: 47 additions & 5 deletions pulpcore/cli/ansible/content.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import IO, Any, Optional, Union
from typing import IO, Any, Callable, Optional, Union

import click

Expand Down Expand Up @@ -46,6 +46,40 @@ def _content_callback(ctx: click.Context, param: click.Parameter, value: Any) ->
return value


def _fields_callback(ctx: click.Context, param: click.Parameter, value: Any) -> Any:
if value:
click.echo(_("Option {name} is deprecated.").format(name=param.opts[0]), err=True)
value = tuple(value.split(","))
print(value)

return value


def _list_command_wrapper(list_command: click.Command) -> click.Command:
assert list_command.callback is not None
_old_callback: Callable[[Any], Any] = list_command.callback

def _new_callback(*args: Any, **kwargs: Any) -> Any:
_fields = kwargs.pop("_fields")
if _fields:
kwargs["fields"] += _fields
_exclude_fields = kwargs.pop("_exclude_fields")
if _exclude_fields:
kwargs["exclude_fields"] += _exclude_fields

if not kwargs["fields"] and not kwargs["exclude_fields"]:
kwargs["exclude_fields"] = (
"files",
"manifest",
"docs_blob",
)

return _old_callback(*args, **kwargs)

list_command.callback = _new_callback
return list_command


repository_option = resource_option(
"--repository",
default_plugin="ansible",
Expand Down Expand Up @@ -119,12 +153,20 @@ def content(ctx: click.Context, pulp_ctx: PulpContext, content_type: str) -> Non
),
]

# Deprecated fields options

fields_options = [
pulp_option("--fields", help=_("String list of fields to include in the result")),
pulp_option(
"--fields",
"_fields",
help=_("String list of fields to include in the result [DEPRECATED]"),
callback=_fields_callback,
),
pulp_option(
"--exclude-fields",
default="files,manifest,docs_blob",
help=_("String list of fields to exclude from result"),
"_exclude_fields",
help=_("String list of fields to exclude from result [DEPRECATED]"),
callback=_fields_callback,
),
]

Expand Down Expand Up @@ -175,7 +217,7 @@ def content(ctx: click.Context, pulp_ctx: PulpContext, content_type: str) -> Non
href_option,
]

content.add_command(list_command(decorators=list_options + fields_options))
content.add_command(_list_command_wrapper(list_command(decorators=list_options + fields_options)))
content.add_command(show_command(decorators=lookup_options))


Expand Down

0 comments on commit 3583c49

Please sign in to comment.