Skip to content

Commit

Permalink
Provide a factory for resource options
Browse files Browse the repository at this point in the history
The options accept resources by the "[[<plugin>:]<type>:]name" pattern.

fixes pulp#158
  • Loading branch information
mdellweg committed Mar 8, 2021
1 parent 1670114 commit 3c40ccd
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 14 deletions.
1 change: 1 addition & 0 deletions CHANGES/158.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
WIP: Change resource options to accept plugin and type along with the name.
50 changes: 49 additions & 1 deletion pulpcore/cli/common/generic.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import gettext
import json
from typing import Any, Optional, Tuple, Union
from typing import Any, Callable, Dict, Optional, Tuple, Type, TypeVar, Union

import click

Expand All @@ -19,6 +19,8 @@

_ = gettext.gettext

F = TypeVar("F")


class PulpCommand(click.Command):
def get_short_help_str(self, limit: int = 45) -> str:
Expand Down Expand Up @@ -127,6 +129,52 @@ def load_json_callback(
# Decorator common options


def resource_option(*args: Any, **kwargs: Any) -> Callable[[F], F]:
default_plugin: str = kwargs.pop("default_plugin", None)
default_type: str = kwargs.pop("default_type", None)
context_table: Dict[str, Type[PulpEntityContext]] = kwargs.pop("context_table")

def _option_callback(
ctx: click.Context, param: click.Parameter, value: Optional[str]
) -> Union[None, str, PulpEntityContext]:
# Pass None and "" verbatim
if value:
split_value = value.split(":", maxsplit=2)
if len(split_value) < 2:
if default_type is None:
raise click.ClickException(
_(
"A resource type must be specified with the {option_name} option."
).format(option_name=param.name)
)
split_value.insert(0, default_type)
if len(split_value) < 3:
if default_plugin is None:
raise click.ClickException(
_("A plugin must be specified with the {option_name} option.").format(
option_name=param.name
)
)
split_value.insert(0, default_plugin)
plugin, resource_type, name = split_value
context_class = context_table.get(plugin + ":" + resource_type)
if context_class is None:
raise click.ClickException(
_(
"The type '{plugin}:{resource_type}' "
"is not valid for the {option_name} option."
).format(plugin=plugin, resource_type=resource_type, option_name=param.name)
)
pulp_ctx: PulpContext = ctx.find_object(PulpContext)
return context_class(pulp_ctx, entity={"name": value})
return value

if "cls" not in kwargs:
kwargs["cls"] = PulpOption
kwargs["callback"] = _option_callback
return click.option(*args, **kwargs)


limit_option = click.option(
"--limit",
default=DEFAULT_LIMIT,
Expand Down
24 changes: 11 additions & 13 deletions pulpcore/cli/file/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
name_option,
repository_href_option,
repository_option,
resource_option,
show_command,
update_command,
version_command,
Expand All @@ -35,16 +36,6 @@
_ = gettext.gettext


def _remote_callback(
ctx: click.Context, param: click.Parameter, value: Optional[str]
) -> Optional[Union[str, PulpEntityContext]]:
# Pass None and "" verbatim
if value:
pulp_ctx: PulpContext = ctx.find_object(PulpContext)
return PulpFileRemoteContext(pulp_ctx, entity={"name": value})
return value


@click.group()
@click.option(
"-t",
Expand All @@ -62,16 +53,23 @@ def repository(ctx: click.Context, pulp_ctx: PulpContext, repo_type: str) -> Non
raise NotImplementedError()


remote_option = resource_option(
"--remote",
default_plugin="file",
default_type="file",
context_table={"file:file": PulpFileRemoteContext},
)

lookup_options = [href_option, name_option]
nested_lookup_options = [repository_href_option, repository_option]
create_options = [
click.option("--name", required=True),
click.option("--description"),
click.option("--remote", callback=_remote_callback),
remote_option,
]
update_options = [
click.option("--description"),
click.option("--remote", callback=_remote_callback),
remote_option,
]

repository.add_command(list_command(decorators=[label_select_option]))
Expand All @@ -87,7 +85,7 @@ def repository(ctx: click.Context, pulp_ctx: PulpContext, repo_type: str) -> Non
@repository.command()
@name_option
@href_option
@click.option("--remote", callback=_remote_callback)
@remote_option
@pass_repository_context
def sync(
repository_ctx: PulpRepositoryContext,
Expand Down

0 comments on commit 3c40ccd

Please sign in to comment.