Skip to content

Commit aebfa7d

Browse files
committed
Make widespead use of type_option
1 parent c1d71d2 commit aebfa7d

22 files changed

Lines changed: 135 additions & 344 deletions

src/pulpcore/cli/ansible/content.py

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
pulp_option,
3131
resource_option,
3232
show_command,
33+
type_option,
3334
)
3435

3536
translation = get_translation(__package__)
@@ -66,24 +67,15 @@ def _content_callback(ctx: click.Context, value: t.Any) -> t.Any:
6667

6768

6869
@pulp_group()
69-
@click.option(
70-
"-t",
71-
"--type",
72-
"content_type",
73-
type=click.Choice(["collection-version", "role", "signature"], case_sensitive=False),
74-
default="collection-version",
70+
@type_option(
71+
choices={
72+
"collection-version": PulpAnsibleCollectionVersionContext,
73+
"role": PulpAnsibleRoleContext,
74+
"signature": PulpAnsibleCollectionVersionSignatureContext,
75+
}
7576
)
76-
@pass_pulp_context
77-
@click.pass_context
78-
def content(ctx: click.Context, pulp_ctx: PulpCLIContext, /, content_type: str) -> None:
79-
if content_type == "collection-version":
80-
ctx.obj = PulpAnsibleCollectionVersionContext(pulp_ctx)
81-
elif content_type == "role":
82-
ctx.obj = PulpAnsibleRoleContext(pulp_ctx)
83-
elif content_type == "signature":
84-
ctx.obj = PulpAnsibleCollectionVersionSignatureContext(pulp_ctx)
85-
else:
86-
raise NotImplementedError()
77+
def content() -> None:
78+
pass
8779

8880

8981
list_options = [

src/pulpcore/cli/ansible/distribution.py

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
EntityDefinition,
66
EntityFieldDefinition,
77
PluginRequirement,
8-
PulpContext,
98
PulpEntityContext,
109
)
1110
from pulp_glue.common.i18n import get_translation
@@ -21,11 +20,11 @@
2120
list_command,
2221
name_option,
2322
pass_entity_context,
24-
pass_pulp_context,
2523
pulp_group,
2624
pulp_labels_option,
2725
resource_option,
2826
show_command,
27+
type_option,
2928
)
3029

3130
translation = get_translation(__package__)
@@ -42,20 +41,9 @@
4241

4342

4443
@pulp_group()
45-
@click.option(
46-
"-t",
47-
"--type",
48-
"distribution_type",
49-
type=click.Choice(["ansible"], case_sensitive=False),
50-
default="ansible",
51-
)
52-
@pass_pulp_context
53-
@click.pass_context
54-
def distribution(ctx: click.Context, pulp_ctx: PulpContext, /, distribution_type: str) -> None:
55-
if distribution_type == "ansible":
56-
ctx.obj = PulpAnsibleDistributionContext(pulp_ctx)
57-
else:
58-
raise NotImplementedError()
44+
@type_option(choices={"ansible": PulpAnsibleDistributionContext})
45+
def distribution() -> None:
46+
pass
5947

6048

6149
lookup_options = [href_option, name_option, distribution_lookup_option]

src/pulpcore/cli/ansible/remote.py

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
from pulp_glue.common.i18n import get_translation
1111

1212
from pulp_cli.generic import (
13-
PulpCLIContext,
1413
common_remote_create_options,
1514
common_remote_update_options,
1615
create_command,
@@ -20,12 +19,12 @@
2019
list_command,
2120
load_file_wrapper,
2221
name_option,
23-
pass_pulp_context,
2422
pulp_group,
2523
pulp_option,
2624
remote_filter_options,
2725
remote_lookup_option,
2826
show_command,
27+
type_option,
2928
update_command,
3029
)
3130

@@ -43,23 +42,14 @@ def yaml_callback(ctx: click.Context, param: click.Parameter, value: t.Any) -> s
4342

4443

