This is an automated email from the ASF dual-hosted git repository.
potiuk pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/airflow.git
The following commit(s) were added to refs/heads/main by this push:
new 6e459b4b0b3 feat (airflowctl): Allow adding the same commands with
custom action commands (#49674)
6e459b4b0b3 is described below
commit 6e459b4b0b3985886f220134c76f9c7ddc663a05
Author: Bugra Ozturk <[email protected]>
AuthorDate: Thu Apr 24 15:34:31 2025 +0200
feat (airflowctl): Allow adding the same commands with custom action
commands (#49674)
---
airflow-ctl/src/airflowctl/ctl/cli_config.py | 61 ++++++++++++++++-
.../tests/airflow_ctl/ctl/test_cli_config.py | 80 +++++++++++++++++++++-
2 files changed, 137 insertions(+), 4 deletions(-)
diff --git a/airflow-ctl/src/airflowctl/ctl/cli_config.py
b/airflow-ctl/src/airflowctl/ctl/cli_config.py
index 79cc4c824ee..245f9c09aae 100644
--- a/airflow-ctl/src/airflowctl/ctl/cli_config.py
+++ b/airflow-ctl/src/airflowctl/ctl/cli_config.py
@@ -227,7 +227,7 @@ class CommandFactory:
args_map: dict[tuple, list[Arg]]
func_map: dict[tuple, Callable]
commands_map: dict[str, list[ActionCommand]]
- group_commands_list: list[GroupCommand]
+ group_commands_list: list[CLICommand]
def __init__(self, file_path: str | Path | None = None):
self.datamodels_extended_map = {}
@@ -465,7 +465,7 @@ class CommandFactory:
)
@property
- def group_commands(self) -> list[GroupCommand]:
+ def group_commands(self) -> list[CLICommand]:
"""List of GroupCommands generated for airflowctl."""
self._inspect_operations()
self._create_args_map_from_operation()
@@ -475,6 +475,59 @@ class CommandFactory:
return self.group_commands_list
+def merge_commands(
+ base_commands: list[CLICommand], commands_will_be_merged: list[CLICommand]
+) -> list[CLICommand]:
+ """
+ Merge group commands with existing commands which extends base_commands
with will_be_merged commands.
+
+ Args:
+ base_commands: List of base commands to be extended.
+ commands_will_be_merged: List of group commands to be merged with
base_commands.
+
+ Returns:
+ List of merged commands.
+ """
+ merge_command_map = {}
+ for command in commands_will_be_merged:
+ if isinstance(command, GroupCommand):
+ merge_command_map[command.name] = command
+ new_commands: list[CLICommand] = []
+ merged_commands = []
+ # Common commands
+ for command in base_commands:
+ if command.name in merge_command_map.keys():
+ merged_command = merge_command_map[command.name]
+ if isinstance(command, GroupCommand):
+ # Merge common group command with existing group command
+ current_subcommands = list(command.subcommands)
+ current_subcommands.extend(list(merged_command.subcommands))
+ new_commands.append(
+ GroupCommand(
+ name=command.name,
+ help=command.help,
+ subcommands=current_subcommands,
+ api_operation=merged_command.api_operation,
+ description=merged_command.description,
+ epilog=command.epilog,
+ )
+ )
+ elif isinstance(command, ActionCommand):
+ new_commands.append(merged_command)
+ merged_commands.append(command.name)
+ else:
+ new_commands.append(command)
+ # Discrete commands
+ new_commands.extend(
+ [
+ merged_command
+ for merged_command in merge_command_map.values()
+ if merged_command.name not in merged_commands
+ ]
+ )
+ return new_commands
+
+
command_factory = CommandFactory()
AUTH_COMMANDS = (
@@ -497,4 +550,6 @@ core_commands: list[CLICommand] = [
),
]
# Add generated group commands
-core_commands.extend(command_factory.group_commands)
+core_commands = merge_commands(
+ base_commands=command_factory.group_commands,
commands_will_be_merged=core_commands
+)
diff --git a/airflow-ctl/tests/airflow_ctl/ctl/test_cli_config.py
b/airflow-ctl/tests/airflow_ctl/ctl/test_cli_config.py
index 75e1261f6e3..94073d8350f 100644
--- a/airflow-ctl/tests/airflow_ctl/ctl/test_cli_config.py
+++ b/airflow-ctl/tests/airflow_ctl/ctl/test_cli_config.py
@@ -25,7 +25,7 @@ from typing import Any
import pytest
from airflowctl.api.datamodels.generated import ReprocessBehavior
-from airflowctl.ctl.cli_config import CommandFactory, GroupCommand
+from airflowctl.ctl.cli_config import ActionCommand, CommandFactory,
GroupCommand, merge_commands
@pytest.fixture
@@ -167,3 +167,81 @@ class TestCommandFactory:
assert arg.kwargs["default"] == test_arg[1]["default"]
assert arg.kwargs["type"] == test_arg[1]["type"]
assert arg.kwargs["dest"] == test_arg[1]["dest"]
+
+
+class TestCliConfigMethods:
+ def test_merge_commands(self, no_op_method):
+ """Test the merge_commands method."""
+ # Create two Command objects with different names and help texts
+ action_commands_1 = (
+ ActionCommand(
+ name="subcommand1",
+ help="This is command 1",
+ func=no_op_method,
+ args=(),
+ ),
+ ActionCommand(
+ name="subcommand2",
+ help="This is command 2",
+ func=no_op_method,
+ args=(),
+ ),
+ )
+ action_commands_2 = (
+ ActionCommand(
+ name="subcommand3",
+ help="This is command 3",
+ func=no_op_method,
+ args=(),
+ ),
+ ActionCommand(
+ name="subcommand4",
+ help="This is command 4",
+ func=no_op_method,
+ args=(),
+ ),
+ )
+ command_list_1 = [
+ GroupCommand(
+ name="command1",
+ help="This is command 1",
+ subcommands=action_commands_1,
+ ),
+ GroupCommand(
+ name="command2",
+ help="This is command 2",
+ subcommands=action_commands_2,
+ ),
+ ]
+ command_list_2 = [
+ GroupCommand(
+ name="command1",
+ help="This is command 1 new help",
+ description="This is command 1 new description",
+ subcommands=action_commands_2,
+ ),
+ GroupCommand(
+ name="command4",
+ help="This is command 4",
+ subcommands=action_commands_1,
+ ),
+ ]
+
+ # Merge the commands
+ merged_command = merge_commands(base_commands=command_list_1,
commands_will_be_merged=command_list_2)
+ merged_command_names = [command.name for command in merged_command]
+ assert "command1" in merged_command_names
+ assert "command2" in merged_command_names
+ assert "command3" not in merged_command_names
+ assert "command4" in merged_command_names
+
+ for command in merged_command:
+ if command.name == "command1":
+ # assert command.help == "This is command 1 new help"
+ # assert command.description == "This is command 1 new
description"
+ sub_command_names = [sc.name for sc in
list(command.subcommands)]
+ print(f"sub_command_names: {sub_command_names}")
+ assert "subcommand1" in sub_command_names
+ assert "subcommand2" in sub_command_names
+ assert "subcommand3" in sub_command_names
+ assert "subcommand4" in sub_command_names