@@ -267,6 +267,17 @@ def float_or_empty(value: str) -> t.Union[str, float]:
267267# Custom classes for commands and parameters
268268
269269
270+ def _check_allowed_contexts (
271+ ctx : click .Context , allowed_contexts : t .Optional [t .Tuple [t .Type [PulpEntityContext ]]]
272+ ) -> bool :
273+ if allowed_contexts is None :
274+ return True
275+ for ctx_type in allowed_contexts :
276+ if ctx .find_object (ctx_type ) is not None :
277+ return True
278+ return False
279+
280+
270281class PulpCommand (click .Command ):
271282 def __init__ (
272283 self ,
@@ -308,10 +319,10 @@ def get_params(self, ctx: click.Context) -> t.List[click.Parameter]:
308319 params = super ().get_params (ctx )
309320 new_params : t .List [click .Parameter ] = []
310321 for param in params :
311- if isinstance (param , PulpOption ):
312- if param .allowed_with_contexts is not None :
313- if not isinstance ( ctx . obj , param . allowed_with_contexts ):
314- continue
322+ if isinstance (param , PulpOption ) and not _check_allowed_contexts (
323+ ctx , param .allowed_with_contexts
324+ ):
325+ continue
315326 new_params .append (param )
316327 return new_params
317328
@@ -323,10 +334,8 @@ class PulpGroup(PulpCommand, click.Group):
323334 def get_command (self , ctx : click .Context , cmd_name : str ) -> t .Optional [click .Command ]:
324335 # Overwriting this removes the command from the help message and from being callable
325336 cmd = super ().get_command (ctx , cmd_name )
326- if (
327- isinstance (cmd , (PulpCommand , PulpGroup ))
328- and cmd .allowed_with_contexts is not None
329- and not isinstance (ctx .obj , cmd .allowed_with_contexts )
337+ if isinstance (cmd , (PulpCommand , PulpGroup )) and not _check_allowed_contexts (
338+ ctx , cmd .allowed_with_contexts
330339 ):
331340 raise IncompatibleContext (
332341 _ ("The subcommand '{name}' is not available in this context." ).format (name = cmd .name )
@@ -337,10 +346,8 @@ def list_commands(self, ctx: click.Context) -> t.List[str]:
337346 commands_filtered_by_context = []
338347
339348 for name , cmd in self .commands .items ():
340- if (
341- isinstance (cmd , (PulpCommand , PulpGroup ))
342- and cmd .allowed_with_contexts is not None
343- and not isinstance (ctx .obj , cmd .allowed_with_contexts )
349+ if isinstance (cmd , (PulpCommand , PulpGroup )) and not _check_allowed_contexts (
350+ ctx , cmd .allowed_with_contexts
344351 ):
345352 continue
346353 commands_filtered_by_context .append (name )
@@ -1530,6 +1537,10 @@ def repository_content_command(**kwargs: t.Any) -> click.Group:
15301537 """A factory that creates a repository content command group."""
15311538
15321539 content_contexts = kwargs .pop ("contexts" , {})
1540+ list_kwargs = kwargs .pop ("list_kwargs" , {})
1541+ add_kwargs = kwargs .pop ("add_kwargs" , {})
1542+ remove_kwargs = kwargs .pop ("remove_kwargs" , {})
1543+ modify_kwargs = kwargs .pop ("modify_kwargs" , {})
15331544
15341545 def version_callback (
15351546 ctx : click .Context , param : click .Parameter , value : t .Optional [int ]
@@ -1540,7 +1551,7 @@ def version_callback(
15401551
15411552 # This is a mypy bug getting confused with positional args
15421553 # https://github.com/python/mypy/issues/15037
1543- @pulp_command ("list" ) # type: ignore [arg-type]
1554+ @pulp_command ("list" , ** list_kwargs ) # type: ignore [arg-type]
15441555 @click .option ("--all-types" , is_flag = True )
15451556 @limit_option
15461557 @offset_option
@@ -1563,7 +1574,7 @@ def content_list(
15631574 result = content_ctx .list (limit = limit , offset = offset , parameters = parameters )
15641575 pulp_ctx .output_result (result )
15651576
1566- @pulp_command ("add" )
1577+ @pulp_command ("add" , ** add_kwargs )
15671578 @repository_option
15681579 @click .option ("--base-version" , type = int , callback = version_callback )
15691580 @pass_content_context
@@ -1574,7 +1585,7 @@ def content_add(
15741585 repo_ctx = base_version .repository_ctx
15751586 repo_ctx .modify (add_content = [content_ctx .pulp_href ], base_version = base_version .pulp_href )
15761587
1577- @pulp_command ("remove" )
1588+ @pulp_command ("remove" , ** remove_kwargs )
15781589 @click .option ("--all" , is_flag = True , help = _ ("Remove all content from repository version" ))
15791590 @repository_option
15801591 @click .option ("--base-version" , type = int , callback = version_callback )
@@ -1588,7 +1599,7 @@ def content_remove(
15881599 remove_content = ["*" if all else content_ctx .pulp_href ]
15891600 repo_ctx .modify (remove_content = remove_content , base_version = base_version .pulp_href )
15901601
1591- @pulp_command ("modify" )
1602+ @pulp_command ("modify" , ** modify_kwargs )
15921603 @repository_option
15931604 @click .option ("--base-version" , type = int , callback = version_callback )
15941605 def content_modify (
0 commit comments