4544
@pulp_group()
46-
@click.option(
47-
"-t",
48-
"--type",
49-
"remote_type",
50-
type=click.Choice(["collection", "role"], case_sensitive=False),
51-
default="collection",
52-
is_eager=True,
45+
@type_option(
46+
choices={
47+
"collection": PulpAnsibleCollectionRemoteContext,
48+
"role": PulpAnsibleRoleRemoteContext,
49+
}
5350
)
54-
@pass_pulp_context
55-
@click.pass_context
56-
def remote(ctx: click.Context, pulp_ctx: PulpCLIContext, /, remote_type: str) -> None:
57-
if remote_type == "role":
58-
ctx.obj = PulpAnsibleRoleRemoteContext(pulp_ctx)
59-
elif remote_type == "collection":
60-
ctx.obj = PulpAnsibleCollectionRemoteContext(pulp_ctx)
61-
else:
62-
raise NotImplementedError()
51+
def remote() -> None:
52+
pass
6353

6454

6555
collection_context = (PulpAnsibleCollectionRemoteContext,)

src/pulpcore/cli/ansible/repository.py

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
load_string_callback,
3333
name_option,
3434
option_group,
35-
pass_pulp_context,
3635
pass_repository_context,
3736
pulp_group,
3837
pulp_labels_option,
@@ -43,6 +42,7 @@
4342
resource_option,
4443
retained_versions_option,
4544
show_command,
45+
type_option,
4646
update_command,
4747
version_command,
4848
)
@@ -68,7 +68,13 @@
6868

6969

7070
CONTENT_LIST_SCHEMA = s.Schema(
71-
[{"name": s.And(str, len), "namespace": s.And(str, len), "version": s.And(str, len)}]
71+
[
72+
{
73+
"name": s.And(str, len),
74+
"namespace": s.And(str, len),
75+
"version": s.And(str, len),
76+
}
77+
]
7278
)
7379

7480

@@ -87,20 +93,9 @@ def _signing_service_callback(ctx: click.Context, param: click.Parameter, value:
8793

8894

8995
@pulp_group()
90-
@click.option(
91-
"-t",
92-
"--type",
93-
"repo_type",
94-
type=click.Choice(["ansible"], case_sensitive=False),
95-
default="ansible",
96-
)
97-
@pass_pulp_context
98-
@click.pass_context
99-
def repository(ctx: click.Context, pulp_ctx: PulpCLIContext, /, repo_type: str) -> None:
100-
if repo_type == "ansible":
101-
ctx.obj = PulpAnsibleRepositoryContext(pulp_ctx)
102-
else:
103-
raise NotImplementedError()
96+
@type_option(choices={"ansible": PulpAnsibleRepositoryContext})
97+
def repository() -> None:
98+
pass
10499

105100

106101
lookup_options = [href_option, name_option, repository_lookup_option]
@@ -112,7 +107,9 @@ def repository(ctx: click.Context, pulp_ctx: PulpCLIContext, /, repo_type: str)
112107
callback=load_string_callback,
113108
needs_plugins=[
114109
PluginRequirement(
115-
"ansible", specifier=">=0.15.0", feature="gpgkeys on ansible repositories"
110+
"ansible",
111+
specifier=">=0.15.0",
112+
feature="gpgkeys on ansible repositories",
116113
)
117114
],
118115
),

src/pulpcore/cli/container/content.py

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,15 @@
1111
)
1212

1313
from pulp_cli.generic import (
14-
PulpCLIContext,
1514
content_filter_options,
1615
href_option,
1716
label_command,
1817
list_command,
1918
option_group,
20-
pass_pulp_context,
2119
pulp_group,
2220
pulp_option,
2321
show_command,
22+
type_option,
2423
)
2524

2625
_ = gettext.gettext
@@ -36,24 +35,16 @@ def _content_callback(ctx: click.Context, value: t.Dict[str, t.Any]) -> None:
3635

3736

