Skip to content

Commit b66e434

Browse files
committed
WIP
fixes #1315
1 parent a0e036d commit b66e434

8 files changed

Lines changed: 63 additions & 66 deletions

File tree

CHANGES/1315.bugfix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixed warnings about parameters being used multiple times.

src/pulp_cli/generic.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,7 @@ def __init__(
399399
):
400400
self.allowed_with_contexts = allowed_with_contexts
401401
self.needs_plugins = needs_plugins
402+
self.option_processors: list[t.Callable[[click.Context], None]] = []
402403
super().__init__(*args, **kwargs)
403404

404405
def invoke(self, ctx: click.Context) -> t.Any:
@@ -408,6 +409,8 @@ def invoke(self, ctx: click.Context) -> t.Any:
408409
assert pulp_ctx is not None
409410
for plugin_requirement in self.needs_plugins:
410411
pulp_ctx.needs_plugin(plugin_requirement)
412+
for processor in self.option_processors:
413+
processor(ctx)
411414
return super().invoke(ctx)
412415
except PulpException as e:
413416
raise click.ClickException(str(e))
@@ -483,6 +486,16 @@ def pulp_command(
483486
return click.command(name=name, cls=PulpCommand, **kwargs)
484487

485488

489+
def option_processor(
490+
*, callback: t.Callable[[click.Context], None]
491+
) -> t.Callable[[PulpCommand], PulpCommand]:
492+
def _inner(cmd: PulpCommand) -> PulpCommand:
493+
cmd.option_processors.append(callback)
494+
return cmd
495+
496+
return _inner
497+
498+
486499
def pulp_group(name: str | None = None, **kwargs: t.Any) -> t.Callable[[_AnyCallable], PulpGroup]:
487500
"""
488501
Pulp command group factory.

src/pulpcore/cli/ansible/remote.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,6 @@ def remote(ctx: click.Context, pulp_ctx: PulpCLIContext, /, remote_type: str) ->
6767
nested_lookup_options = [remote_lookup_option]
6868
remote_options = [
6969
click.option("--policy", help=_("policy to use when downloading")),
70-
]
71-
collection_options = [
7270
pulp_option(
7371
"--requirements-file",
7472
callback=yaml_callback,
@@ -96,9 +94,8 @@ def remote(ctx: click.Context, pulp_ctx: PulpCLIContext, /, remote_type: str) ->
9694
allowed_with_contexts=collection_context,
9795
),
9896
]
99-
ansible_remote_options = remote_options + collection_options
100-
create_options = common_remote_create_options + remote_options + ansible_remote_options
101-
update_options = common_remote_update_options + remote_options + ansible_remote_options
97+
create_options = common_remote_create_options + remote_options
98+
update_options = common_remote_update_options + remote_options
10299

103100
remote.add_command(list_command(decorators=remote_filter_options))
104101
remote.add_command(show_command(decorators=lookup_options))

src/pulpcore/cli/container/content.py

Lines changed: 15 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,21 @@
11
import gettext
2-
import typing as t
32

43
import click
54

6-
from pulp_glue.common.context import PluginRequirement, PulpEntityContext
5+
from pulp_glue.common.context import PluginRequirement, PulpContentContext
76
from pulp_glue.container.context import (
87
PulpContainerBlobContext,
98
PulpContainerManifestContext,
109
PulpContainerTagContext,
1110
)
1211

1312
from pulp_cli.generic import (
14-
GroupOption,
1513
PulpCLIContext,
1614
content_filter_options,
1715
href_option,
1816
label_command,
1917
list_command,
18+
option_processor,
2019
pass_pulp_context,
2120
pulp_group,
2221
pulp_option,
@@ -26,14 +25,18 @@
2625
_ = gettext.gettext
2726

2827

29-
def _content_callback(ctx: click.Context, param: click.Parameter, value: t.Any) -> None:
30-
if value is not None:
31-
entity_ctx = ctx.find_object(PulpEntityContext)
28+
def _content_callback(ctx: click.Context) -> None:
29+
lookup = {
30+
key: value
31+
for key, value in ((key, ctx.params.pop(key, None)) for key in ["name", "digest"])
32+
if value is not None
33+
}
34+
if lookup:
35+
entity_ctx = ctx.find_object(PulpContentContext)
3236
assert entity_ctx is not None
33-
if isinstance(entity_ctx, PulpContainerTagContext):
34-
entity_ctx.entity = value
35-
else:
36-
entity_ctx.entity = {"digest": value}
37+
if isinstance(entity_ctx, PulpContainerTagContext) and len(lookup) != 2:
38+
raise click.UsageError(_("Both 'name' and 'digest' are needed to describe a tag."))
39+
entity_ctx.entity = lookup
3740

3841

3942
@pulp_group()
@@ -98,33 +101,15 @@ def content(ctx: click.Context, pulp_ctx: PulpCLIContext, /, content_type: str)
98101
lookup_options = [
99102
pulp_option(
100103
"--digest",
101-
expose_value=False,
102104
help=_("Digest associated with {entity}"),
103-
callback=_content_callback,
104-
allowed_with_contexts=(PulpContainerBlobContext, PulpContainerManifestContext),
105105
),
106-
click.option(
107-
"--digest",
108-
expose_value=False,
109-
help=_("Digest associated with {entity}"),
110-
callback=_content_callback,
111-
allowed_with_contexts=(PulpContainerTagContext,),
112-
group=[
113-
"name",
114-
],
115-
cls=GroupOption,
116-
),
117-
click.option(
106+
pulp_option(
118107
"--name",
119-
expose_value=False,
120108
help=_("Name of {entity}"),
121109
allowed_with_contexts=(PulpContainerTagContext,),
122-
group=[
123-
"digest",
124-
],
125-
cls=GroupOption,
126110
),
127111
href_option,
112+
option_processor(callback=_content_callback),
128113
]
129114

130115
content.add_command(list_command(decorators=list_options))

src/pulpcore/cli/core/content_guard.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,6 @@ def x509(ctx: click.Context, pulp_ctx: PulpCLIContext, /) -> None:
203203

204204

205205
certguard_create_options = common_create_options + [
206-
click.option("--description"),
207206
click.option(
208207
"--ca-certificate",
209208
help=_("a PEM encoded CA certificate or @file containing same"),
@@ -212,8 +211,7 @@ def x509(ctx: click.Context, pulp_ctx: PulpCLIContext, /) -> None:
212211
),
213212
]
214213

215-
certguard_update_options = lookup_options + [
216-
click.option("--description"),
214+
certguard_update_options = lookup_options + common_update_options + [
217215
click.option(
218216
"--ca-certificate",
219217
help=_("a PEM encoded CA certificate or @file containing same"),

src/pulpcore/cli/file/repository.py

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
)
1818

1919
from pulp_cli.generic import (
20-
GroupOption,
2120
PulpCLIContext,
2221
create_command,
2322
create_content_json_callback,
@@ -29,6 +28,7 @@
2928
list_command,
3029
load_file_wrapper,
3130
name_option,
31+
option_processor,
3232
pass_pulp_context,
3333
pass_repository_context,
3434
pulp_group,
@@ -60,12 +60,20 @@
6060
)
6161

6262

63-
def _content_callback(ctx: click.Context, param: click.Parameter, value: t.Any) -> t.Any:
64-
if value:
65-
pulp_ctx = ctx.find_object(PulpCLIContext)
66-
assert pulp_ctx is not None
67-
ctx.obj = PulpFileContentContext(pulp_ctx, entity=value)
68-
return value
63+
def _content_callback(ctx: click.Context) -> t.Any:
64+
lookup = {
65+
key: value
66+
for key, value in ((key, ctx.params.pop(key, None)) for key in ["sha256", "relative_path"])
67+
if value is not None
68+
}
69+
if lookup:
70+
if len(lookup) != 2:
71+
raise click.UsageError(
72+
_("Both 'sha256' and 'relative-path' must be provided to specify a file content.")
73+
)
74+
entity_ctx = ctx.find_object(PulpFileContentContext)
75+
assert entity_ctx is not None
76+
entity_ctx.entity = lookup
6977

7078

7179
CONTENT_LIST_SCHEMA = s.Schema([{"sha256": str, "relative_path": s.And(str, len)}])
@@ -119,15 +127,10 @@ def repository(ctx: click.Context, pulp_ctx: PulpCLIContext, /, repo_type: str)
119127
pulp_labels_option,
120128
]
121129
create_options = update_options + [click.option("--name", required=True)]
122-
file_options = [
123-
click.option("--sha256", cls=GroupOption, expose_value=False, group=["relative_path"]),
124-
click.option(
125-
"--relative-path",
126-
cls=GroupOption,
127-
expose_value=False,
128-
group=["sha256"],
129-
callback=_content_callback,
130-
),
130+
file_content_options = [
131+
click.option("--sha256"),
132+
click.option("--relative-path"),
133+
option_processor(callback=_content_callback),
131134
]
132135
content_json_callback = create_content_json_callback(
133136
PulpFileContentContext, schema=CONTENT_LIST_SCHEMA
@@ -155,7 +158,10 @@ def repository(ctx: click.Context, pulp_ctx: PulpCLIContext, /, repo_type: str)
155158

156159
repository.add_command(
157160
list_command(
158-
decorators=[label_select_option, click.option("--name-startswith", "name__startswith")]
161+
decorators=[
162+
label_select_option,
163+
click.option("--name-startswith", "name__startswith"),
164+
]
159165
)
160166
)
161167
repository.add_command(show_command(decorators=lookup_options))
@@ -170,8 +176,8 @@ def repository(ctx: click.Context, pulp_ctx: PulpCLIContext, /, repo_type: str)
170176
contexts={"file": PulpFileContentContext},
171177
base_default_plugin="file",
172178
base_default_type="file",
173-
add_decorators=file_options,
174-
remove_decorators=file_options,
179+
add_decorators=file_content_options,
180+
remove_decorators=file_content_options,
175181
modify_decorators=modify_options,
176182
)
177183
)

src/pulpcore/cli/rpm/content.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -354,15 +354,11 @@ def content() -> None:
354354
@pulp_option(
355355
"--file",
356356
type=click.File("rb"),
357-
required=True,
358-
help=_("An advisory JSON file."),
359-
allowed_with_contexts=(PulpRpmAdvisoryContext,),
360-
)
361-
@pulp_option(
362-
"--file",
363-
type=click.File("rb"),
364-
help=_("An RPM binary. One of --file or --directory is required."),
365-
allowed_with_contexts=(PulpRpmPackageContext,),
357+
help=_("A file to upload. One of --file or --directory is required."),
358+
allowed_with_contexts=(
359+
PulpRpmAdvisoryContext,
360+
PulpRpmPackageContext,
361+
),
366362
required=False,
367363
)
368364
@pulp_option(

tests/test_help_pages.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ def getter(self: t.Any) -> None:
5151

5252

5353
@pytest.mark.help_page(base_cmd=[])
54+
@pytest.mark.filterwarnings("error::UserWarning:click")
5455
def test_accessing_the_help_page_does_not_invoke_api(
5556
no_api: None,
5657
args: list[str],

0 commit comments

Comments
 (0)