1717)
1818
1919from pulp_cli .generic import (
20- GroupOption ,
2120 PulpCLIContext ,
2221 create_command ,
2322 create_content_json_callback ,
2928 list_command ,
3029 load_file_wrapper ,
3130 name_option ,
31+ option_processor ,
3232 pass_pulp_context ,
3333 pass_repository_context ,
3434 pulp_group ,
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
7179CONTENT_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]
121129create_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]
132135content_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
156159repository .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)
161167repository .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)
0 commit comments