3837
@pulp_group()
39-
@click.option(
40-
"-t",
41-
"--type",
42-
"content_type",
43-
type=click.Choice(["blob", "manifest", "tag"], case_sensitive=False),
38+
@type_option(
39+
choices={
40+
"blob": PulpContainerBlobContext,
41+
"manifest": PulpContainerManifestContext,
42+
"tag": PulpContainerTagContext,
43+
},
4444
default="tag",
4545
)
46-
@pass_pulp_context
47-
@click.pass_context
48-
def content(ctx: click.Context, pulp_ctx: PulpCLIContext, /, content_type: str) -> None:
49-
if content_type == "manifest":
50-
ctx.obj = PulpContainerManifestContext(pulp_ctx)
51-
elif content_type == "tag":
52-
ctx.obj = PulpContainerTagContext(pulp_ctx)
53-
elif content_type == "blob":
54-
ctx.obj = PulpContainerBlobContext(pulp_ctx)
55-
else:
56-
raise NotImplementedError()
46+
def content() -> None:
47+
pass
5748

5849

5950
list_options = [

src/pulpcore/cli/container/distribution.py

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

1919
from pulp_cli.generic import (
20-
PulpCLIContext,
2120
content_guard_option,
2221
create_command,
2322
destroy_command,
@@ -28,12 +27,12 @@
2827
list_command,
2928
name_option,
3029
pass_entity_context,
31-
pass_pulp_context,
3230
pulp_group,
3331
pulp_labels_option,
3432
resource_option,
3533
role_command,
3634
show_command,
35+
type_option,
3736
)
3837

3938
translation = get_translation(__package__)
@@ -53,20 +52,9 @@
5352

5453

5554
@pulp_group()
56-
@click.option(
57-
"-t",
58-
"--type",
59-
"distribution_type",
60-
type=click.Choice(["container"], case_sensitive=False),
61-
default="container",
62-
)
63-
@pass_pulp_context
64-
@click.pass_context
65-
def distribution(ctx: click.Context, pulp_ctx: PulpCLIContext, /, distribution_type: str) -> None:
66-
if distribution_type == "container":
67-
ctx.obj = PulpContainerDistributionContext(pulp_ctx)
68-
else:
69-
raise NotImplementedError()
55+
@type_option(choices={"container": PulpContainerDistributionContext})
56+
def distribution() -> None:
57+
pass
7058

7159

7260
lookup_options = [href_option, name_option, distribution_lookup_option]

src/pulpcore/cli/container/namespace.py

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,10 @@
2828

2929

3030
@pulp_group()
31-
@click.option(
32-
"-t",
33-
"--type",
34-
"namespace_type",
35-
type=click.Choice(["container"], case_sensitive=False),
36-
default="container",
37-
)
3831
@pass_pulp_context
3932
@click.pass_context
40-
def namespace(ctx: click.Context, pulp_ctx: PulpCLIContext, /, namespace_type: str) -> None:
41-
if namespace_type == "container":
42-
ctx.obj = PulpContainerNamespaceContext(pulp_ctx)
43-
else:
44-
raise NotImplementedError()
33+
def namespace(ctx: click.Context, pulp_ctx: PulpCLIContext, /) -> None:
34+
ctx.obj = PulpContainerNamespaceContext(pulp_ctx)
4535

4636

4737
lookup_options = [href_option, name_option, namespace_lookup_option]

src/pulpcore/cli/container/remote.py

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
from pulp_glue.container.context import PulpContainerRemoteContext
55

66
from pulp_cli.generic import (
7-
PulpCLIContext,
87
common_remote_create_options,
98
common_remote_update_options,
109
create_command,
@@ -14,12 +13,12 @@
1413
list_command,
1514
load_json_callback,
1615
name_option,
17-
pass_pulp_context,
1816
pulp_group,
1917
remote_filter_options,
2018
remote_lookup_option,
2119
role_command,
2220
show_command,
21+
type_option,
2322
update_command,
2423
)
2524

@@ -28,27 +27,17 @@
2827

2928

3029
@pulp_group()
31-
@click.option(
32-
"-t",
33-
"--type",
34-
"remote_type",
35-
type=click.Choice(["container"], case_sensitive=False),
36-
default="container",
37-
)
38-
@pass_pulp_context
39-
@click.pass_context
40-
def remote(ctx: click.Context, pulp_ctx: PulpCLIContext, /, remote_type: str) -> None:
41-
if remote_type == "container":
42-
ctx.obj = PulpContainerRemoteContext(pulp_ctx)
43-
else:
44-
raise NotImplementedError()
30+
@type_option(choices={"container": PulpContainerRemoteContext})
31+
def remote() -> None:
32+
pass
4533

4634

4735
lookup_options = [href_option, name_option, remote_lookup_option]
4836
nested_lookup_options = [remote_lookup_option]
4937
remote_options = [
5038
click.option(
51-
"--policy", type=click.Choice(["immediate", "on_demand", "streamed"], case_sensitive=False)
39+
"--policy",
40+
type=click.Choice(["immediate", "on_demand", "streamed"], case_sensitive=False),
5241
),
5342
click.option("--include-tags", callback=load_json_callback),
5443
click.option("--exclude-tags", callback=load_json_callback),

src/pulpcore/cli/core/group.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,8 @@ def group(ctx: click.Context, pulp_ctx: PulpCLIContext, /) -> None:
105105
group.add_command(create_command(decorators=create_options))
106106
group.add_command(
107107
role_command(
108-
decorators=lookup_options, needs_plugins=[PluginRequirement("core", specifier=">=3.17.0")]
108+
decorators=lookup_options,
109+
needs_plugins=[PluginRequirement("core", specifier=">=3.17.0")],
109110
)
110111
)
111112

@@ -138,7 +139,8 @@ def permission(
138139

139140
permission.add_command(
140141
list_command(
141-
help=_("Show a list of the permissioons granted to a group."), decorators=[group_option]
142+
help=_("Show a list of the permissioons granted to a group."),
143+
decorators=[group_option],
142144
)
143145
)
144146

0 commit comments

Comments
 (0)