This is an automated email from the ASF dual-hosted git repository.

taragolis 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 f84c05989e Move cli's Connection export and Variable export command 
print logic to a seprate function (#34647)
f84c05989e is described below

commit f84c05989eaabaaa273a9e70e2b97b0281f9b98d
Author: Utkarsh Sharma <[email protected]>
AuthorDate: Thu Oct 19 22:36:18 2023 +0530

    Move cli's Connection export and Variable export command print logic to a 
seprate function (#34647)
    
    * Move export output logic to a seprate function
    
    * Standardise airflow users export output
    
    * Use file object to derive values
    
    * Check if the file is closed or not
---
 airflow/auth/managers/fab/cli_commands/user_command.py |  3 ++-
 airflow/cli/commands/connection_command.py             | 10 +++-------
 airflow/cli/commands/variable_command.py               |  8 ++------
 airflow/cli/utils.py                                   | 10 +++++++++-
 4 files changed, 16 insertions(+), 15 deletions(-)

diff --git a/airflow/auth/managers/fab/cli_commands/user_command.py 
b/airflow/auth/managers/fab/cli_commands/user_command.py
index e82ece05d5..f970c5bb5a 100644
--- a/airflow/auth/managers/fab/cli_commands/user_command.py
+++ b/airflow/auth/managers/fab/cli_commands/user_command.py
@@ -31,6 +31,7 @@ from marshmallow.exceptions import ValidationError
 
 from airflow.auth.managers.fab.cli_commands.utils import 
get_application_builder
 from airflow.cli.simple_table import AirflowConsole
+from airflow.cli.utils import print_export_output
 from airflow.utils import cli as cli_utils
 from airflow.utils.cli import suppress_logs_and_warning
 from airflow.utils.providers_configuration_loader import 
providers_configuration_loaded
@@ -173,7 +174,7 @@ def users_export(args):
 
         with open(args.export, "w") as file:
             file.write(json.dumps(users, sort_keys=True, indent=4))
-            print(f"{len(users)} users successfully exported to {file.name}")
+            print_export_output("users", users, file)
 
 
 @cli_utils.action_cli
diff --git a/airflow/cli/commands/connection_command.py 
b/airflow/cli/commands/connection_command.py
index 90d7bd553c..e279faf48c 100644
--- a/airflow/cli/commands/connection_command.py
+++ b/airflow/cli/commands/connection_command.py
@@ -19,7 +19,6 @@ from __future__ import annotations
 
 import json
 import os
-import sys
 import warnings
 from pathlib import Path
 from typing import Any
@@ -29,7 +28,7 @@ from sqlalchemy import select
 from sqlalchemy.orm import exc
 
 from airflow.cli.simple_table import AirflowConsole
-from airflow.cli.utils import is_stdout
+from airflow.cli.utils import is_stdout, print_export_output
 from airflow.compat.functools import cache
 from airflow.configuration import conf
 from airflow.exceptions import AirflowNotFoundException
@@ -171,7 +170,7 @@ def connections_export(args):
         provided_file_format = f".{(args.format or args.file_format).lower()}"
 
     with args.file as f:
-        if file_is_stdout := is_stdout(f):
+        if is_stdout(f):
             filetype = provided_file_format or default_format
         elif provided_file_format:
             filetype = provided_file_format
@@ -196,10 +195,7 @@ def connections_export(args):
 
         f.write(msg)
 
-    if file_is_stdout:
-        print(f"\n{len(connections)} connections successfully exported.", 
file=sys.stderr)
-    else:
-        print(f"{len(connections)} connections successfully exported to 
{args.file.name}.")
+    print_export_output("Connections", connections, f)
 
 
 alternative_conn_specs = ["conn_type", "conn_host", "conn_login", 
"conn_password", "conn_schema", "conn_port"]
diff --git a/airflow/cli/commands/variable_command.py 
b/airflow/cli/commands/variable_command.py
index bc09d0208d..2a624bd738 100644
--- a/airflow/cli/commands/variable_command.py
+++ b/airflow/cli/commands/variable_command.py
@@ -20,13 +20,12 @@ from __future__ import annotations
 
 import json
 import os
-import sys
 from json import JSONDecodeError
 
 from sqlalchemy import select
 
 from airflow.cli.simple_table import AirflowConsole
-from airflow.cli.utils import is_stdout
+from airflow.cli.utils import print_export_output
 from airflow.models import Variable
 from airflow.utils import cli as cli_utils
 from airflow.utils.cli import suppress_logs_and_warning
@@ -132,7 +131,4 @@ def variables_export(args):
 
     with args.file as varfile:
         json.dump(var_dict, varfile, sort_keys=True, indent=4)
-        if is_stdout(varfile):
-            print("\nVariables successfully exported.", file=sys.stderr)
-        else:
-            print(f"Variables successfully exported to {varfile.name}.")
+        print_export_output("Variables", var_dict, varfile)
diff --git a/airflow/cli/utils.py b/airflow/cli/utils.py
index 361495c249..d9476c10a8 100644
--- a/airflow/cli/utils.py
+++ b/airflow/cli/utils.py
@@ -17,8 +17,9 @@
 
 from __future__ import annotations
 
+import io
 import sys
-from typing import TYPE_CHECKING
+from typing import TYPE_CHECKING, Collection
 
 if TYPE_CHECKING:
     from io import IOBase
@@ -40,3 +41,10 @@ def is_stdout(fileio: IOBase) -> bool:
     .. warning:: *fileio* must be open for this check to be successful.
     """
     return fileio.fileno() == sys.stdout.fileno()
+
+
+def print_export_output(command_type: str, exported_items: Collection, file: 
io.TextIOWrapper):
+    if not file.closed and is_stdout(file):
+        print(f"\n{len(exported_items)} {command_type} successfully 
exported.", file=sys.stderr)
+    else:
+        print(f"{len(exported_items)} {command_type} successfully exported to 
{file.name}.")

Reply via email to